概述
Hexo 是一个快速、简洁且高效的静态博客框架, 基于 Node.js 构建. 它支持 Markdown 语法, 可以通过插件快速扩展功能, 并能一键部署到 GitHub Pages、Vercel、Netlify 等平台.
本指南将详细介绍如何使用 Hexo 搭建个人博客,配置 Butterfly 主题,并通过 GitHub Actions 实现自动部署.
Hexo 官方文档
Butterfly 主题文档
选择 Hexo 的原因: 生成速度快(100+ 文章只需几秒)、插件生态丰富、部署简单、主题支持多
环境准备
在开始之前,请确保你的电脑已经安装以下工具:
安装 Node.js
Hexo 基于 Node.js 运行,需要先安装 Node.js 环境.
1 2 3 4 5 6
| brew install node
node -v npm -v
|
1 2 3 4 5
| winget install OpenJS.NodeJS.LTS
|
1 2 3 4 5 6 7 8 9 10
| sudo apt update sudo apt install nodejs npm
sudo yum install nodejs npm
node -v npm -v
|
推荐 Node.js 版本 >= 16.x,npm 版本 >= 8.x 以确保兼容性
安装 Git
Git 用于版本管理和代码部署.
1 2 3 4 5
| brew install git
git --version
|
1 2 3 4 5
| winget install Git.Git
|
1 2 3 4 5 6 7 8
| sudo apt install git
sudo yum install git
git --version
|
配置 Git (首次使用)
1 2 3 4 5 6
| git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
git config --list
|
Git 官方文档
安装 Hexo 框架
全局安装 Hexo-CLI
1 2 3 4 5
| npm install -g hexo-cli
hexo version
|
创建博客项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| hexo init hexo-blog
cd hexo-blog
npm install
npm install hexo-deployer-git --save
npm install hexo-renderer-pug hexo-renderer-stylus --save
hexo server
hexo s
|
访问 http://localhost:4000 即可查看默认的 Hexo 博客.
📁 项目目录结构
1 2 3 4 5 6 7 8 9 10 11
| hexo-blog/ ├── _config.yml # Hexo 主配置文件 ├── _config.butterfly.yml # Butterfly 主题配置文件(需手动创建) ├── package.json # 依赖管理 ├── source/ # 源文件目录 │ ├── _posts/ # Markdown 文章存放处 │ └── img/ # 图片资源 ├── themes/ # 主题目录 │ └── butterfly/ # Butterfly 主题 ├── public/ # 生成的静态文件(执行 hexo g 后生成) └── node_modules/ # npm 依赖
|
Hexo 安装文档
创建 GitHub Pages 仓库
创建仓库
- 访问 GitHub 并登录
- 点击右上角
+ → New repository
- 设置仓库信息:
| 配置项 |
值 |
说明 |
| Repository name |
${用户名}.github.io |
必须使用此格式 |
| Description |
Personal Blog |
可选 |
| Public/Private |
Public |
Public 才能使用免费 GitHub Pages |
| Initialize |
添加 README/ .gitignore |
可选 |
- 点击
Create repository
配置分支
GitHub Pages 默认支持以下分支:
| 分支名 |
目录 |
说明 |
main |
/ (root) |
推荐使用 |
gh-pages |
/ (root) |
传统方式 |
main |
/docs |
文档项目使用 |
GitHub Pages 官方文档
配置 Hexo 框架
编辑根目录下的 _config.yml 文件:
⚙️ 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
|
title: My Blog subtitle: 技术成长记录 description: 分享技术心得 keywords: Hexo, Butterfly author: Your Name language: zh-CN timezone: Asia/Shanghai
url: https://${用户名}.github.io permalink: :year/:month/:day/:title/ permalink_defaults:
source_dir: source public_dir: public
default_layout: post titlecase: false external_link: enable: true field: site exclude: ''
filename_case: 0 render_drafts: false post_asset_folder: false relative_link: false future: true syntax_highlighter: highlight.js highlight: line_number: true auto_detect: false tab_replace: ''
default_category: uncategorized category_map: tag_map:
date_format: YYYY-MM-DD time_format: HH:mm:ss
per_page: 10 pagination_dir: page
theme: butterfly
deploy: type: git repo: https://github.com/${用户名}/${用户名}.github.io.git branch: main message: Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }}
|
配置文件使用 YAML 格式,注意冒号后要有空格,缩进使用空格而非 Tab
安装配置 Butterfly 主题
Butterfly 是一款美观、功能丰富的 Hexo 主题,支持响应式设计、暗黑模式、代码高亮、多种评论系统等.
Butterfly 主题文档
安装主题
1 2
| git clone https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly
|
1
| npm install hexo-theme-butterfly
|
启用主题
编辑 _config.yml,修改主题设置:
创建主题配置文件
复制主题默认配置到博客根目录:
1
| cp themes/butterfly/_config.yml _config.butterfly.yml
|
后续修改主题配置都在 _config.butterfly.yml 中进行,主题更新时不会覆盖此文件
基础配置示例
🎨 Butterfly 主题配置
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
|
favicon: /img/favicon.png
avatar: img: /img/avatar.png effect: false
index_img:
nav: logo: display_title: true fixed: false
menu: 首页: / || fas fa-home 归档: /archives/ || fas fa-archive 标签: /tags/ || fas fa-tags 分类: /categories/ || fas fa-folder-open 关于: /about/ || fas fa-user
code_blocks: theme: darker macStyle: true height_limit: 300 word_wrap: false copy: true language: true shrink: false fullpage: true
social: fab fa-github: https://github.com/${用户名} || Github fas fa-envelope: mailto:your.email@example.com || Email
footer: owner: enable: true since: 2024 custom_text: Hi, welcome to my blog!
comments: use: Waline text: true lazyload: false count: true
|
主题更新
1 2
| cd themes/butterfly git pull
|
如使用 npm 安装,执行 npm update hexo-theme-butterfly 更新
Hexo 常用命令
核心命令
📝 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
|
hexo new "${文章标题}"
hexo new draft "${草稿标题}"
hexo clean hexo generate hexo server
hexo clean && hexo g && hexo s
hexo clean hexo generate hexo deploy
hexo clean && hexo g -d
hexo list hexo version hexo --help
|
命令对比
| 命令 |
简写 |
说明 |
使用场景 |
hexo generate |
hexo g |
生成静态文件到 public/ |
每次修改后执行 |
hexo server |
hexo s |
启动本地服务器 |
本地预览 |
hexo deploy |
hexo d |
部署到配置的 Git 仓库 |
发布文章 |
hexo clean |
无 |
删除缓存和静态文件 |
遇到奇怪问题时 |
推荐工作流:
- 写文章 →
hexo new "文章标题"
- 编辑 Markdown →
source/_posts/文章标题.md
- 本地预览 →
hexo clean && hexo s
- 部署发布 →
hexo clean && hexo g -d
Hexo 命令文档
自动部署: GitHub Actions
为什么使用 GitHub Actions
| 对比项 |
hexo deploy |
GitHub Actions |
| 部署位置 |
public/ → 仓库 A |
源码仓库 B → Pages 仓库 A |
| 源码备份 |
需手动备份 |
自动备份 |
| 多机器协作 |
困难 |
简单 |
| 自动化程度 |
手动执行 |
自动触发 |
推荐方案: 使用两个仓库
${用户名}.github.io - 静态博客(只读)
hexo-blog-source - 源码仓库(主仓库)
配置 GitHub Actions
1. 创建源码仓库
创建 hexo-blog-source 仓库,推送当前项目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| git init
cat > .gitignore << EOF node_modules/ public/ .deploy_git/ db.json *.log .DS_Store Thumbs.db EOF
git add . git commit -m "Initial commit"
git remote add origin https://github.com/${用户名}/hexo-blog-source.git
git branch -M main git push -u origin main
|
2. 创建 Actions 工作流
创建 .github/workflows/deploy.yml:
🚀 GitHub Actions 配置
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
| name: Deploy to GitHub Pages
on: push: branches: - main workflow_dispatch:
jobs: build-and-deploy: runs-on: ubuntu-latest
steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0
- name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20' cache: 'npm'
- name: Install dependencies run: | npm ci # 比 npm install 更快更可靠 npm install -g hexo-cli
- name: Generate static files run: | hexo clean hexo generate
- name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./public publish_branch: main
|
3. 配置 GitHub Pages
在 ${用户名}.github.io 仓库中:
- 进入
Settings → Pages
- Source 选择
GitHub Actions
4. 配置 Secrets(跨仓库部署)
如果源码和 Pages 在不同仓库,需要配置部署密钥:
1 2 3 4 5 6 7 8 9 10 11
| ssh-keygen -t rsa -b 4096 -C "${用户名}@github.com" -f github-pages-deploy
|
修改 .github/workflows/deploy.yml:
1 2 3 4 5 6 7
| - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: deploy_key: ${{ secrets.DEPLOY_PRIVATE_KEY }} external_repository: ${用户名}/${用户名}.github.io publish_dir: ./public publish_branch: main
|
GitHub Actions 工作流程
flowchart LR
A[推送文章到源码仓库] --> B{GitHub Actions}
B --> C[安装依赖]
C --> D[生成静态文件]
D --> E[部署到 Pages 仓库]
E --> F[自动发布博客]
GitHub Actions 官方文档
peaceiris/actions-gh-pages
Butterfly 主题进阶配置
自定义头像与图标
1. 创建资源目录
2. 放置资源文件
1 2 3 4
| source/img/ ├── avatar.png # 头像 ├── favicon.png # 网站图标 └── logo.png # Logo
|
3. 配置路径
编辑 _config.butterfly.yml:
1 2 3 4 5 6
| favicon: /img/favicon.png
avatar: img: /img/avatar.png effect: false
|
推荐头像尺寸: 200x200px,favicon: 32x32px
图片管理方案
方案对比
| 方案 |
优点 |
缺点 |
推荐度 |
本地存储 source/img/ |
简单直接 |
部署慢、仓库大 |
⭐⭐ |
| 图床 CDN |
加载快、省空间 |
需额外配置 |
⭐⭐⭐⭐⭐ |
| GitHub CDN |
免费、稳定 |
国内访问慢 |
⭐⭐⭐⭐ |
推荐方案: jsDelivr + GitHub
1 2 3 4 5
| <!-- 图片链接格式 --> https://cdn.jsdelivr.net/gh/${用户名}/${仓库名}@${分支名}/${图片路径}
<!-- 示例 --> 
|
本地图片过多会导致 hexo g 生成时间过长,强烈建议使用图床
配置自定义域名
1. 添加 DNS 解析
在域名服务商处添加记录:
| 类型 |
主机记录 |
记录值 |
TTL |
| CNAME |
www |
${用户名}.github.io |
600 |
| CNAME |
@ |
${用户名}.github.io |
600 |
🌐 使用 Cloudflare 配置
- 添加站点 → 输入域名
- 添加 DNS 记录 → CNAME → ${用户名}.github.io
- 等待 DNS 生效(通常 10-30 分钟)
2. 配置 CNAME 文件
1
| echo "yourdomain.com" > source/CNAME
|
CNAME 文件会随 hexo g 生成到 public/ 根目录,GitHub Pages 会自动识别
3. 在 GitHub 仓库配置
在 ${用户名}.github.io 仓库:
Settings → Pages
- Custom domain → 输入域名
- 勾选
Enforce HTTPS
GitHub Pages 自定义域名文档
评论系统配置
Butterfly 支持多种评论系统,推荐使用 Waline.
Waline 配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| comments: use: Waline text: true lazyload: false count: true
waline: serverURL: https://${你的Waline域名}.vercel.app/ lang: zh-CN emoji: ['https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/bilibili'] requiredMeta: ['nick', 'mail'] login: enable
|
🚀 Waline 部署到 Vercel
Waline 官方文档
搜索功能
Algolia 搜索(推荐)
1 2 3 4 5 6 7 8 9
| algolia_search: enable: true hits: per_page: 10 labels: input_placeholder: 搜索文章 hits_empty: 没有找到相关文章 hits_stats: 找到 ${hits} 篇文章
|
配置步骤:
- 注册 Algolia
- 创建 Index →
hexo-blog
- 生成 API Key
- 安装插件:
1
| npm install hexo-algolia --save
|
- 配置
_config.yml:
1 2 3 4
| algolia: applicationID: ${你的ApplicationID} apiKey: ${你的Admin API Key} indexName: hexo-blog
|
- 索引文章:
Algolia DocSearch
最佳实践
文章写作规范
Front Matter 模板
1 2 3 4 5 6 7 8 9 10 11 12 13
| --- title: 文章标题 date: 2026-01-06 14:00:00 tags: - Hexo - GitHub categories: - 技术教程 cover: https://example.com/cover.jpg # 封面图 top_img: https://example.com/top.jpg # 顶部图(可选) toc: true # 显示目录 password: 123456 # 文章加密(可选) ---
|
| 字段 |
说明 |
示例 |
title |
文章标题 |
必填 |
date |
发布日期 |
自动生成 |
tags |
标签列表 |
数组格式 |
categories |
分类 |
支持层级 |
cover |
封面图 |
URL |
toc |
目录 |
true/false |
password |
访问密码 |
加密文章 |
内容规范
- 使用 Markdown 语法
- 代码块指定语言: ```javascript
- 图片使用图床链接
- 文件名使用英文或拼音
- 标题层级不超过 4 级
性能优化
1. 启用压缩
安装插件:
1 2
| npm install hexo-offline --save npm install hexo-filter-nofollow --save
|
配置 _config.yml:
1 2 3 4 5
| nofollow: enable: true exclude: - '排除的域名'
|
2. 图片优化
- 使用 WebP 格式
- 压缩图片(tinypng.com)
- CDN 加速
- 懒加载(Butterfly 已内置)
3. 缓存配置
1 2 3 4
| marked: gfm: true breaks: false
|
SEO 优化
1. 站点地图
1 2
| npm install hexo-generator-sitemap --save npm install hexo-generator-baidu-sitemap --save
|
1 2 3 4 5 6 7 8 9
| sitemap: path: sitemap.xml rel: false tags: true categories: true
baidusitemap: path: baidusitemap.xml
|
2. robots.txt
创建 source/robots.txt:
1 2 3 4 5 6 7
| User-agent: * Allow: / Allow: /archives/ Disallow: /js/ Disallow: /css/
Sitemap: https://你的域名/sitemap.xml
|
3. 提交搜索引擎
- Google Search Console
- Bing Webmaster Tools
- 百度搜索资源平台
SEO 提交流程
flowchart TD
A[生成 sitemap.xml] --> B[验证网站所有权]
B --> C[提交 sitemap]
C --> D[等待爬虫抓取]
D --> E[文章收录]
备份策略
方案一: 单仓库方案
所有代码和源文件在一个仓库,使用分支管理:
| 分支 |
用途 |
main |
静态博客 |
source |
源码备份 |
方案二: 双仓库方案(推荐)
| 仓库 |
内容 |
分支 |
${用户名}.github.io |
静态博客 |
main |
hexo-blog-source |
源码 |
main |
常见问题
Q1: hexo g 生成失败
症状: 执行 hexo generate 报错
解决方案:
1 2 3 4 5 6 7 8 9 10 11
| hexo clean
rm -rf node_modules
npm install
hexo generate
|
Q2: 部署后样式错乱
症状: 本地正常,部署后样式丢失
解决方案:
- 检查
_config.yml 中的 url 配置
- 检查主题是否正确安装
- 清除浏览器缓存
- 检查 CDN 资源是否可访问
Q3: 图片无法显示
症状: 文章中图片显示为裂图
解决方案:
| 原因 |
解决方案 |
| 路径错误 |
使用 /img/ 或完整 URL |
| 大小写问题 |
Linux 区分大小写,检查文件名 |
| 图床失效 |
更换图床链接 |
| CDN 缓存 |
添加版本号 ?v=1.0 |
Q4: GitHub Actions 部署失败
症状: Actions 工作流执行失败
解决方案:
- 检查
.github/workflows/deploy.yml 语法
- 查看 Actions 运行日志
- 验证 GitHub Token 权限
- 检查 Node.js 版本兼容性
1 2 3 4 5
| - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20'
|
Q5: 评论系统不显示
症状: 评论框不显示
解决方案:
- 检查
_config.butterfly.yml 中评论系统配置
- 验证服务地址是否正确
- 检查浏览器控制台错误信息
- 确认评论服务是否正常运行
Q6: 搜索功能不工作
症状: 搜索框无结果
解决方案:
Q7: 如何迁移博客
从其他平台迁移到 Hexo:
1 2 3 4 5 6 7 8 9
|
hexo clean && hexo g -d
|
Q8: 如何备份到新电脑
1 2 3 4 5 6 7 8 9 10 11 12
| git clone https://github.com/${用户名}/hexo-blog-source.git cd hexo-blog-source
npm install
npm install -g hexo-cli
hexo clean && hexo s
|
进阶主题
多环境部署
同时部署到多个平台:
1 2 3 4 5 6 7 8
| deploy: - type: git repo: https://github.com/${用户名}/${用户名}.github.io.git branch: main - type: git repo: git@gitee.com:${用户名}/${用户名}.git branch: master
|
| 平台 |
说明 |
速度 |
| GitHub Pages |
全球通用 |
国内一般 |
| Gitee Pages |
国内访问快 |
需实名认证 |
| Vercel |
CDN 加速 |
快 |
| Netlify |
预览功能 |
快 |
自定义页面
创建独立页面:
1 2 3 4 5 6 7 8
| hexo new page about
hexo new page categories
hexo new page tags
|
编辑页面内容:
1 2 3 4 5 6 7 8 9
| --- title: 关于 date: 2026-01-06 14:00:00 type: about ---
# 关于我
这里是关于页面内容
|
数据文件
使用外部数据源:
1 2 3 4
| Home: / || fas fa-home Archives: /archives/ || fas fa-archive Tags: /tags/ || fas fa-tags
|
配置主题使用:
插件开发
自定义 Hexo 插件:
1 2 3 4 5 6 7 8
| hexo.extend.helper.register('my_helper', function(){ return '<p>My Custom Helper</p>'; });
hexo.extend.tag.register('my_tag', function(args, content){ return '<div class="my-tag">' + content + '</div>'; }, {ends: true});
|
在模板中使用:
1 2 3 4 5
| <%- my_helper() %>
<% my_tag %> 内容 <% endmy_tag %>
|
Hexo 插件开发文档
总结
本文档详细介绍了 Hexo 博客的搭建流程,从环境准备到自动部署,再到主题定制和性能优化. 核心要点:
- 环境准备: Node.js + Git 是基础
- Hexo 安装:
hexo init + npm install
- 主题配置: Butterfly 推荐用于美观博客
- 自动部署: GitHub Actions 实现持续集成
- 内容管理: Markdown + Front Matter 规范
- 性能优化: 图片 CDN + 缓存 + 压缩
- SEO 优化: 站点地图 + robots.txt
快速参考卡片
⚡ 快速命令参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| hexo init blog && cd blog && npm install
hexo new "标题"
hexo clean && hexo s
hexo clean && hexo g -d
cd themes/butterfly && git pull
|
推荐文章结构
1 2 3 4 5 6
| source/_posts/ ├── 2026-01-06-hexo-tutorial.md ├── 2026-01-07-javascript-guide.md └── img/ ├── cover.jpg └── screenshot.png
|
Hexo 官方文档
Butterfly 主题文档
GitHub Pages 文档