Hexo 笔记网站搭建
习惯在漫步

一、域名申请

申请域名,下载域名文件到服务器,Nginx 路径下 /etc/nginx/

xgzmb.fun.key、xgzmb.fun_bundle.crt

二、安装 Nginx

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
sudo apt update
# 安装
sudo apt install nginx
# 启动
sudo systemctl start nginx
# 设置为开机启动
sudo systemctl enable nginx
# 查看服务状态
sudo systemctl status nginx
# 检测配置文件格式
sudo nginx -t
# 重新加载命令
sudo systemctl reload nginx
nginx -s reload
# 删除default
rm /etc/nginx/sites-enabled/default
# 修改
vim /etc/nginx/sites-available/my_site
################################################
################参考下面的配置信息#################
################################################
server {
listen 80;
listen [::]:80;
server_name xgzmb.fun www.xgzmb.fun;
#把http的域名请求转成https
return 301 https://$host$request_uri;

#root /var/www/my_site;
#root /var/www/html;
#index index.html index.htm;

#location / {
# try_files $uri $uri/ =404;
#}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name xgzmb.com wwww.xgzmb.fun;

#root /var/www/my_site;
root /var/www/html/notes/public;
index index.html index.htm;

ssl_certificate /etc/nginx/xgzmb.fun_bundle.crt; # 证书文件路径
ssl_certificate_key /etc/nginx/xgzmb.fun.key; # 私钥文件路径

# 其他SSL配置参数
ssl_protocols TLSv1.2 TLSv1.3; # 支持的协议版本
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on; # 优先使用服务器端的加密套件

location / {
try_files $uri $uri/ =404;
}
# 其他server配置...
}
################################################

# 建立新的配置软连接
sudo ln -s /etc/nginx/sites-available/my_site /etc/nginx/sites-enabled/
# log日志
tail -f /var/log/nginx/error.log

# nginx 启动默认的用户组是 www-data
vim /etc/nginx/nginx.conf
user www-data; 可以修改为 user www;

# 重新启动 nginx
sudo systemctl reload nginx

# 递归修改用户及用户组,注意是 ----- 用户:用户组
sudo chown -R www:www /var/www/html
# 设置目录权限为755(所有者可读写执行,组和其他用户可读执行)
chmod -R 774 /var/www/html/

# 切换到 www
su - www
# 切回 root,需要输入 www 的密码
sudo -i
# 如果忘记了 www 密码
# sudo passwd www

三、安装 Hexo

由于设置了用户为www,nginx 的访问权限为 www 用户权限,以下设置均以 www 用户视角

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
# 1. 安装 git
sudo apt update
git --version
# git version 2.43.0
# sudo apt-get install git-core
# 2. 安装 Node.js
sudo apt install nodejs npm
node -v
npm -v
# 3. npm 换源
npm config get registry
npm config set registry https://mirrors.cloud.tencent.com/npm/
# 4. 安装 Hexo
npm install -g hexo-cli
hexo -v
# 5. Hexo 将会在指定文件夹中新建所需要的文件
hexo init notes

# 修改主题
################################################
################参考下面的配置信息#################
################################################
# Site
title: '习惯在漫步'
subtitle: '笔记'
description: ''
keywords:
author: xgzmb
language: cn
timezone: 'Asia/Shanghai'

# 安装使用 keep 主题
theme: keep
################################################


四、安装 Keep

Keep 主题使用手册

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# 使用 npm 安装主题
cd notes
npm install hexo-theme-keep@latest
# 复制主题配置文件
cp node_modules/hexo-theme-keep/_config.yml _config.keep.yml
# 修改主题配置文件
################################################
################参考下面的配置信息#################
################################################

## ======================================================================================
## Hexo Theme Keep
## Documents: https://keep-docs.xpoet.cn
## Repository: https://github.com/XPoet/hexo-theme-keep
## ======================================================================================
# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/base_info.html
# ---------------------------------------------------------------------------------------
base_info:
# Theme primary color
primary_color: "#0066cc"

