Hexo博客搭建全流程

Hexo 博客搭建全流程

环境准备 → GitHub 关联 → Hexo 初始化 → 主题安装 → 配置部署,一篇搞定!

1. 环境准备

macOS

在 macOS 上,一切从终端(Terminal)开始。

安装 Homebrew(如果还没装)

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

一键安装所有工具

1
brew install node git gh

配置镜像源(加速 npm 下载)

1
npm config set registry https://mirrors.huaweicloud.com/repository/npm/

Windows

在 Windows 上,推荐使用 PowerShell 或 Windows Terminal。

安装工具

  1. Node.js:从 Node.js 官网 下载 LTS 版本
  2. Git:从 Git 官网 下载安装
  3. GitHub CLI:从 gh 官网 下载安装

配置镜像源(加速 npm 下载)

1
npm config set registry https://mirrors.huaweicloud.com/repository/npm/

配置 Git(可选但推荐)

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

2. 关联 GitHub (网页一键登录)

这是你要求的”网页登录”核心步骤,通过 gh 工具直接在浏览器完成授权。

启动登录流程

1
gh auth login

交互式选择

1
2
3
4
What account...? -> GitHub.com
Preferred protocol...? -> HTTPS (选这个,不需要 SSH)
Authenticate Git with your GitHub credentials? -> Yes
How would you like to authenticate...? -> Login with a web browser

网页授权

终端会给出一串 8 位验证码 并自动打开浏览器。在网页中输入代码,点 Authorize,回到终端就登录成功了。


3. 初始化 Hexo 博客

选择一个你喜欢的文件夹(比如 Documents),开始搭建。

1
2
3
4
5
6
7
8
9
10
11
# 安装 Hexo 脚手架
npm install -g hexo-cli

# 创建博客文件夹
cd ~/Documents
hexo init my-blog
cd my-blog
npm install

# 本地预览(看到 "Hexo is running" 后访问 http://localhost:4000)
hexo s

4. 安装安知鱼 (Anzhiyu) 主题

根据教程 No.2 的配置,快速应用美化。

下载主题

1
2
git clone -b main https://github.com/anzhiyu-c/hexo-theme-anzhiyu.git themes/anzhiyu
npm install hexo-renderer-pug hexo-renderer-stylus --save

应用配置

修改根目录 _config.yml,设置 theme: anzhiyu

覆盖配置(macOS 命令):

1
cp -rf ./themes/anzhiyu/_config.yml ./_config.anzhiyu.yml

5. 配置部署 (Deployment)

由于我们改用了 HTTPS 网页登录,部署配置也需要调整为 HTTPS 链接。

安装插件

1
npm install hexo-deployer-git --save

修改 _config.yml 末尾

repository 改为你的 GitHub 仓库 HTTPS 地址:

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

6. 日常更新流程

以后你写博客只需要记住这三组命令:

动作 命令
新建文章 hexo new "文章标题"
本地预览 hexo cl && hexo s
部署上线 hexo cl && hexo g && hexo d

常见问题:SSH 转 HTTPS 报错

找到原因了! 你的 _config.yml 配置文件里,deploy 部分用的还是 SSH 地址git@github.com:...),而我们之前配置的是 HTTPS 网页登录

Git 在尝试通过 SSH 连接时找不到密钥,所以报错了。

解决方案

1. 修改 _config.yml (核心修复)

URL 部分:把 example.com 换成你真正的域名,否则博文里的图片和链接可能会失效。

1
2
3
# URL
url: https://yusialone.github.io
root: /

Deployment 部分(重点):把 repo 的地址从 git@ 开头换成 https:// 开头:

1
2
3
4
5
6
# Deployment
deploy:
type: git
# 替换为 HTTPS 地址
repo: https://github.com/yusialone/yusialone.github.io.git
branch: main

2. 激活 HTTPS 免密推送

由于你之前已经用 gh auth login 登录过了,现在只需要告诉 Git:”以后碰到 GitHub 的 HTTPS 链接,直接管 GitHub CLI 要授权。”

在终端执行:

1
gh auth setup-git

3. 清理并重新发布

配置改好后,执行这套”组合拳”:

1
2
3
4
5
6
7
8
# 1. 进入博客根目录
cd ~/Documents/blog/my-blog

# 2. 删除旧的部署缓存(防止旧地址残留)
rm -rf .deploy_git

# 3. 重新生成并发布
hexo clean && hexo g && hexo d

💡 为什么之前会报错?

报错原因git@github.com 是 SSH 协议,它强制要求你的电脑里有一对 id_rsa 密钥。

解决方案:我们改用 https://github.com 协议,它会通过你网页登录后的 Token(由 gh 工具管理)进行身份验证。这更符合你”网页登录”的需求,也不用再去折腾 SSH 密钥了。


额外的小建议

  • 留言板信封效果:在你的配置里看到了 envelope_comment(留言板信封效果),这个插件非常酷!
  • 图床选择:你的 custom_pic 用的是 jsDelivr。由于 jsDelivr 在国内部分地区访问不稳定,如果以后图片刷不出来,建议考虑换成自己的图床或者 unpkg.com。