在Mac上搭建个人博客:Hexo + NexT + 自定义域名搭建永久有效的个人博客网站

本文记录在 Mac 上搭建和维护 Hexo + NexT 博客的过程,涵盖 GitHub Pages、自定义域名与 DNS,并补充 HanLior 网站当前的项目目录、主题、文章阅读和页脚统计配置。

配置Github项目

在Github右上角点击Repository

然后去New一个

image-20261016175738215

按照下面的输入配置,名字输入自己想要起的名字就可以

image-20261016175714562

1
2
3
4
5
6
7
8
9
生成密钥
ssh-keygen -t rsa -b 4096
然后一路回车,可以看到生成到了/Users/cr1sp/.ssh/id_rsa 这里Cr1sp是我电脑的用户名
用编辑器打开,去复制id_rsa.pub到github:
https://github.com/settings/ssh
改变密钥认证方式:
ssh-keyscan -t ed25519 github.com >> ~/.ssh/known_hosts
测试是否成功:
ssh -T git@github.com

如果出现这样的字就是代表可以链接成功

image-20261016181146553

设置账号信息:

1
git config --global user.name "HanLior"

设置github邮箱:

1
git config --global user.email "你的 GitHub 邮箱"

下载Git

这里我用的homebrew下载的git

1.先安装homebrew

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

2.通过Homebrew安装Git

1
brew install git

3.验证是否安装成功

image-20261016180454015

出现这样的就是安装成功

安装node.js

1.下载node.js

1
brew install node

2.验证是否下载成功

1
node --version

image-20261016181508935

搭建Hexo

1.切换阿里云镜像源

1
npm config set registry https://registry.npmmirror.com

2.安装cnpm

1
npm install -g cnpm --registry=https://registry.npmmirror.com

3.用 cnpm 安装 hexo

1
cnpm install -g hexo-cli

4.验证是否安装成功

image-20261016181759230

本地创建Hexo博客文件

先在本地目录里面创建一个文件夹

1
2
3
mkdir blog
cd blog
hexo init

然后看到blog目录文件里面有这些东西就可以

image-20261016182022391

1
2
3
4
5
6
node_modules: 依赖包
public:存放生成的页面
scaffolds:生成文章的一些模板
source:用来存放你的文章
themes:放下下载的主题
_config.yml: 博客的核心配置文件(设置主体、标题等属性)

启动cnpm install

image-20261016182214771

启动hexo hexo s -g

image-20261016182256053

访问127.0.0.1:4000

image-20261016182322636

配置域名解析,可以参考这篇文章

https://www.duheweb.com/post/20210417143744.html

然后去github配置一下域名,启用https

image-20261016192203746

本站实际配置记录

以下配置对应本博客当前使用的 GitHub 仓库和域名:

  • GitHub 用户:HanLior
  • Pages 仓库:HanLior/HanLior.github.io
  • 域名:www.hanlior.top
  • Hexo 项目目录:/Users/cr1sp/blog
  • Hexo:8.1.2;NexT:8.21.1,Pisces 风格

目录和发布链路

博客源文件放在 /Users/cr1sp/blog。source/_posts 保存文章,source/categories、source/tags 和 source/about 保存导航页面;NexT 配置放在 _config.next.yml。文章图片托管在单独的图床仓库 xhcola/typora_drawing_bed,正文使用 raw.githubusercontent.com 图片链接,不把图片文件混进 Hexo 文章目录。

发布时,Hexo 先把源文件生成到 public,再由 hexo-deployer-git 把生成结果推送到 Pages 仓库 HanLior/HanLior.github.io 的 main 分支。这个 Pages 仓库用于托管生成后的网站。

配置 DNS

在域名服务商的 DNS 解析页面添加以下记录。根域名用 4 条 A 记录,www 用 CNAME;CNAME 的记录值不要带 https:// 或末尾路径。

主机记录 类型 记录值
@ A 185.199.108.153
@ A 185.199.109.153
@ A 185.199.110.153
@ A 185.199.111.153
www CNAME HanLior.github.io

在 GitHub 仓库 HanLior/HanLior.github.io 的 Settings → Pages 中,发布来源选择 Deploy from a branch,分支选择 main、目录选择 /(root)。Custom domain 填 www.hanlior.top。等 DNS 检查和 TLS 证书签发完成后,再启用 Enforce HTTPS。

在 Hexo 的 source/CNAME 文件中只写自定义域名:

1
www.hanlior.top

不要把 CNAME 文件放在 source/_posts 里,也不要把它命名为 CNAME.txt。

配置 Hexo 部署到 GitHub Pages

在博客根目录 /Users/cr1sp/blog 安装部署插件:

1
npm install hexo-deployer-git --save

在根目录的 _config.yml 配置仓库和分支:

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

每次发布前先清理并重新生成,再部署:

1
2
3
npx hexo clean
npx hexo generate
npx hexo deploy

也可以依次使用 hexo clean、hexo generate、hexo deploy。第一次在本机部署时需要已经配置 GitHub SSH Key,并确认 ssh -T git@github.com 可以连接。

安装并启用 NexT 主题

本站使用 NexT 8.21.1,主题通过 npm 安装并由 package.json 管理:

1
npm install hexo-theme-next@8.21.1 --save

Hexo 根目录 _config.yml 中设置:

