deploy-my-blog

1. 安装 openresty

1.1 安装依赖库

1
yum install libpcre3-dev libssl-dev perl make build-essential curl

1.2 编译并安装 openresty

1
2
3
4
5
tar -xzvf openresty-VERSION.tar.gz
cd openresty-VERSION/
./configure --prefix=/usr/local/openresty
make
sudo make install

1.3 编写服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/openresty/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

1.4 启动服务

1
2
3
4
5
6
7
8
9
10
11
12
# 设置自动启动
systemctl enable nginx.service
# 编辑配置
/usr/local/openresty/nginx/conf/nginx.conf
####
location / {
root /home/git/blog;
index index.html index.htm;
}
####
# 启动
systemctl start nginx.service

2. 安装 hexo

https://hexo.io/docs/

3. 部署

3.1 建个仓库,用来存放数据

3.2 https://hexo.io/docs/one-command-deployment

3.3 服务器中新建用户, 并定时拉取仓库

1
2
3
4
5
sudo adduser git
git clone xxxxx

# 设置定时任务
*/3 * * * * /home/git/git_pull.sh
1
2
3
4
5
6
# git_pull.sh
echo "start run git pull"
cd /home/git/blog
date +"%Y-%m-%d %H:%M:%S" > /home/git/cron-exec.log
git pull --rebase
echo "end git pull"

4. ipynb 转 markdown

package.json

1
2
3
4
5
6
7
8
"scripts": {
"new": "hexo new post",
"build": "npm run convert:notebooks && hexo generate",
"convert:notebooks": "node convert_notebooks.js",
"clean": "hexo clean",
"deploy": "hexo deploy",
"server": "hexo server"
},

convert_notebooks.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const { exec  } = require('child_process');

// 要执行的命令
const command = 'd:/anaconda3/envs/py311/python.exe convert_ipynb2md.py'; // 这里的 script.py 是你想要执行的 Python 脚本

// 执行命令
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`执行错误: ${error.message}`);
return;
}
if (stderr) {
console.error(`标准错误输出: ${stderr}`);
return;
}
console.log(`标准输出: ${stdout}`);
});

convert_ipynb2md.py

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
import os
from nbconvert import MarkdownExporter
import nbformat
import glob
import datetime


def convert_ipynb_to_md(ipynb_file, md_file, fname):
if md_file is None:
md_file = os.path.splitext(ipynb_file)[0] + ".md"

with open(ipynb_file, "r", encoding="utf-8") as f:
contents = f.read()

exporter = MarkdownExporter()
(body, resources) = exporter.from_notebook_node(nbformat.reads(contents, as_version=4))

with open(md_file, "w", encoding="utf-8") as f:
f.write(f"""
---
title: {fname}
date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
tags: note
---
""")
f.write(body)

print(f"Converted {ipynb_file} to {md_file}")

if __name__ == "__main__":
for file in glob.glob("source/asset/*.ipynb"):
fname = os.path.basename(file)
convert_ipynb_to_md(file, f"source/_posts/{fname.replace('.ipynb', '.md')}", fname)

npm run build , 会自动将 source/asset 下面的 .ipynb 文件自动转换为 .md ,并放到 source/_posts 下面


deploy-my-blog
http://47.97.104.205/2024-05-10/deploy-my-blog/
作者
Rojer Di
发布于
2024年5月10日
许可协议