GitHub Pages部署
GitHub Pages 部署
GitHub Pages 是 GitHub 提供的免费静态网站托管服务,非常适合部署 Hexo 博客。
准备工作
- 一个 GitHub 账号
- 已安装 Git 并配置 SSH 密钥
- 已创建好 Hexo 博客项目
创建仓库
- 登录 GitHub
- 点击右上角 + → New repository
- 仓库名填写:
username.github.io(将username替换为你的 GitHub 用户名) - 选择 Public
- 点击 Create repository
安装部署插件
1 | npm install hexo-deployer-git |
配置部署
编辑 _config.yml,添加部署配置:
1 | deploy: |
将 username 替换为你的 GitHub 用户名。
部署
1 | # 清除旧文件 |
或者一步到位:
1 | hexo clean && hexo generate && hexo deploy |
验证部署
部署完成后,访问 https://username.github.io 查看博客。
⚠️ 注意:首次部署可能需要等待几分钟才能生效。
自定义域名(可选)
如果你有自己的域名:
- 在
source/目录下创建CNAME文件,内容为你的域名:
1 | blog.example.com |
- 在域名 DNS 设置中添加 CNAME 记录:
1 | blog.example.com → username.github.io |
- 在 GitHub 仓库 Settings → Pages 中勾选 Enforce HTTPS
常见问题
部署后页面 404
- 检查仓库名是否为
username.github.io - 检查分支是否为
gh-pages - 等待几分钟后刷新
样式丢失
检查 _config.yml 中的 url 和 root 配置:
1 | url: https://username.github.io |
权限问题
确保 GitHub 仓库 Settings → Actions → General 中:
- Workflow permissions 设置为 Read and write permissions
💡 提示:GitHub Pages 每月有 100GB 带宽限制,对于个人博客足够使用。