1
theme: next

主题设置写在博客根目录的 _config.next.yml,不是 themes/next/_config.yml。本站使用 Pisces、深色模式、左侧文章侧栏、中文导航和 GitHub 个人链接:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
scheme: Pisces
darkmode: true

sidebar:
position: left
display: post

menu:
home: / || home
categories: /categories/ || th
tags: /tags/ || tags
about: /about/ || user

social:
GitHub: https://github.com/HanLior || fab fa-github

menu 中的页面需在 source 目录下创建对应页面。本站导航显示首页、分类、标签和关于;归档页面仍可通过 /archives/ 访问,但没有放进主导航。

1
archives: /archives/ || archive

创建分类、标签和关于页面

可用 Hexo 命令生成页面:

1
2
3
npx hexo new page categories
npx hexo new page tags
npx hexo new page about

命令会创建 source/<页面名>/index.md。分类页和标签页的 front matter 分别设置 type: categories、type: tags;这些页面以及关于页均可设置 comments: false。例如:

1
2
3
4
5
---
title: 分类
type: categories
comments: false
---

文章通过 front matter 设置分类和标签。本站的分类包括“博客搭建”“逆向学习”和“web安全学习”。Hexo 默认会隐藏没有文章的分类;persistent_categories 配置和 scripts/persistent-categories.js 会让预设分类在文章发布前也能显示。文章归入该分类后,分类页会自动列出文章。

首页摘要、分页和文章信息

本站首页每页显示 10 篇文章。文章使用 <!-- more --> 分隔摘要和正文:首页只显示分隔符之前的内容,读者点击“阅读全文”进入全文。新文章可按下面的结构设置分类、标签和摘要:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
---
title: 文章标题
date: 2026-11-02 16:00:00
categories:
- web安全学习
tags:
- PHP
---

这里写首页摘要。

<!-- more -->

这里开始是文章全文。

字数和预计阅读时间由 hexo-word-counter 生成,只有文章级统计开启,不显示全站总字数或总阅读时间:

1
npm install hexo-word-counter --save

Hexo 根目录 _config.yml:

1
2
3
4
5
symbols_count_time:
symbols: true
time: true
total_symbols: false
total_time: false

主题配置 _config.next.yml 控制发表于日期、分类和字数/阅读时间布局。source/_data/post-meta.njk 额外显示文章文件的实际修改日期和时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
post_meta:
item_text: true
created_at: true
updated_at:
enable: false
another_day: true
categories: true

symbols_count_time:
separated_meta: true
item_text_total: false

custom_file_path:
postMeta: source/_data/post-meta.njk

项目的 updated_option 设为 mtime,因此文章更新信息根据源文件修改时间生成。自定义元信息模板会把更新日期和时分秒显示在文章标题下方。

本站开启的搜索和 SEO 基础功能

安装搜索索引、Atom 订阅和站点地图插件:

1
npm install hexo-generator-searchdb hexo-generator-feed hexo-generator-sitemap --save

Hexo 根目录 _config.yml 中配置搜索索引、订阅和站点地图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
search:
path: search.xml
field: post
content: true
format: striptags

feed:
type: atom
path: atom.xml
limit: 20

sitemap:
path: sitemap.xml
tags: true
categories: true

NexT 的 _config.next.yml 开启本地搜索:

1
2
3
4
5
local_search:
enable: true
top_n_per_article: 1
unescape: false
preload: false

本站在 _config.next.yml 中开启了 Mac 风格的代码块复制按钮:

1
2
3
4
5
codeblock:
copy_button:
enable: true
style: mac

页脚的访客数和浏览量写在 source/_data/footer.njk,通过兼容不蒜子接口的脚本 https://busuanzi.meuicat.com/js 显示。NexT 内置的不蒜子开关保持关闭,因为原接口曾返回 502。切换到新服务后,访客数和浏览量从新服务开始累计。相同页脚还显示博客运行时间,起点为首次部署记录的北京时间 2026-10-16 18:48:16。

根目录配置还要填写站点标题、作者、语言、时区和正式网址。本站当前值如下:

1
2
3
4
5
6
7
8
title: HanLior
subtitle: 技术学习与实践
description: HanLior 的个人博客,记录技术学习、项目实践与思考。
keywords: Hexo, NexT, HanLior
author: HanLior
language: zh-CN
timezone: Asia/Shanghai
url: https://www.hanlior.top

搜索引擎抓取规则放在 source/robots.txt:

1
2
3
User-agent: *
Allow: /
Sitemap: https://www.hanlior.top/sitemap.xml

本站开启了 NexT 深色模式、左侧侧栏、本地搜索和代码复制。网站根目录 _config.yml 还设置每页 10 篇、Atom 订阅、站点地图和 robots.txt。访问统计会在访客打开网页后逐步累计。

删除默认示例文章

Hexo 初始化时会生成 source/_posts/hello-world.md。删除该文件并重新部署后,文章和归档列表里就不会再显示 Hello World。source/index.md 是没有文章时的首页提示;有文章时,首页由文章索引生成器列出文章摘要:

1
2
3
4
5
6
7
---
title: 首页
layout: page
comments: false
---

文章正在整理中,欢迎稍后再来。

之后写新文章可运行:

1
npx hexo new post "文章标题"

完成编辑后再运行清理、生成和部署命令。