# Blog website title
title: 习惯在漫步

# Blog website author name
author: 习惯在漫步

# Author avatar, You can use local image path or image link
avatar:

# Website LOGO, You can use local image path or image link
logo:

# Website favicon, You can use local image path or image link
favicon:


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/menu.html
# ---------------------------------------------------------------------------------------
# If you want to enable a new navigation menu, you need to create a corresponding page
# e.g. to enable categories, you need to execute `hexo new page categories`
# Use `||` to set icon for navigation menu, e.g. `home: / || fa-solid fa-home`
menu:
home: / # || fa-solid fa-home
archives: /archives # || fa-solid fa-box-archive
# tags: /tags # || fa-solid fa-tags
# categories: /categories # || fa-solid fa-layer-group
# links: /links # || fa-solid fa-link
# photos: /photos # || fa-solid fa-image
# tools: /tools # || fa-solid fa-tools
# about: /about # || fa-solid fa-user-graduate
# ......


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/first_screen.html
# ---------------------------------------------------------------------------------------
first_screen:
enable: true # Option values: true | false

# First screen background image in light mode, You can use local image path or image link
background_img: /images/bg.svg

# First screen background image in dark mode, You can use local image path or image link
background_img_dark: /images/bg.svg

# First screen description
# You can use the "||" to begin a newline, maximum is two lines.
description: 莫愁前路无知己 || 天下谁人不识君

# If enable hitokoto, first screen description is different every time when you enter the website
hitokoto: false # Option values: true | false


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/social_contact.html
# ---------------------------------------------------------------------------------------
social_contact:
enable: false # Option values: true | false
links:
# Fill in your social platform links here, e.g. `github: https://github.com/XPoet`
# If you want to click open the picture, you need to add a prefix `img | `,
# at the same time change your link to the image link.
# e.g. `weixin: img | ./images/qrcode.png`
github: # GitHub
weixin: # WeChat
qq: # QQ
weibo: # WeiBo
zhihu: # ZhiHu
twitter: # Twitter
x: # X
facebook: # Facebook
email: # Email


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/scroll.html
# ---------------------------------------------------------------------------------------
scroll:
# Show progress bar in top when page scroll
progress_bar: false # Option values: true | false

# Show percent when page scroll
percent: true # Option values: true | false

# Hide header in top when page scroll
hide_header: true # Option values: true | false


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/home.html
# ---------------------------------------------------------------------------------------
home:
# Website announcement in home page
announcement:

# Show category in home page post block
category: false # Option values: true | false

# Show tags in home page post block
tag: false # Option values: true | false

# Set the datetime type of home page post block
post_datetime: updated # Option values: updated | created
post_datetime_format: YYYY年MM月DD日 HH:mm:ss

# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/post.html
# ---------------------------------------------------------------------------------------
post:
# Author badge in the post
author_badge:
enable: false # Option values: true | false

# If true, show Lv1, Lv2, Lv3 ...
# If false, show custom badge
level_badge: false # Option values: true | false

# Custom badge array, You can be fill one or more item
custom_badge: ["One", "Two", "Three"]

# Post word count
# Depend on Hexo Plugin: hexo-wordcount (`npm install hexo-wordcount`)
# See: https://github.com/willin/hexo-wordcount
word_count:
wordcount: true # Word count, one post. Option values: true | false
min2read: true # Time to read, one post. Option values: true | false

# Post datetime
datetime_format: "YYYY-MM-DD HH:mm:ss" # Datetime format. e.g. "YYYY-MM-DD HH:mm:ss"

# Post copyright info
copyright_info: false # Option values: true | false

# Post share
share: false # Option values: true | false

# Reward author
reward:
enable: false # Option values: true | false
img_link: # Image link for the payment QR code
text: # Custom reward text, Can be null
icon: # Custom reward icon, Can be null


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/code_block.html
# ---------------------------------------------------------------------------------------
code_block:
# Toolbar include: "code copy", "code block collapse" and "code language"
tools:
enable: true # Option values: true | false
style: default # Option values: default | mac
highlight_theme: obsidian # Option values: default | obsidian


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/toc.html
# ---------------------------------------------------------------------------------------
toc:
enable: false # Option values: true | false

