Hexo博客搭建教程

返回首页

本文最后更新于 2025年12月17日 晚上

Hexo 博客搭建教程

💡 一、环境准备

1. 安装 Node.js

Hexo 依赖 Node.js 运行,请先完成安装。

前往官网下载安装完成后,打开命令提示符(CMD)验证:

1
node -v

若能返回版本号,则代表安装成功。

进行 npm 换源

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

无报错表示换源完成。


2. 安装 Git

前往官网下载 Git,安装时使用默认项。

验证安装:

1
git --version

3. 安装 Hexo

在系统中打开 Git Bash,运行:

1
npm install -g hexo-cli

验证是否安装成功:

1
hexo -v

4. GitHub 部署准备

注册 GitHub 后,创建仓库:

<用户名>.github.io

建议设为私密库(后续涉及 API Key)。

配置 Git 全局信息(Git Bash):

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

检查配置:

1
git config -l

5. 连接 GitHub(SSH)

生成 SSH 密钥:

1
ssh-keygen -t rsa -C <你的邮箱>

一路回车即可生成。

前往:

C:\Users\<你的用户名>\.ssh\id_rsa.pub
使用记事本打开,复制全部内容。

GitHub 添加密钥路径:

右上角头像 → Settings → SSH and GPG keys → New SSH key

测试连接:

1
ssh -T git@github.com

若出现连接成功提示,则环境部署完成。


💡 二、新建 Hexo 项目

在桌面或任意路径右键选择 Open Git Bash here,运行:

1
2
3
hexo init <项目名>
cd <项目名>
npm i

💡 三、配置 Hexo

推荐使用 Visual Studio Code 进行项目编辑。

运行开发环境:

1
2
3
hexo cl
hexo g
hexo s

浏览器访问:

http://localhost:4000/

按 CTRL+C 可停止运行。


💡 四、安装主题

推荐主题:Butterfly

请按照主题官方文档进行安装与配置。


💡 五、托管到 GitHub Pages

安装部署组件:

1
npm install hexo-deployer-git --save

修改 _config.yml 中的 deploy:

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

部署到 GitHub:

1
2
3
hexo cl
hexo g
hexo d

出现 Deploy done 表示成功。


💡 六、部署到 Vercel

使用 GitHub 登录 Vercel:

Add New → Project → Import GitHub 仓库 → Deploy

完成部署后,在 Project → Domains
添加自定义域名,并在域名服务商中解析即可。


💡 七、部署到 Cloudflare Pages(可选)

Cloudflare Pages 是 Cloudflare 提供的免费静态网站托管服务,具有全球 CDN 加速、自动 HTTPS、无限带宽等优势。

1. 准备工作

确保你的 Hexo 博客代码已经推送到 GitHub 仓库。

2. 连接 GitHub 仓库

  1. 访问 Cloudflare Dashboard
  2. 登录或注册 Cloudflare 账号
  3. 在左侧菜单选择 Pages
  4. 点击 Create a project
  5. 选择 Connect to Git
  6. 授权 Cloudflare 访问你的 GitHub 账号
  7. 选择你的 Hexo 博客仓库

3. 配置构建设置

Set up builds and deployments 页面配置:

Framework preset: NoneHexo

Build command:

1
npm install && hexo generate

Build output directory:

1
public

Root directory: 留空或填写 /(如果仓库根目录就是 Hexo 项目)

Environment variables (可选):

  • NODE_VERSION: 1820(指定 Node.js 版本)

4. 部署

点击 Save and Deploy,Cloudflare 会自动开始构建和部署。

首次部署可能需要几分钟时间,后续每次推送到 GitHub 的 main 分支都会自动触发部署。

5. 自定义域名

  1. 在项目页面点击 Custom domains
  2. 点击 Set up a custom domain
  3. 输入你的域名(如 example.com
  4. 按照提示在域名服务商添加 CNAME 记录:
    • 类型: CNAME
    • 名称: @www
    • 目标: Cloudflare 提供的 Pages 域名(如 xxx.pages.dev

6. 环境变量配置(可选)

如果你的 Hexo 配置中使用了环境变量,可以在 Settings → Environment variables 中添加。

7. 优势

  • 全球 CDN 加速:访问速度快
  • 免费 SSL 证书:自动 HTTPS
  • 无限带宽:无流量限制
  • 自动部署:Git 推送自动触发
  • 预览部署:PR 自动生成预览链接
  • 构建时间快:相比其他平台更快

8. 注意事项

  • Cloudflare Pages 支持 Node.js 18 和 20
  • 构建时间限制为 15 分钟(免费版)
  • 每月构建次数限制为 500 次(免费版)
  • 建议使用 .gitignore 排除 node_modulespublic 目录

📚 总结

完成以上步骤后,你的 Hexo 博客已经成功搭建,实现:

  • 环境配置\
  • Hexo 框架搭建\
  • GitHub Pages 部署\
  • 主题美化\
  • Vercel 部署与域名绑定\
  • Cloudflare Pages 部署与全球加速

Hexo博客搭建教程
https://hyper001.cn/posts/102.html
作者
Hyper001
发布于
2025年12月2日
更新于
2025年12月17日
许可协议
返回首页