Hexo 博客部署与自动化发布指南

本文记录了从零搭建 Hexo 博客、配置自动化部署、日常写作与管理文章的全过程。
部署环境云服务器

目的:就是为了让我方便管理


✅ 0. 环境说明

项目
系统 Ubuntu
Web 服务器 Nginx
博客框架 Hexo
部署方式 Git 钩子自动化部署
博客目录 ~/hexo
网站根目录 /var/www/blog
服务访问 http://115.190.228.209/

✅ 1. 初始化 Hexo(服务器上执行)

1
2
3
4
mkdir -p ~/hexo && cd ~/hexo
npm install -g hexo-cli
hexo init .
npm i

✅ 2. 安装博客主题(NexT)

Git 方式

1
2
3
cd ~/hexo
git clone https://github.com/next-theme/hexo-theme-next themes/next
sed -i 's/^theme:.*/theme: next/' _config.yml

无法 git clone 时可使用 ZIP 下载方式


✅ 3. Hexo 配置 _config.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
title: 我的技术博客
subtitle: 记录与分享
description: 技术 / SLAM / 编程
author: 你的名字
language: zh-CN
timezone: Asia/Shanghai

url: http://115.190.228.209
root: /
permalink: :year/:month/:day/:title/
theme: next

highlight:
enable: true
line_number: true

✅ 4. 首次发布(服务器手动执行一次)

1
2
3
4
5
cd ~/hexo
hexo clean && hexo g

sudo mkdir -p /var/www/blog
sudo rsync -av --delete public/ /var/www/blog/

✅ 5. Nginx 配置

文件:/etc/nginx/sites-available/blog

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name 115.190.228.209;

root /var/www/blog;
index index.html;

location / {
try_files $uri $uri/ /index.html;
}
}

启用并重载:

1
2
sudo ln -sf /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/blog
sudo nginx -t && sudo systemctl reload nginx

访问:
📎 http://115.190.228.209/


✅ 6. 配置 Git 自动部署

创建仓库

1
2
3
mkdir -p ~/repos/hexo.git
cd ~/repos/hexo.git
git init --bare

创建 post-receive 钩子

~/repos/hexo.git/hooks/post-receive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
set -e
WEB_DIR="$HOME/hexo"
NGINX_DIR="/var/www/blog"

if [ ! -d "$WEB_DIR/.git" ]; then
git clone ~/repos/hexo.git "$WEB_DIR"
else
cd "$WEB_DIR"
git fetch --all
git reset --hard origin/main
fi

cd "$WEB_DIR"
npm i
npx hexo clean && npx hexo g
rsync -av --delete public/ "$NGINX_DIR/"
systemctl reload nginx || true

给权限:

1
chmod +x ~/repos/hexo.git/hooks/post-receive

✅ 7. 本地绑定远程仓库

本地运行:

1
2
3
4
5
6
git init
git add .
git commit -m "init blog"
git branch -M main
git remote add prod ssh://root@115.190.228.209/~/repos/hexo.git
git push prod main

以后发布文章只需:

1
2
3
git add .
git commit -m "update posts"
git push origin main

✈️ 自动部署完成!


✅ 8. 写作与管理文章

新建文章

1
hexo new post "文章标题"

生成位置:

1
source/_posts/文章标题.md

新建草稿

1
hexo new draft "草稿标题"

草稿发布

1
hexo publish "草稿标题"

删除文章

1
2
3
4
rm source/_posts/文件名.md
git add -A
git commit -m "delete post"
git push prod main

隐藏文章(不删除)

在 MD 文件头增加:

1
published: false

✅ 9. 本地预览

1
2
hexo server
# 浏览器访问 http://localhost:4000

✅ 10. 常见问题

问题 解决方案
页面没更新 hexo clean && hexo g && git push
Nginx 报错 sudo nginx -t 检查语法
图片不显示 source/images 并用 /images/xxx.png 引用
git clone 慢 改用 ZIP 下载主题

🎉 完成!

现在你已经拥有:

  • ✅ Hexo 博客
  • ✅ NexT 主题
  • ✅ Nginx 部署
  • ✅ Git 自动发布系统
  • ✅ 新建/删除文章流程

写完 Markdown → git push → 自动上线 ✨


Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment