本地环境
本地Hexo环境搭建
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
npm install -g hexo-cli
hexo init my-blog
cd my-blog npm install
|
测试
服务器环境
准备服务器环境(Ubuntu)
1 2 3 4 5 6 7 8 9 10
| ssh root@你的服务器IP
sudo apt update sudo apt install git nginx -y
sudo mkdir -p /var/www/hexo sudo chown -R $USER:$USER /var/www/hexo
|
配置Git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mkdir -p /var/repo/
git init --bare hexo.git
vim /var/repo/hexo.git/hooks/post-receive
git --work-tree=/var/www/hexo --git-dir=/var/repo/{自定义仓库名name}.git checkout -f
chmod +x hexo.git/hooks/post-receive
|
修改本地Hexo配置
1 2 3 4 5 6 7 8 9 10 11 12 13
|
deploy: type: git repo: root@你的服务器IP:/var/repo/hexo.git branch: master
|
安装部署插件 + 测试推送
1 2 3 4 5 6 7 8 9 10
|
npm install hexo-deployer-git --save
hexo clean hexo g hexo d
|
配置Nginx指向站点
1 2 3 4 5
| ssh root@你的服务器IP
sudo vim /etc/nginx/sites-available/default
|
启用配置并重启 Nginx
1 2 3 4 5 6 7 8 9 10 11 12
| sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx
|
专属配置配置(根据需求修改,需要先申请证书)
1 2 3 4 5
| sudo vim /etc/nginx/sites-available/hexo
sudo ln -s /etc/nginx/sites-available/hexo /etc/nginx/sites-enabled/hexo
|
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
| server { listen 80 default_server; listen [::]:80 default_server; return 444; }
server { listen 80; server_name yourdomain.me; return 301 https://www.yourdomain.me$request_uri; }
server { listen 80; server_name www.yourdomain.me; return 301 https://www.yourdomain.me$request_uri; }
server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name www.yourdomain.me; ssl_certificate /etc/letsencrypt/live/yourdomain.me/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.me/privkey.pem; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; ssl_prefer_server_ciphers on; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Content-Type-Options nosniff; add_header X-Frame-Options "SAMEORIGIN"; root /var/www/hexo; index index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ =404; } location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; add_header Cache-Control "public"; } location ~ /\.(?!well-known).* { deny all; access_log off; log_not_found off; } error_page 404 /404.html; location = /404.html { internal; } }
server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name yourdomain.me; ssl_certificate /etc/letsencrypt/live/yourdomain.me/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.me/privkey.pem; return 301 https://www.yourdomain.me$request_uri; }
|
添加域名
域名解析配置(关键步骤)
阿里云操作流程:
登录 阿里云控制台
进入「云解析 DNS」→ 选择你的域名
点击「添加记录」:
1 2 3 4
| 记录类型: A 主机记录: @ // 主域名解析(如 yourdomain.com) 记录值: 你的服务器IP TTL: 10分钟 // 快速生效
|
再添加一条 WWW 解析:
1 2 3
| 记录类型: A 主机记录: www // www.yourdomain.com 记录值: 你的服务器IP
|
解析生效时间:通常 5-30 分钟(全球最长不超过 48 小时)
验证解析是否生效
1 2 3
| dig yourdomain.com +short
|
申请免费 SSL 证书(启用 HTTPS)
使用 Certbot 自动获取 Let’s Encrypt 证书:
1 2 3 4 5 6 7
|
sudo apt update sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
|
按提示操作:
- 输入邮箱(接收证书更新提醒)
- 同意服务条款
- 选择
2: Redirect(强制所有流量跳转 HTTPS)
证书将自动续期(无需手动管理)
Hexo 配置更新
修改 Hexo 站点配置
编辑 _config.yml:
1 2 3 4
| url: https://yourdomain.com root: / permalink: :year/:month/:title/
|
重建博客并部署
1 2 3 4 5 6 7 8
| hexo clean
hexo generate
hexo deploy
|
其他配置
用 SSH 密钥免密登录,实现部署免密
本地生成密钥对
上传公钥到服务器
1
| ssh-copy-id root@你的服务器IP
|
测试免密登录
重新部署
自动化优化:一键部署脚本
在本地博客目录创建deploy.sh,每次只需要在博客的根目录执行./deploy.sh
在 Hexo 博客根目录执行:
1 2 3 4
| touch deploy.sh //创建 chmod +x deploy.sh //添加执行权限 vim deploy.sh //编辑
|
粘贴以下完整脚本(macos)
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
| #!/bin/bash
clear echo echo "██████ 开始部署博客进程 ██████" echo
BLOG_URL="https://你的IP或域名"
echo echo "[1/3] 🧹 清理生成缓存..." hexo clean --silent || { echo "❌ 清理失败!" exit 1 }
echo echo "[2/3] 🏗️ 生成静态页面..." hexo generate --silent || { echo "❌ 生成静态文件失败!" open -a "TextEdit" ./debug.log exit 1 }
echo echo "[3/3] 🚀 部署到服务器..." hexo deploy --silent || { echo "❌ 部署失败!尝试修复权限..." git config --global --add safe.directory $(pwd) hexo deploy --silent || exit 1 }
TIME_USED=$((SECONDS)) echo echo "██████████████████████████████" echo "✅ 部署成功!耗时: ${TIME_USED}秒" echo "博客地址: $BLOG_URL" echo "██████████████████████████████"
echo read -p "是否打开浏览器?[y/N] " -n 1 REPLY if [[ $REPLY =~ ^[Yy]$ ]]; then open $BLOG_URL fi
exit 0
|