Ubuntu 20.04 服务器运维日记 (2026-06-10)
服务器信息
| 项目 | 详情 |
|---|---|
| 型号 | 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_rsa1.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_keys1.3 启用密钥认证
编辑 /etc/ssh/sshd_config:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no重启 SSH 服务:
BASH
sudo systemctl restart sshd二、系统更新
2.1 更新软件源
BASH
sudo apt update2.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遇到的问题:
- Ubuntu 20.04 官方源安装的 Hugo 版本过旧(v0.68),PaperMod 主题需要更高版本
- 切换到 Mainroad 主题后成功运行
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 -y3.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/blog3.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 docker4.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 docker4.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 |
关键经验
- PowerShell SSH 远程操作存在变量转义问题(
$uri、引号、JSON),使用 base64 编码或 SCP 传输脚本是可靠的解决方案 - Ubuntu 20.04 官方源的 Hugo 版本过旧,如需使用新主题需手动安装新版 Hugo
- unattended-upgrades 进程可能占用 dpkg 锁,需要先处理再执行包管理操作
- Docker 镜像拉取在特殊网络环境下需要配置镜像加速器
后续规划
- [x] 配置 WordPress 主题和插件
- [ ] 设置 HTTPS(Let's Encrypt)
- [ ] 部署 Portainer 管理 Docker 容器
- [ ] 考虑部署 Nextcloud、Gitea 等应用
- [ ] 配置定期备份
记录时间:2026年6月10日
服务器:hw-RH2288-V3 (10.10.10.42)