06-Hexo+Cloudflare-零成本搭建个人博客

一、方案简介

使用 Hexo + Cloudflare Pages 搭建个人博客,简单易上手,完全免费!

|| 特性 | 说明 ||
|—–|—–|—–|
|| 完全免费 | Cloudflare Pages无限流量+无限请求 |
|| 全球CDN | Cloudflare全球节点,访问速度快 |
|| HTTPS | 自动配置SSL证书 |
|| 静态部署 | 无服务器,秒级部署 |
|| 主题丰富 | 1000+ Hexo主题 |


二、环境准备

2.1 安装Node.js

1
2
3
4
5
6
7
8
9
10
# 方法1:官网下载
# https://nodejs.org/

# 方法2:使用nvm(推荐)
# Windows: https://github.com/coreybutler/nvm-windows
# macOS/Linux: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# 验证安装
node --version
npm --version

2.2 安装Hexo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 全局安装Hexo
npm install -g hexo-cli

# 验证安装
hexo --version

# 创建博客目录
hexo init my-blog
cd my-blog

# 安装依赖
npm install

# 启动本地服务器
hexo server

# 访问 http://localhost:4000

三、Hexo基础配置

3.1 目录结构

1
2
3
4
5
6
7
my-blog/
├── _config.yml # 站点配置文件
├── package.json # 依赖配置
├── scaffolds/ # 模板目录
├── source/ # 源文件目录
│ └── _posts/ # 文章目录
└── themes/ # 主题目录

3.2 配置文件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# _config.yml
# Site
title: 我的技术博客
subtitle: 用代码改变世界
description: 分享Python、数据分析、Web开发的干货教程
keywords: Python, 数据分析, 机器学习
author: 博主
language: zh-CN
timezone: Asia/Shanghai

# URL
url: https://yourname.pages.dev
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:

# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:

# Writing
new_post_name: :title.md
default_layout: post
titlecase: false
external_link:
enable: true
field: site
exclude: ''

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Date / Time format
date_format: YYYY-MM-DD
time_format: HH:mm:ss

# Pagination
per_page: 10
pagination_dir: page

# Extensions
theme: butterfly
exclude_generator:

# Deployment
deploy:
type: git
repo: https://github.com/yourname/yourname.github.io.git
branch: main

3.3 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 新建文章
hexo new "我的第一篇文章"

# 新建草稿
hexo new draft "草稿文章"

# 发布草稿
hexo publish "草稿文章"

# 生成静态文件
hexo generate
# 或简写
hexo g

# 启动本地服务器
hexo server
# 或简写
hexo s

# 清除缓存
hexo clean

# 部署到GitHub
hexo deploy
# 或简写
hexo d

# 一键部署
hexo clean && hexo g -d

四、主题配置(以Butterfly为例)

4.1 安装主题

1
2
3
4
5
# 下载主题
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly

# 安装依赖
npm install hexo-renderer-pug hexo-renderer-stylus --save

4.2 主题配置

各主题的详细步骤可见https://hexo.io/themes/


五、Cloudflare Pages部署

5.1 创建GitHub仓库

1
2
3
4
5
6
7
8
# 1. GitHub新建仓库:yourname.github.io
# 2. 本地初始化
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/yourname/yourname.github.io.git
git push -u origin main

5.2 连接Cloudflare Pages

1
2
3
4
5
6
7
8
9
步骤:
1. 登录 Cloudflare: https://www.cloudflare.com/
2. 进入 Pages → Create a project
3. Connect to Git → 选择你的GitHub仓库
4. 配置构建设置:
- Framework preset: Hexo
- Build command: npm run build
- Build output directory: public
5. 点击 Deploy site

5.3 自定义域名(可选)

1
2
3
4
1. 进入 Pages → 你的项目 → Custom domains
2. Add custom domain
3. 输入你的域名
4. 按照提示配置DNS

六、常用插件

6.1 安装插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 文章字数统计
npm install hexo-wordcount --save

# RSS订阅
npm install hexo-generator-feed --save

# Sitemap
npm install hexo-generator-sitemap --save
npm install hexo-generator-baidu-sitemap --save

# 图片懒加载
npm install hexo-lazyload-image --save

# 代码高亮
npm install hexo-prism-plugin --save

# 文章加密
npm install hexo-blog-encrypt --save

6.2 插件配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# _config.yml

# 字数统计
wordcount:
enable: true
post_word: true
min2read: true

# RSS
feed:
type: atom
path: atom.xml
limit: 20
hub:
content: true

# Sitemap
sitemap:
path: sitemap.xml
baidusitemap:
path: baidusitemap.xml

# 代码高亮
prism_plugin:
mode: preprocess
theme: tomorrow-night

七、写文章

7.1 新建文章

1
hexo new "Python爬虫入门"

7.2 文章模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
---
title: Python爬虫从入门到实战
date: 2024-01-15 12:00:00
updated: 2024-01-15 12:00:00
categories:
- Python
tags:
- 爬虫
- Requests
- BeautifulSoup
description: 网络爬虫是获取数据的重要手段...
cover: /img/covers/python-spider.jpg
---

网络爬虫(Web Crawler)是一种自动化获取网页数据的程序...

八、常见问题

8.1 部署后页面空白

1
2
# 解决:检查主题配置
hexo clean && hexo g

8.2 图片不显示

1
2
# _config.yml
post_asset_folder: true

8.3 部署慢

1
2
# 使用淘宝npm镜像
npm install -g cnpm --registry=https://registry.npmmirror.com

十、总结

|| 阶段 | 操作 ||
|—–|—–|—–|
|| 本地写作 | hexo new "文章标题" ||
|| 本地预览 | hexo s ||
|| 生成静态 | hexo g ||
|| 一键部署 | hexo clean && hexo g -d ||

💡 提示:配合GitHub Actions实现自动部署,写完文章推送就自动发布!


如果你觉得有帮助,欢迎点赞+在看👍


06-Hexo+Cloudflare-零成本搭建个人博客
https://r0f2.my/post/06-hexo-cloudflare-blog/
作者
JA
发布于
2026年2月3日
许可协议