Hexo博客部署到GitHub Pages的说明
要将Hexo博客部署到GitHub Pages,可以按照以下步骤操作:
1. 准备工作
- 安装 Hexo(如果尚未安装):
1
npm install -g hexo-cli
- 创建 Hexo 项目(如果还没有):
1
2
3hexo init my-blog
cd my-blog
npm install - 确保 Git 已安装:如果没有,请先安装 Git。
1
git --version
2. 创建 GitHub 仓库
在 GitHub 上新建仓库:
- 仓库名格式必须为:
<你的用户名>.github.io
(例如yourusername.github.io
)。 - 如果是项目站点(非个人主页),可以任意命名,但需在
_config.yml
中额外配置。
- 仓库名格式必须为:
初始化本地 Git 并关联远程仓库:
1
2git init
git remote add origin https://github.com/yourusername/yourusername.github.io.git
3. 安装 Hexo 部署插件
1 |
|
4. 配置 _config.yml
在 Hexo 根目录下的 _config.yml
文件末尾找到 deploy
部分,修改为:
1 |
|
注意:
- 如果使用 SSH 方式,
repo
可写为git@github.com:yourusername/yourusername.github.io.git
。- 确保缩进正确(2 个空格)。
5. 生成静态文件并部署
生成静态文件:
1
hexo clean && hexo generate
(或简写
hexo clean && hexo g
)部署到 GitHub:
1
hexo deploy
(或简写
hexo d
)访问博客:
稍等几分钟后,打开https://yourusername.github.io
即可查看。
6. 可选:自定义域名
- 在仓库的
Settings > Pages
里设置自定义域名(如blog.yourdomain.com
)。 - 在 Hexo 的
source
目录下创建CNAME
文件(无后缀):1
blog.yourdomain.com
- 重新部署:
1
hexo clean && hexo g -d
7. 管理博客源码(推荐)
为了避免丢失本地文件,建议将 Hexo 源码也推送到 GitHub:
- 新建一个分支(如
hexo-source
):1
git checkout -b hexo-source
- 提交代码:
1
2
3git add .
git commit -m "初始化 Hexo 源码"
git push origin hexo-source - 以后更新博客时,先提交源码,再部署:
1
2
3
4git add .
git commit -m "更新文章"
git push origin hexo-source
hexo clean && hexo g -d
常见问题
1. 部署后页面 404?
- 检查仓库名是否正确(必须是
<username>.github.io
)。 - 确保
branch
是main
或master
(与 GitHub 默认分支一致)。
2. 部署失败(权限错误)?
- 如果使用 SSH,确保已添加公钥到 GitHub。
- 如果使用 HTTPS,可能需要输入 GitHub 账号密码(推荐改用 SSH 或 Personal Access Token)。
3. 如何更新博客?
- 修改文章后,重新执行:
1
hexo clean && hexo g -d
Hexo博客部署到GitHub Pages的说明
hhttps://www.fengyu-blog.top/2025/05/10/Hexo博客部署到GitHub Pages的说明/