🚀 Hexo 静态博客搭建与部署教程 (SSH 密钥方式)
优势:免密码推送、高安全性、自动化部署
📋 前置条件
- 安装 Node.js (v14+)
- 安装 Git
- 已有 GitHub 账号
🔑 第一步:生成 SSH 密钥
- 打开终端(Windows: Git Bash / macOS: Terminal)
- 生成密钥对(替换邮箱):
1
| ssh-keygen -t ed25519 -C "your_email@example.com"
|
- 按回车使用默认路径 (
~/.ssh/)
- 设置密钥密码(可选,增强安全性)
- 查看公钥:
1
| cat ~/.ssh/id_ed25519.pub
|
复制输出的内容(以 ssh-ed25519 开头)
🌐 第二步:添加 SSH 密钥到 GitHub
- 访问 GitHub → Settings → SSH and GPG keys
- 点击 New SSH Key
- Title: 自定义名称(如
My Hexo Deploy Key)
- Key type: Authentication Key
- Key: 粘贴复制的公钥内容
- 点击 Add SSH Key
️ 第三步:安装并配置 Hexo
- 安装 Hexo CLI:
- 初始化博客项目:
1 2 3
| hexo init my-blog cd my-blog npm install
|
- 安装 Git 部署插件:
1
| npm install hexo-deployer-git --save
|
🔧 第四步:配置 Hexo 部署设置
- 编辑博客目录下的
_config.yml 文件(末尾添加):
1 2 3 4 5
| deploy: type: git repo: git@github.com:yourusername/yourusername.github.io.git branch: main
|
- 替换信息:
yourusername: 你的 GitHub 用户名
branch: 若使用 GitHub Pages,通常为 main 或 gh-pages
🚀 第五步:本地测试与部署
- 启动本地服务器预览:
1
| hexo clean && hexo server
|
访问 http://localhost:4000 查看效果
- 部署到 GitHub Pages:
1
| hexo clean && hexo deploy --generate
|
🌐 第六步:访问线上博客
1
| https://yourusername.github.io
|
(首次部署需等待 1-2 分钟生效)
🔄 日常更新流程
写新文章:
https://test3.gumengyo.top/2025/10/29/Publish%20articles%20on%20hexo%20and%20Github%20pages/
🛠️ 故障排除
- SSH 连接失败:
1 2
| eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519
|
- 部署权限错误:
- 确认 GitHub 仓库名格式:
username.github.io
- 检查 SSH Key 是否绑定正确账户
- 页面未更新:
- 强制刷新浏览器
Ctrl + Shift + R
- 等待 GitHub Pages 构建完成(仓库 → Settings → Pages → 查看构建状态)
💡 进阶配置
- 自定义域名: 在博客
source/ 目录创建 CNAME 文件(无后缀),写入域名 并在 DNS 服务商添加 CNAME 记录指向 username.github.io
- 更换主题: 修改
_config.yml 中的 theme 字段(如 theme: fluid)