# Automatically add list number to toc
number: false # Option values: true | false

# If true, all level of TOC in a post will be displayed, rather than the activated part of it.
expand_all: false # Option values: true | false

# If true, auto open TOC every time when you enter post page
init_open: true # Option values: true | false

# TOC layout on post page
layout: right # Option values: left | right


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/website_count.html
# ---------------------------------------------------------------------------------------
website_count:
# busuanzi
# See: http://ibruce.info/2015/04/04/busuanzi/
busuanzi_count:
enable: false # Option values: true | false
site_uv: false # Option values: true | false
site_pv: false # Option values: true | false
page_pv: false # Option values: true | false


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/local_search.html
# Depend on Hexo Plugin: hexo-generator-searchdb (`npm install hexo-generator-searchdb`)
# See: https://github.com/theme-next/hexo-generator-searchdb
# ---------------------------------------------------------------------------------------
local_search:
enable: false # Option values: true | false
preload: false # Preload the search data when the page loads. Option values: true | false


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/comment.html
# ---------------------------------------------------------------------------------------
comment:
enable: false # Option values: true | false
use: valine # Option values: valine | gitalk | twikoo | waline | giscus | artalk | disqus

# Valine
# See: https://github.com/xCss/Valine
# https://valine.js.org
valine:
appid: # Your leancloud application appid
appkey: # Your leancloud application appkey
server_urls: # Your Server URL
placeholder: # Input box placeholder

# Gitalk
# See: https://github.com/gitalk/gitalk
# https://gitalk.github.io
gitalk:
github_id: # GitHub repo owner
github_admins: # GitHub Admins (in Array type), optional
repository: # Repository name to store issues
client_id: # GitHub Application Client ID
client_secret: # GitHub Application Client Secret
proxy: # GitHub oauth request reverse proxy for CORS

# Twikoo
# See: https://github.com/imaegoo/twikoo
# https://twikoo.js.org
twikoo:
env_id: # Environment ID
region: # Environment region, Can be null
version: 1.6.36 # Twikoo version, default use v1.6.36

# Waline
# See: https://github.com/walinejs/waline
# https://waline.js.org/guide/get-started.html
waline:
server_url: # Server URL
reaction: false # Post reactions, option values: true | false
version: 3.2.1 # Waline version, default use v3.2.1

# Giscus
# See: https://github.com/giscus/giscus
# https://giscus.app
# Please generate your configuration items in https://giscus.app
giscus:
repo:
repo_id:
category: Announcements # Recommend use Announcements
category_id:
reactions_enabled: false # Option values: true | false

# Artalk
# See: https://github.com/ArtalkJS/Artalk
# https://artalk.js.org
artalk:
server: # Server URL

# Disqus
# See: https://disqus.com/
disqus:
shortname: # Disqus Shortname


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/rss.html
# Depend on Hexo Plugin: hexo-generator-feed (`npm install hexo-generator-feed`)
# See: https://github.com/hexojs/hexo-generator-feed
# ---------------------------------------------------------------------------------------
rss:
enable: false # Option values: true | false


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/lazyload.html
# ---------------------------------------------------------------------------------------
lazyload:
enable: false # Option values: true | false


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/cdn.html
# ---------------------------------------------------------------------------------------
cdn:
enable: false # Option values: true | false
provider: cdnjs # Option values: cdnjs | jsdelivr | unpkg


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/pjax.html
# ---------------------------------------------------------------------------------------
pjax:
enable: false # Option values: true | false


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/footer.html
# ---------------------------------------------------------------------------------------
footer:
since: 2024 # The starting year of your website, Can be null
word_count: true # Option values: true | false

site_deploy:
enable: true # Option values: true | false
provider: tencent_cloud # Option values: github | vercel | netlify | cloudflare | gitee | aliyun | tencent_cloud | upyun
url: # Your deployment provider url, Can be null

