Article
部署Hexo
介绍在 CentOS 7.9 环境下手动部署 Hexo 静态博客的完整流程。
部署Hexo
前言
由于CentOS 7.9不支持Node.js 18+的版本,导致Hexo的一键部署脚本部署不下来需要进行手动安装,核心原因是系统底层依赖库(glibc、libstdc++)版本过低,无法满足新版 Node.js 的运行要求。
1.生成并打包 Hexo 静态文件
1.1 首先需要有Hexo的完整依赖包
1 2
| hexo-starter-master.zip
|
1.2.本地进行编译
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| 1.对zip进行本地解压
2.win+x打开Windows PowerShell(终端管理员)
3.进入目录 cd hexo-starter-master
PS D:\hexo-starter-master> ls 目录: D:\hexo-starter-master Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2026/3/23 22:35 hexo-starter-master -a---- 2026/3/23 22:36 98 package-lock.json
PS D:\hexo-starter-master> cd hexo-starter-master PS D:\hexo-starter-master\hexo-starter-master> ls 目录: D:\hexo-starter-master\hexo-starter-master Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2026/3/23 22:35 .github d----- 2026/3/23 22:35 scaffolds d----- 2026/3/23 22:35 source d----- 2026/3/23 22:35 themes -a---- 2026/3/23 22:35 82 .gitignore -a---- 2026/3/23 22:35 615 package.json -a---- 2026/3/23 22:35 0 _config.landscape.yml -a---- 2026/3/23 22:35 2443 _config.yml
3. 安装依赖 npm install npm install -g hexo-cli
|
正常的 Hexo 项目应该包含以下文件:
1.3 生成静态文件
1 2 3 4 5 6 7 8
| hexo clean
hexo generate
ls public
|
1.4打包文件
1 2 3 4 5
| Compress-Archive -Path public -DestinationPath hexo-blog.zip -Force
ls *.zip
|
2.部署到 CentOS
2.1 将压缩包上传到服务器
1 2 3
| scp hexo-blog.zip root@your-server-ip:/opt/
|
2.2 部署hexo
2.2.1 一键部署脚本,手动部署可以看下边
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| cd /var/www/ rm -rf hexo unzip -o /root/hexo-blog.zip -d hexo_temp mv hexo_temp/public hexo rm -rf hexo_temp chown -R nginx:nginx /var/www/hexo rm /root/hexo-blog.zip systemctl restart nginx
cat > deploy.sh << EOF
#!/bin/bash # Hexo 博客部署脚本
set -e # 遇到错误立即退出
echo "========================================" echo "开始部署 Hexo 博客" echo "========================================"
# 1. 安装必要软件 echo "[1/8] 检查并安装 Nginx..." if ! command -v nginx &> /dev/null; then yum install epel-release -y yum install nginx -y systemctl start nginx systemctl enable nginx echo "✓ Nginx 安装完成" else echo "✓ Nginx 已安装" fi
# 2. 创建网站目录 echo "[2/8] 配置网站目录..." mkdir -p /var/www/hexo chown -R nginx:nginx /var/www/hexo chmod -R 755 /var/www/hexo echo "✓ 网站目录配置完成"
# 3. 配置 Nginx echo "[3/8] 配置 Nginx..." cat > /etc/nginx/conf.d/hexo.conf << 'EOF' server { listen 80; server_name _; root /var/www/hexo; index index.html; location / { try_files $uri $uri/ /index.html; } location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ { expires 7d; add_header Cache-Control "public, immutable"; } location ~ /\. { deny all; } } EOF
# 测试配置 nginx -t systemctl reload nginx echo "✓ Nginx 配置完成"
# 4. 配置防火墙 echo "[4/8] 配置防火墙..." if command -v firewall-cmd &> /dev/null; then firewall-cmd --permanent --add-service=http >/dev/null 2>&1 firewall-cmd --permanent --add-service=https >/dev/null 2>&1 firewall-cmd --reload >/dev/null 2>&1 echo "✓ 防火墙配置完成" else echo "⚠ 防火墙未运行,跳过配置" fi
# 5. 备份旧版本 echo "[5/8] 备份旧版本..." cd /var/www/ if [ -d "hexo" ]; then BACKUP_NAME="hexo_backup_$(date +%Y%m%d_%H%M%S)" echo "备份当前版本到 $BACKUP_NAME" mv hexo $BACKUP_NAME else echo "没有旧版本,跳过备份" fi
# 6. 解压新版本 echo "[6/8] 解压新版本..." if [ ! -f "/root/hexo-blog.zip" ]; then echo "错误: 找不到 /root/hexo-blog.zip 文件" echo "请先从 Windows 上传该文件到服务器" exit 1 fi
unzip -o /root/hexo-blog.zip -d hexo_temp mv hexo_temp/public hexo rm -rf hexo_temp echo "✓ 文件解压完成"
# 7. 设置权限 echo "[7/8] 设置文件权限..." chown -R nginx:nginx /var/www/hexo chmod -R 755 /var/www/hexo echo "✓ 权限设置完成"
# 8. 验证部署 echo "[8/8] 验证部署..." echo "部署的文件:" ls -la /var/www/hexo/
# 获取服务器IP SERVER_IP=$(hostname -I | awk '{print $1}')
echo "========================================" echo "✓ 部署完成!" echo "========================================" echo "访问地址: http://$SERVER_IP" echo "========================================"
# 清理上传的文件 rm /root/hexo-blog.zip
# 重启 Nginx 确保配置生效 systemctl restart nginx
echo "Nginx 状态:" systemctl status nginx --no-pager | head -10 EOF
|
2.2.2 执行脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 1. 给脚本执行权限 chmod +x deploy.sh
2. 执行脚本 ./deploy.sh1
3.查看状态
systemctl status nginx
netstat -tuln | grep :80
ls -la /var/www/hexo/
|
2.3 配置环境
2.3.1 配置conf文件
由于我的80端口已经被占用了,所以把nginx改为88端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| cat > /etc/nginx/conf.d/hexo.conf << EOF server { listen 88; # 修改为 88 端口 server_name _; # 监听所有域名/IP root /code/hexo/public; index index.html; ssl_certificate _; #你的证书路径 ssl_certificate_key _; #你的证书路径 # 字符集 charset utf-8; # 主要路由处理 location / { try_files $uri $uri/ /index.html; } # 静态资源缓存优化 location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg|eot)$ { expires 7d; add_header Cache-Control "public, immutable"; access_log off; } # 禁止访问隐藏文件 location ~ /\. { deny all; access_log off; log_not_found off; } # 自定义错误页面 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } EOF
nginx -t
systemctl reload nginx
systemctl status nginx
netstat -tuln | grep :88
|
2.3.2 部署Hexo
1 2 3 4 5 6 7 8
| 1.创建目录 mkdir /code/hexo
2.解压到目录 unzip /opt/hexo-blog.zip -d /code/hexo/
3.测试 curl -k -I https://域名或IP:88
|
自此Hexo就部署完成了
3.后续的上传文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| PS D:\hexo-starter-master\hexo-starter-master> ls 目录: D:\hexo-starter-master\hexo-starter-master Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2026/3/23 22:35 .github d----- 2026/3/23 22:45 node_modules d----- 2026/3/24 14:39 public d----- 2026/3/23 22:35 scaffolds d----- 2026/3/24 14:27 source d----- 2026/3/23 22:35 themes -a---- 2026/3/23 22:35 82 .gitignore -a---- 2026/3/24 14:48 49571 db.json -a---- 2026/3/23 22:45 104029 package-lock.json -a---- 2026/3/23 23:03 619 package.json -a---- 2026/3/24 14:40 2046166 public.zip -a---- 2026/3/23 22:35 0 _config.landscape.yml -a---- 2026/3/23 22:35 2443 _config.yml
PS D:\hexo-starter-master\hexo-starter-master>
PS D:\hexo-starter-master\hexo-starter-master> cd .\source\ PS D:\hexo-starter-master\hexo-starter-master\source> ls 目录: D:\hexo-starter-master\hexo-starter-master\source Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2026/3/24 14:27 images d----- 2026/3/24 14:39 _posts
PS D:\hexo-starter-master\hexo-starter-master\source>
hexo clean
hexo generate
hexo server
|
过程中会报的错误
1 2
| ERROR Process failed: _posts/file.md YAMLException: end of the stream or a document separator is expected
|
原因
1 2 3 4 5 6 7 8 9 10 11 12 13
| md文档最顶部的 Front-Matter 格式写错了,Hexo 解析失败!
--- title: name date: 2025-03-24 15:00:00 tags: Linux ---
这里开始写你的文章内容...
|