参考自 博客园 - chenlove,经整理改写为学习清单。

环境准备清单

按以下顺序安装,缺一不可:

1. 安装 Git

2. 安装 Node.js

3. 安装 Hexo

1
npm install hexo -g

验证:hexo -v

4. 安装部署依赖

1
npm install --save hexo-deployer-git

SSH Key 配置

配置 SSH Key 后可以免密推送代码到 GitHub。

生成密钥:

1
ssh-keygen -t rsa -C "你的GitHub邮箱"

连续回车三次,密钥保存在 ~/.ssh/

添加到 GitHub:

  1. 复制 ~/.ssh/id_rsa.pub 全部内容
  2. GitHub → Settings → SSH and GPG keys → New SSH key
  3. 粘贴公钥,随意命名

验证连接:

1
ssh -T git@github.com

配置用户信息:

1
2
git config --global user.name "你的GitHub用户名"
git config --global user.email "你的GitHub邮箱"

搭建博客

Hexo 常用命令

命令 缩写 说明
hexo new "文章名" hexo n 新建文章
hexo new page "页面名" 新建页面
hexo generate hexo g 生成静态页面
hexo server hexo s 本地预览(端口 4000)
hexo deploy hexo d 部署到 GitHub
hexo clean 清理缓存和 public

初始化博客

1
hexo init

目录下会生成 sourcethemes_config.yml 等核心文件。

本地预览

1
2
hexo g
hexo s

浏览器访问 http://localhost:4000/

常见报错:line.mathALL is not function —— Node.js 版本低于 12,升级即可。

部署到 GitHub

1. 创建仓库

仓库名格式:你的用户名.github.io

我的仓库是 16-SIXTEEN-16.github.io,访问地址:https://16-sixteen-16-github-io.pages.dev/

2. 配置 _config.yml

1
2
3
4
deploy:
type: git
repository: git@github.com:你的用户名/你的用户名.github.io.git
branch: main

3. 部署

1
hexo d

部署完成后访问博客地址即可看到效果。

编写与发布文章

新建文章

1
hexo new "文章标题"

source/_posts/ 下编辑生成的 .md 文件。

发布流程

1
hexo g && hexo d

在正文中插入 <!--more--> 可实现摘要截断,首页只显示标记前的内容。

小结

从零搭建 Hexo + GitHub 个人博客的完整流程如上。最易踩坑的是 SSH Key 配置,按步骤来即可。日常写作就是三个动作:hexo new → 编辑 .mdhexo g && hexo d

我的博客:https://16-sixteen-16-github-io.pages.dev/