一、文档定位与目标
本指南基于真实环境(Aliyun ECS 1核 0.5GB内存)的搭建记录复盘生成。旨在为零基础用户提供一份可逐行复制执行的操作手册,涵盖从服务器初始化、安全加固、资源优化到 Hexo 博客自动化部署的全过程。文档兼具教学价值与故障排除手册功能。
二、项目概览与技术栈
- 项目目标:搭建独立部署的个人博客,实现 “本地写作 -> 一键推送 -> 自动发布” 的 DevOps 流程。
- 硬件约束:0.5GB RAM (极度受限,需精细化内存管理,严防 OOM)。
- 操作系统:Ubuntu 22.04 LTS。
- 完整技术栈:
- 云平台:阿里云 ECS (Elastic Compute Service)
- Web服务器:Nginx (高性能,低内存占用)
- 静态生成器:Hexo (本地生成,服务器仅托管静态文件)
- 自动化:Git Hooks (实现 CI/CD 流水线)
- 安全:SSH Key认证 + 自动 Swap + 防火墙 + SSHD 硬化
三、分阶段详细操作手册
阶段 1:服务器初始访问与安全基线
阶段目标:建立安全的远程连接,摒弃不安全的密码登录,启用密钥认证体系。
1.1 首次连接(密码验证)
前置检查:在阿里云控制台重置实例密码,并重启实例。确认安全组(防火墙)入方向已放行 22 端口。
1 2 3 4
|
ssh root@47.xxx.xxx.xxx
|
- 交互说明:
- 出现
Are you sure you want to continue connecting (yes/no/fingerprint)? -> 输入 yes 并回车。
- 出现
root@... password: -> 输入密码(注意:Linux 终端输入密码不显示任何字符),输完盲打回车。
- 预期输出:出现
Welcome to Alibaba Cloud Elastic Compute Service! 及命令提示符 root@...:~#。
1.2 配置 SSH 密钥登录(免密)
前置检查:本地需已有 SSH 公钥(通常在 ~/.ssh/id_rsa.pub 或 ~/.ssh/id_ed25519.pub)。若无,请先运行 ssh-keygen 生成。
步骤 A:本地获取公钥
1 2 3
| Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
|
步骤 B:服务器端写入白名单
1 2 3 4 5 6 7 8 9
| mkdir -p ~/.ssh chmod 700 ~/.ssh
echo "[YOUR_PUBLIC_KEY]" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
|
验证步骤:保持当前窗口不关,新建一个本地终端窗口,执行 ssh root@<IP>。若直接登录无需密码,则成功。
1.3 SSH 服务硬化(禁用密码登录)
警告:必须先验证密钥登录成功,否则会把自己锁在门外。
1 2 3 4 5 6 7 8 9 10 11 12
| nano /etc/ssh/sshd_config
PasswordAuthentication no PermitRootLogin prohibit-password
sshd -t systemctl restart ssh
|
阶段 2:资源优化与生存保障(0.5GB 专用)
阶段目标:移除云厂商监控代理(节省 ~50MB 内存),配置 Swap 防止 OOM(内存溢出)崩溃。
2.1 移除阿里云监控插件
说明:低配机器上,监控插件是主要内存杀手。
1 2 3 4 5 6 7 8 9
| systemctl stop argusagent pkill -9 argusagent rm -rf /usr/local/cloudmonitor
wget http://update.aegis.aliyun.com/download/uninstall.sh chmod +x uninstall.sh ./uninstall.sh
|
- 验证步骤:执行
top 命令,确保列表中不再有 AliYunDun 或 argusagent 进程。
2.2 创建 Swap 交换空间(生命线)
说明:当物理内存耗尽时,利用磁盘充当虚拟内存,防止系统卡死或杀进程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile swapon /swapfile
cp /etc/fstab /etc/fstab.bak echo '/swapfile none swap sw 0 0' >> /etc/fstab
|
- 验证步骤:执行
free -h。确保 Swap: 行显示 Total 为 2.0Gi。
阶段 3:Web 服务器部署与修复
阶段目标:修复系统依赖,安装 Nginx 并配置静态站点托管。
3.1 系统更新与依赖修复
常见问题:0.5GB 机器在 apt update 时容易因内存不足卡死或被中断,导致锁文件残留。
1 2 3 4 5 6 7 8 9
| rm /var/lib/dpkg/lock* rm /var/lib/apt/lists/lock
dpkg --configure -a
apt update && apt upgrade -y
|
3.2 安装与启动 Nginx
1 2 3 4 5 6 7 8 9
| apt install nginx -y
systemctl enable nginx systemctl start nginx
systemctl status nginx
|
- 验证步骤:浏览器访问
http://<服务器IP>。应看到 Welcome to nginx! 页面。
阶段 4:自动化部署流水线搭建
阶段目标:利用 Git Hooks 实现代码推送到服务器后,自动部署到 Nginx 目录。
4.1 创建目录结构
1 2 3 4 5 6 7
| mkdir -p /var/www/hexo
mkdir -p /var/repo/hexo.git cd /var/repo/hexo.git git init --bare
|
4.2 编写 Post-Receive 钩子
核心逻辑:Git 收到代码后,强制检出(Checkout)到网站目录。
1 2 3 4 5 6 7 8 9 10 11 12
| cd hooks nano post-receive
git --work-tree=/var/www/hexo --git-dir=/var/repo/hexo.git checkout -f
chmod +x post-receive
|
4.3 配置 Nginx 指向新目录
1 2 3 4 5 6 7 8
| nano /etc/nginx/sites-enabled/default
systemctl reload nginx
|
阶段 5:本地开发环境与部署 (Windows 端)
阶段目标:在本地生成博客内容并推送到服务器。
5.1 Node.js 环境准备
注意:Hexo 7+ 需要 Node.js 14+。若版本过低需升级。
5.2 Hexo 初始化与配置
1 2 3 4 5 6 7 8 9 10
| npm install -g hexo-cli
hexo init myblog cd myblog npm install
npm install hexo-deployer-git --save
|
5.3 配置连接参数
编辑博客根目录下的 _config.yml 文件,修改底部 deploy 部分:
1 2 3 4
| deploy: type: git repo: root@<服务器IP>:/var/repo/hexo.git branch: master
|
5.4 首次部署
1 2
| hexo clean; hexo g; hexo d
|
- 验证步骤:
- 终端显示
INFO Deploy done: git。
- 浏览器访问
http://<服务器IP>,应看到 Hexo 默认博客页面。
四、常见 DEBUG 场景库(故障排除中心)
场景 1:SSH 连接被拒绝或卡死
- 现象:
Permission denied (publickey)
Connection closed by remote host
- 输入命令后光标闪烁无反应(卡死)
- 可能原因:
- 密钥未正确添加到
authorized_keys。
- 服务器负载过高(CPU/内存满载)导致 SSH 无法响应。
- 防火墙/安全组未放行端口 22。
- 解决方案:
- 权限排查:尝试带详细日志连接
ssh -v root@IP。
- 强制重启:若长时间无反应,去阿里云控制台“强制重启”实例。重启后立即执行
rm /var/lib/dpkg/lock* 清理残留锁。
- 回退密码:若密钥完全失效,去控制台使用 VNC 登录修改
sshd_config 重新允许密码登录 (PasswordAuthentication yes)。
场景 2:apt/dpkg 安装软件时卡住或被 Killed
- 现象:
Processing triggers for man-db... 卡住不动。
- 出现
Killed 字样,随后进程终止。
- 可能原因:
- OOM (Out of Memory):内存耗尽,Linux 内核杀死了 apt 进程。
- 解决方案:
- 检查 Swap:执行
free -h 确认 Swap 是否存在且被使用。若无,参考阶段 2 重建。
- 自我修复:
1 2 3 4 5 6 7
| killall apt apt-get
rm /var/lib/apt/lists/lock rm /var/lib/dpkg/lock*
dpkg --configure -a
|
场景 3:Hexo 部署成功但访问仍是 “Welcome to nginx”
- 现象:本地显示
Deploy done: git,但网页内容未变。
- 可能原因:
Nginx 配置文件中的 root 路径未修改。
Git Hook 脚本没有执行权限 (chmod +x)。
- 浏览器缓存。
- 诊断步骤:
- 检查文件是否到了服务器:
ls /var/www/hexo。
- 检查
Nginx 配置:cat /etc/nginx/sites-enabled/default。
- 检查
Hook 权限:ls -l /var/repo/hexo.git/hooks/post-receive(必须有 x 位)。
场景 4:Windows 本地 hexo server 报错
- 现象:
hexo : 无法将“hexo”项识别为 cmdlet… 或 ERR_REQUIRE_ESM。
- 可能原因:
Node.js 路径变更导致环境变量失效。
Hexo 版本与 Node.js 版本不兼容 (Hexo 7+ 需要 Node 14+)。
- 解决方案:
- 重新安装 CLI:
npm install -g hexo-cli。
- 使用 npx:尝试
npx hexo server。
- 重装依赖:删除
node_modules 文件夹后重新 npm install。
五、命令速查表
系统健康检查
1 2 3 4
| free -h df -h top ss -antpl | grep 80
|
服务管理
1 2 3
| systemctl status nginx systemctl restart nginx systemctl reload nginx
|
Git 钩子调试
1 2 3
| cd /var/repo/hexo.git/hooks ./post-receive
|
六、配置示例库
1. 安全加固版 /etc/ssh/sshd_config (关键参数)
1 2 3 4
| Port 22 PermitRootLogin prohibit-password PubkeyAuthentication yes PasswordAuthentication no
|
2. Git Post-Receive 钩子 /var/repo/hexo.git/hooks/post-receive
1 2 3
| #!/bin/bash
git --work-tree=/var/www/hexo --git-dir=/var/repo/hexo.git checkout -f
|
3. Hexo 部署配置 _config.yml
1 2 3 4 5
| deploy: type: git repo: root@<你的公网IP>:/var/repo/hexo.git branch: master
|