🚀 Hexo 静态博客搭建与部署教程

Gumeng Lv1

🚀 Hexo 静态博客搭建与部署教程 (SSH 密钥方式)

优势:免密码推送、高安全性、自动化部署

📋 前置条件

  1. 安装 Node.js (v14+)
  2. 安装 Git
  3. 已有 GitHub 账号

🔑 第一步:生成 SSH 密钥

  1. 打开终端(Windows: Git Bash / macOS: Terminal)
  2. 生成密钥对(替换邮箱):
1
ssh-keygen -t ed25519 -C "your_email@example.com"
  • 按回车使用默认路径 (~/.ssh/)
  • 设置密钥密码(可选,增强安全性)
  1. 查看公钥
1
cat ~/.ssh/id_ed25519.pub

复制输出的内容(以 ssh-ed25519 开头)

🌐 第二步:添加 SSH 密钥到 GitHub

  1. 访问 GitHub → Settings → SSH and GPG keys
  2. 点击 New SSH Key
    • Title: 自定义名称(如 My Hexo Deploy Key
    • Key type: Authentication Key
    • Key: 粘贴复制的公钥内容
  3. 点击 Add SSH Key

️ 第三步:安装并配置 Hexo

  1. 安装 Hexo CLI:
1
npm install -g hexo-cli
  1. 初始化博客项目:
1
2
3
hexo init my-blog
cd my-blog
npm install
  1. 安装 Git 部署插件:
1
npm install hexo-deployer-git --save

🔧 第四步:配置 Hexo 部署设置

  1. 编辑博客目录下的 _config.yml 文件(末尾添加):
1
2
3
4
5
# Deployment
deploy:
type: git
repo: git@github.com:yourusername/yourusername.github.io.git
branch: main
  • 替换信息
    • yourusername: 你的 GitHub 用户名
    • branch: 若使用 GitHub Pages,通常为 maingh-pages

🚀 第五步:本地测试与部署

  1. 启动本地服务器预览:
1
hexo clean && hexo server

访问 http://localhost:4000 查看效果

  1. 部署到 GitHub Pages
1
hexo clean && hexo deploy --generate
  • 首次部署需验证 SSH 连接:
1
ssh -T git@github.com  # 提示 "Hi username!" 即成功

🌐 第六步:访问线上博客

  • 打开浏览器访问:
1
https://yourusername.github.io

(首次部署需等待 1-2 分钟生效)

🔄 日常更新流程

  1. 写新文章:

    https://test3.gumengyo.top/2025/10/29/Publish%20articles%20on%20hexo%20and%20Github%20pages/

🛠️ 故障排除

  1. SSH 连接失败
1
2
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519 # 添加私钥到代理
  1. 部署权限错误
    • 确认 GitHub 仓库名格式:username.github.io
    • 检查 SSH Key 是否绑定正确账户
  2. 页面未更新
    • 强制刷新浏览器 Ctrl + Shift + R
    • 等待 GitHub Pages 构建完成(仓库 → Settings → Pages → 查看构建状态)

💡 进阶配置

  • 自定义域名: 在博客 source/ 目录创建 CNAME 文件(无后缀),写入域名 并在 DNS 服务商添加 CNAME 记录指向 username.github.io
  • 更换主题: 修改 _config.yml 中的 theme 字段(如 theme: fluid
  • 标题: 🚀 Hexo 静态博客搭建与部署教程
  • 作者: Gumeng
  • 创建于 : 2025-11-06 03:10:26
  • 更新于 : 2025-11-06 03:10:29
  • 链接: https://test3.gumengyo.top/2025/11/06/GithubPage搭建Hexo博客/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论