搭建 hexo 博客

环境:ubuntu 20.04

参考:https://hexo.io/zh-cn/docs/ https://theme-next.js.org/docs/getting-started/#Enabling-NexT

升级 nodejs

不然可能会遇到奇奇怪怪的问题

1
2
3
4
sudo npm install n -g
sudo n stable
hash -r
node --version

安装 hexo

1
sudo npm install -g hexo-cli

建站

1
2
3
4
mkdir blog
hexo init blog
cd blog
npm install

安装 Next 主题

1
2
3
cd blog
npm install hexo-theme-next
npm install [email protected] // 更新

有两个配置文件:根目录下的 _config.ymlnode_modules/hexo-theme-next/_config.yml,前者为 hexo 的配置文件,后者为 Next 的配置文件。

配置网站作者

Hexo config file

1
2
# Site
author: 林芝驰

配置网站描述

Hexo config file

1
2
# Site
description: '林芝驰的个人小站'

使能 Next 主题

Hexo config file

1
theme: next

现在查看下网站:

1
2
hexo clean
hexo server

访问 http://localhost:4000/

上面配置的信息:

image-20220925163856353

配置 Next

如果要修改 Next 的配置文件,不要直接修改 node_modules/hexo-theme-next/_config.yml,Next 升级可能会覆盖掉配置或者有冲突导致升级不成功。参考:https://theme-next.js.org/docs/getting-started/configuration.html

1
2
cd blog
cp node_modules/hexo-theme-next/_config.yml _config.next.yml

部署

创建 persuepersue.github.io 仓库

生成 ssh key

1
ssh-keygen -t rsa -C "persuepersue/persuepersue.github.io"

保存为 /home/ql/.ssh/id_rsa_persuepersue.github.io

将 ~/.ssh/id_rsa_persuepersue.github.io.pub 贴到 github

如果一台电脑往多个 github 上提交,就需要

~/.ssh/config

1
2
3
4
host persuepersue_github.com
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa_persuepersue.github.io

配置 github

参考

https://learnku.com/articles/48034

https://hexo.io/zh-tw/docs/github-pages.html (zh-tw 的版本比简体中文的新)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cd blog
git init
vim .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = [email protected]_github.com:persuepersue/persuepersue.github.io.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main

git add .
git commit -m "hexo init"
git branch -M main
git push -u origin main

这里的 url 如果电脑只访问一个 github 账号,可以直接 git@github.com:persuepersue/persuepersue.github.io.git。但是如果有多个 github 账号,那就需要像上面一样。

部署

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
cd blog
mkdir .github/workflows/
vim .github/workflows/pages.yml
name: Pages

on:
push:
branches:
- main # default branch

jobs:
pages:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: Use Node.js 16.x
uses: actions/[email protected]
with:
node-version: '16'
- name: Cache NPM dependencies
uses: actions/[email protected]
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Deploy
uses: peaceiris/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public

在仓库中前往 Settings > Pages > Source,将 branch 改为 gh-pages

1
2
3
4
git add .
git commit -m "github actions 设置"
git branch -M main
git push -u origin main

访问

https://persuepersue.github.io/

写文章

参考:https://hexo.io/zh-tw/docs/writing