Hexo 博客部署指南

本文档记录在 Ubuntu 22.04 服务器上部署 Hexo 博客的完整流程。

快速部署

直接使用一键部署脚本:

1
2
wget http://115.29.231.140/shell/hexo-auto-deploy.sh
sudo bash hexo-auto-deploy.sh

环境准备

安装 nvm

1
2
3
4
5
git clone https://gitee.com/mirrors/nvm.git ~/.nvm
source ~/.nvm/nvm.sh
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bashrc
source ~/.bashrc

安装 Node.js 24 LTS

1
2
3
4
5
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/
nvm install --lts
nvm alias default node
node -v
npm -v

安装 Hexo

1
2
3
4
5
6
7
npm config set registry https://registry.npmmirror.com
npm install -g hexo-cli

mkdir ~/hexo-blog
cd ~/hexo-blog
hexo init
npm install

配置主题

安装 Butterfly 主题

1
2
3
git clone -b master https://gitee.com/immyw/hexo-theme-butterfly.git themes/butterfly
npm install hexo-renderer-pug hexo-renderer-stylus --save
cp themes/butterfly/_config.yml ./_config.butterfly.yml

启用主题

修改 _config.yml

1
theme: butterfly

配置 Live2D

安装插件和模型

1
npm install --save hexo-helper-live2d live2d-widget-model-tororo

配置 Live2D

_config.yml 添加:

1
2
3
4
5
6
7
8
live2d:
enable: true
model:
use: live2d-widget-model-tororo
display:
position: left
width: 100
height: 200

配置静态资源压缩

1
npm install --save hexo-neat

_config.yml 添加:

1
2
3
4
5
6
7
8
neat_enable: true
neat_html:
enable: true
neat_css:
enable: true
neat_js:
enable: true
mangle: true

图片压缩

安装 tinypng CLI

1
npm install -g tinypng-cli

配置 API Key

1
echo 'export TINYPNG_KEY="your_key"' >> ~/.bashrc

压缩图片

1
2
tinypng source/img
tinypng themes/butterfly/source/img

进程管理

安装 pm2

1
npm install -g pm2

创建启动脚本

1
2
3
4
5
6
7
8
cat > ~/hexo-blog/start.sh << 'EOF'
#!/bin/bash
cd ~/hexo-blog
hexo clean
hexo g
hexo s -p 4000
EOF
chmod +x ~/hexo-blog/start.sh

启动博客

1
2
3
pm2 start ~/hexo-blog/start.sh --name "hexo-blog"
pm2 save
pm2 startup

配置 Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cat > /etc/nginx/sites-available/hexo << 'EOF'
server {
listen 80;
server_name your_ip;

location /hexo {
proxy_pass http://127.0.0.1:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

location / {
root /var/www/html;
try_files $uri $uri/ =404;
}
}
EOF

ln -sf /etc/nginx/sites-available/hexo /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

常用操作

新建文章

1
2
3
4
5
cd ~/hexo-blog
hexo new "文章标题"
# 编辑 source/_posts/xxx.md
hexo clean && hexo g
pm2 restart hexo-blog

放入 markdown 文件

1
2
3
4
# 把 md 文件放到 source/_posts/ 目录
cd ~/hexo-blog
hexo clean && hexo g
pm2 restart hexo-blog

pm2 常用命令

1
2
3
4
5
pm2 status              # 查看状态
pm2 logs hexo-blog # 查看日志
pm2 restart hexo-blog # 重启
pm2 stop hexo-blog # 停止
pm2 delete hexo-blog # 删除

一键部署

直接运行以下命令即可完成全部部署:

1
2
wget http://115.29.231.140/shell/hexo-auto-deploy.sh
sudo bash hexo-auto-deploy.sh

部署脚本会自动完成以下操作:

  1. 更新系统并安装基础依赖
  2. 安装 nvm 和 Node.js 24 LTS
  3. 配置 npm 国内镜像
  4. 安装 hexo-cli 和 pm2
  5. 拉取并解压博客压缩包
  6. 安装所有依赖和插件(Butterfly、Live2D、hexo-neat 等)
  7. 配置 nginx
  8. 启动博客并设置开机自启

部署完成后,访问 http://你的服务器IP/hexo 即可查看博客。