春风得意马蹄疾 记录技术,分享生活
随笔: 34 分类: 15 评论: 1 阅读: 2,171

Ubuntu 20.04 服务器运维日记 (2026-06-10)

📅 发表于 2026-06-10 21:40:00 👁 阅读 17 次 💬 评论 0

服务器信息

项目 详情
型号 hw-RH2288-V3
IP 地址 10.10.10.42
操作系统 Ubuntu 20.04 LTS
内存 30GB

一、SSH 远程连接配置

1.1 生成 SSH 密钥对

在 Windows 本地生成 RSA 4096 位密钥对:

BASH
ssh-keygen -t rsa -b 4096 -f C:\Users\xiaoajun\.ssh\id_rsa

1.2 配置免密登录

创建 testuser 账户并配置 sudo 免密码权限:

BASH
sudo useradd -m -s /bin/bash testuser
sudo usermod -aG sudo testuser
echo "testuser ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/testuser

将公钥部署到服务器:

BASH
mkdir -p ~/.ssh
echo "公钥内容" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

1.3 启用密钥认证

编辑 /etc/ssh/sshd_config

PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no

重启 SSH 服务:

BASH
sudo systemctl restart sshd

二、系统更新

2.1 更新软件源

BASH
sudo apt update

2.2 升级系统包

BASH
sudo apt upgrade -y

遇到的问题:unattended-upgrades 进程占用 dpkg 锁。解决方案:先杀掉相关进程,清理 /var/lib/dpkg/lock-frontend 等锁文件,再重新执行更新。


三、个人博客搭建

3.1 方案探索:Hugo 静态博客

最初尝试使用 Hugo 静态博客 + PaperMod 主题:

BASH
sudo apt install hugo -y
hugo new site /var/www/blog
cd /var/www/blog
git clone https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod

遇到的问题

3.2 最终方案:WordPress 动态博客

为了支持数据库和更丰富的功能,最终选择 WordPress。

3.2.1 安装 MySQL 8.0

BASH
sudo apt install mysql-server -y
sudo mysql_secure_installation

创建数据库和用户:

SQL
CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'wp_password_2026';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;

3.2.2 安装 PHP 7.4 FPM

BASH
sudo apt install php7.4-fpm php7.4-mysql php7.4-xml php7.4-mbstring php7.4-curl php7.4-zip php7.4-gd -y

3.2.3 部署 WordPress

BASH
wget https://cn.wordpress.org/latest-zh_CN.zip
sudo unzip latest-zh_CN.zip -d /var/www/
sudo mv /var/www/wordpress /var/www/blog
sudo chown -R www-data:www-data /var/www/blog

3.2.4 配置 Nginx

Nginx 站点配置文件 /etc/nginx/sites-available/blog

NGINX
server {
    listen 80;
    server_name 10.10.10.42;
    root /var/www/blog;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

启用站点:

BASH
sudo ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx

遇到的问题:PowerShell 通过 SSH 传输 Nginx 配置时,$uri 变量被 PowerShell 错误转义。解决方案:使用 base64 编码传输,或通过 SCP 直接传输 Python 脚本执行。

3.2.5 验证

博客成功运行,可通过 http://10.10.10.42 访问。


四、Docker 容器环境搭建

4.1 安装 Docker

BASH
sudo apt install docker.io -y
sudo apt install docker-compose -y
sudo usermod -aG docker testuser
sudo systemctl enable docker
sudo systemctl start docker

4.2 配置镜像加速

创建 /etc/docker/daemon.json

JSON
{
  "registry-mirrors": [
    "https://hub.rat.dev",
    "https://docker.1panel.live",
    "https://dockerpull.org"
  ]
}

重启 Docker:

BASH
sudo systemctl daemon-reload
sudo systemctl restart docker

4.3 验证

BASH
docker pull nginx:alpine

遇到的问题:PowerShell 通过 SSH echo 写入 JSON 时,引号和花括号被错误转义。解决方案:通过 SCP 传输 Python 脚本到服务器执行,完成文件写入。


五、技术总结

组件 版本/详情
操作系统 Ubuntu 20.04 LTS
Web 服务器 Nginx 1.18
PHP 7.4 FPM
数据库 MySQL 8.0
博客系统 WordPress (中文版)
容器引擎 Docker 26.1.3
容器编排 Docker Compose 1.25.0

关键经验

  1. PowerShell SSH 远程操作存在变量转义问题($uri、引号、JSON),使用 base64 编码或 SCP 传输脚本是可靠的解决方案
  2. Ubuntu 20.04 官方源的 Hugo 版本过旧,如需使用新主题需手动安装新版 Hugo
  3. unattended-upgrades 进程可能占用 dpkg 锁,需要先处理再执行包管理操作
  4. Docker 镜像拉取在特殊网络环境下需要配置镜像加速器

后续规划


记录时间:2026年6月10日
服务器:hw-RH2288-V3 (10.10.10.42)

📂 分类:日常记录
评论 (0)
小阿军
技术分享者 · 记录成长的每一步
✨ 特效 ON