Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

这篇文章记录了从零搭建个人博客的完整教程,包括环境配置、主题安装、个性化设置和部署上线。

一、环境准备

在开始之前,需要安装以下工具:

  1. Node.js:Hexo 基于 Node.js,建议安装 LTS 版本。
  2. Git:用于克隆主题和部署到 GitHub。
  3. GitHub 账号:用于托管博客源码和静态页面。

安装完成后,在终端中验证:

1
2
3
node -v
npm -v
git --version

二、安装 Hexo

全局安装 Hexo 命令行工具:

1
npm install -g hexo-cli

初始化博客:

1
2
3
hexo init myblog
cd myblog
npm install

本地预览:

1
hexo server

浏览器访问 http://localhost:4000,能看到默认页面即表示成功。

三、安装 Volantis 主题

进入博客根目录,克隆主题:

1
git clone https://github.com/volantis-x/hexo-theme-volantis.git themes/volantis

修改根目录下的 _config.yml:

1
theme: volantis

将主题的配置文件复制到博客根目录,便于后续升级主题时不丢失配置:

1
cp themes/volantis/_config.yml _config.volantis.yml

之后所有主题相关的设置都在 _config.volantis.yml 中修改。

四、基础配置

打开 _config.volantis.yml,按需修改以下常用项。

1. 导航栏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
navbar:
visiable: always
menu:
- name: 博客
icon: fa-solid fa-blog
url: /
- name: 分类
icon: fa-solid fa-folder-open
url: categories/
- name: 标签
icon: fa-solid fa-tags
url: tags/
- name: 归档
icon: fa-solid fa-archive
url: archives/
- name: 友链
icon: fa-solid fa-link
url: friends/
- name: 关于
icon: fa-solid fa-info-circle
url: about/
search: Search...

2. 侧边栏

1
2
3
4
5
6
7
8
9
10
11
sidebar:
position: left
effect: [shadow, floatable]
widget_library:
blogger:
class: blogger
display: [desktop, mobile]
avatar: /images/profile_photo.jpg
shape: circle
jinrishici: true
social: false

3. 封面

1
2
3
4
5
6
cover:
height_scheme: full
layout_scheme: search
background: /images/background.jpeg
title: 'QianYin-xcy'
search: 搜一下

4. 页脚

1
2
3
4
5
6
7
8
9
10
site_footer:
layout: [social, license, info, copyright]
social:
- icon: fas fa-rss
url: atom.xml
- icon: fas fa-envelope
url: mailto:your@email.com
- icon: fab fa-github
url: https://github.com/yourname
copyright: '[Copyright © 2024 yourname](/)'

5. 正文间距

如果觉得标题上下空白太大,可以适当调小:

1
2
3
4
5
6
7
custom_css:
gap:
h2: 16px
h3: 12px
h4: 8px
p: 1em
line_height: 1.6

五、创建常用页面

在博客根目录执行:

1
2
3
4
5
hexo new page categories
hexo new page tags
hexo new page archives
hexo new page friends
hexo new page about

然后分别编辑 source/ 下对应目录的 index.md。

分类页 source/categories/index.md:

1
2
3
4
5
---
title: 分类
date: 2024-01-01 00:00:00
type: categories
---

标签页 source/tags/index.md:

1
2
3
4
5
---
title: 标签
date: 2024-01-01 00:00:00
type: tags
---

归档页 source/archives/index.md:

1
2
3
4
5
---
title: 归档
date: 2024-01-01 00:00:00
type: archives
---

友链页 source/friends/index.md:

1
2
3
4
5
---
title: 友链
date: 2024-01-01 00:00:00
type: friends
---

关于页 source/about/index.md:

1
2
3
4
5
---
title: 关于
date: 2024-01-01 00:00:00
type: about
---

六、写作规范

Volantis 对标准 Markdown 支持很好,建议:

  • 标题使用 ##、###,不要手动加粗代替标题。
  • 列表使用 - 或 1.,不要用   手动缩进。
  • 分段用空行,不要用 <br>。
  • 代码块标注语言,例如 ```c、```bash。
  • 图片放在 source/images/ 下,用 /images/xxx.png 引用。

七、部署到 GitHub Pages

1. 创建仓库

在 GitHub 上新建仓库,仓库名建议为:

1
<你的用户名>.github.io

例如 qianyin-xcy.github.io。

2. 安装部署插件

在博客根目录执行:

1
npm install hexo-deployer-git --save

3. 配置部署

修改根目录 _config.yml:

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

如果使用 SSH,可写成:

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

4. 生成并部署

1
2
3
hexo clean
hexo generate
hexo deploy

部署完成后,访问 https://<你的用户名>.github.io 即可看到博客。

八、常见问题

Q:修改配置后不生效?
执行 hexo clean && hexo generate,再刷新浏览器。

Q:标题上方空白太大?
检查 custom_css.gap 中的 h2、h3、h4 是否过大,同时确认导航栏高度不要用 vh,建议用固定像素如 64px。

Q:部署后样式丢失?
检查 _config.yml 中的 url 和 root 是否正确。如果是 <用户名>.github.io 仓库,url 填 https://<用户名>.github.io,root 填 /。

Q:如何绑定自定义域名?
在 source/ 下新建 CNAME 文件,写入你的域名,然后在域名服务商处添加 CNAME 解析到 <用户名>.github.io。

九、总结

至此,你已经完成了从环境配置、Hexo 安装、Volantis 主题配置到 GitHub Pages 部署的全部流程。后续只需要专注于写作,用标准 Markdown 记录笔记,博客会自动生成并发布。

评论