使用 Hexo 和 GitHub Page 搭建博客

安装 Hexo(依赖 Node.js)

全局安装 Hexo CLI:

1
npm install -g hexo-cli

创建博客文件夹:

1
2
3
hexo init blog
cd blog
npm install

配置 Hexo

修改 _config.yml 文件中的一些基础配置:

1
2
3
4
5
6
7
8
9
10
# _config.yml

# Site
title: your Blog
subtitle: ""
description: ""
keywords:
author: your name
language: zh
timezone: "Asia/Shanghai"

安装部署工具

1
npm install hexo-deployer-git --save

_config.yml 的末尾添加:

1
2
3
4
deploy:
type: git
repo: git@github.com:username/username.github.io.git
branch: main

配置 SSH 密钥

生成新的 SSH 密钥

1
ssh-keygen -t rsa -b 4096 -C "你的邮箱@example.com"
  • 密钥默认路径:按回车键即可,密钥会保存为 ~/.ssh/id_rsa 和 ~/.ssh/id_rsa.pub
  • 密码:如果你希望加密 SSH 密钥,可以输入密码,否则直接回车

创建 GitHub 仓库

在 GitHub 上创建一个仓库,仓库名为 username.github.io,其中 username 是你的 GitHub 用户名

将密钥添加到 GitHub

1
cat ~/.ssh/id_rsa.pub
  1. 复制输出的整个字符串
  2. 上 GitHub 找到 New SSH key. Title 随便填写,Key 粘贴刚才复制的公钥内容,点击 Add SSH key

测试 ssh 连接

1
ssh -T git@github.com

如果出现 Hi username! You've successfully authenticated, but GitHub does not provide shell access. 则说明连接成功

创建新文章

1
hexo new post "文章标题"

编辑生成的 source/_posts/文章标题.md 文件

hexo 常用命令

1
2
3
4
hexo clean # 清除缓存
hexo generate # 生成静态文件
hexo deploy # 部署到远程仓库
hexo server # 启动本地服务器