record:
enable: true # Option values: true | false
list:
- code: 冀公网安备13102202000623号 # record code of your website
link: http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=13102202000623 # record link of your website, Can be null
- code: 冀ICP备2024087536号-1 # record code of your website
link: https://beian.miit.gov.cn/ # record link of your website, Can be null
# e.g.
# - code: ICP 001
# - link: https://beian.miit.gov.cn


# ---------------------------------------------------------------------------------------
# Docs: https://keep-docs.xpoet.cn/basis/configuration-guide/inject.html
# ---------------------------------------------------------------------------------------
inject:
enable: false # Option values: true | false
css:
-
# e.g.
# - /css/custom-1.css
# - /css/custom-2.css
# - ...
js:
-
# e.g.
# - /js/custom-1.js
# - /js/custom-2.js
# - ...
################################################

# 部署 hexo
hexo clean
hexo generate -d
# 因为 nginx 已经指向了固定的目录/var/www/html/notes/public,所以无需启动服务了
# hexo server

五、服务器拉取笔记

关联上 Git 仓库,针对文档目录结构有一些限制,md 文档中的图片引用,必须是在同级的一个文件夹名称为 assets 下的文件才可以,后续的编码硬性要求了,应该有更简单的设置,等待挖掘

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
58
59
60
61
62
63
64
65
66
67
68
69
# 1. 服务器拉取项目
cd /home/www
git clone https://qingmei-zhuma:<token令牌>@gitee.com/qingmei-zhuma/notes.git

# 2. 建立软连接
ln -s /home/www/opt/notes-git-hexo-auto/notes /var/www/html/notes/source/_posts

# 3. 重新编译
hexo clean generate -d
# nohup hexo server &

# 4. 定时脚本 shell
vim /home/www/opt/notes-git-hexo-auto/git_repo_notes.sh
################################################
################参考下面的配置信息#################
################################################
#!/bin/bash

# 定义你要检查的分支名称
BRANCH="main"

# 定义你的仓库路径
REPO_PATH="/home/www/opt/notes-git-hexo-auto/notes/"
# hexo 路径
HEXO_NOTES_PATH="/var/www/html/notes/"

while true; do
# 切换到仓库目录
cd $REPO_PATH || { echo "无法切换到目录 $REPO_PATH"; exit 1; }

# 获取远程仓库的最新更新
git fetch

# 检查本地分支和远程分支是否有差异
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @ @{u})

if [ $LOCAL = $REMOTE ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S'): 本地仓库已经是最新的。"
elif [ $LOCAL = $BASE ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S'): 远程仓库有更新,正在合并..."
git pull || echo "$(date '+%Y-%m-%d %H:%M:%S'): git pull 失败"

# 开始移动图片
find * -type d -name assets | while read dir; do
echo "$dir"
cp -r "$dir"/* "$HEXO_NOTES_PATH"/source/assets/
done

# 切换到 HEXO 目录
cd $HEXO_NOTES_PATH || { echo "无法切换到目录 $HEXO_NOTES_PATH"; exit 1; }
hexo generate -d

elif [ $REMOTE = $BASE ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S'): 本地分支有未推送的提交。"
else
echo "$(date '+%Y-%m-%d %H:%M:%S'): 本地分支和远程分支有分歧,需要手动解决冲突。"
fi

# 等待1分钟
sleep 60
done
################################################

# 5. 执行shell脚本
nohup bash ./git_repo_notes.sh &
# 6. 查看日志
tail -f nohup.out

六、本地推送笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
```bat
@echo off

REM 设置代码页为 UTF-8
chcp 65001 >nul

REM 提示用户输入提交信息
cd /d "D:\AAAAA\notes"

REM 提示用户输入提交信息
set /p commitMessage=请输入提交信息:

REM 执行 Git 命令
git add .
git commit -m "%commitMessage%"
git push

REM 提示完成
echo 操作完成!
pause
```