博客部署到自己的服务器上

事情原因

码云把我的仓库给封了,其中仓库的内容为 public 生成的文件夹内容
在这里插入图片描述

本地内容推送至仓库

在这里插入图片描述

部署服务器过程

直接在服务器上安装 git,nodejs,hexo,直接hexo s -p 80运行起来

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
# 1. 配置 node.js 安装源
curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -

# 2. yum 安装 node.js
yum -y install nodejs

# 3. 查看 node.js 版本
node -v
》》》v12.20.2

# 4. 配置 npm 淘宝仓库
npm config set registry http://registry.npm.taobao.org

# 5. 使用 npm 全局安装 hexo 客户端
npm -g install hexo-cli

# 6. 查看 hexo 版本
hexo -v
》》》hexo-cli: 4.2.0
》》》os: Linux 3.10.0-1160.6.1.el7.x86_64 linux x64
》》》node: 12.20.2
》》》v8: 7.8.279.23-node.45
》》》uv: 1.40.0
》》》zlib: 1.2.11
》》》brotli: 1.0.9
》》》ares: 1.16.1
》》》modules: 72
》》》nghttp2: 1.41.0
》》》napi: 7
》》》llhttp: 2.1.3
》》》http_parser: 2.9.4
》》》openssl: 1.1.1i
》》》cldr: 37.0
》》》icu: 67.1
》》》tz: 2019c
》》》unicode: 13.0

# 7. 使用 yum 安装 git
yum -y install git

# 8. 查看 git 版本
git --version
》》》git version 1.8.3.1

# 9. 创建文件夹,并进入(用于存放 git clone 的项目)
mkdir -p /app/git/ ; cd /app/git/
git config --global http.sslverify false
git config --global https.sslverify false
ssh-keygen -t rsa -C "18574150851" # 回车三次
cat /root/.ssh/id_rsa.pub # 把内容复制,添加公钥 https://gitee.com/taopanfeng/blog/deploy_keys

# 10. 克隆项目(输入用户名,密码)
git clone https://gitee.com/taopanfeng/blog.git
》》》正克隆到 'blog'...
》》》Username for 'https://gitee.com': 18574150851 # 用户名(大王卡)
》》》Password for 'https://18574150851@gitee.com': # 密码(YY密码)
》》》remote: Enumerating objects: 731, done.
》》》remote: Counting objects: 100% (731/731), done.
》》》remote: Compressing objects: 100% (662/662), done.
》》》remote: Total 731 (delta 49), reused 700 (delta 34), pack-reused 0
》》》接收对象中: 100% (731/731), 2.47 MiB | 987.00 KiB/s, done.
》》》处理 delta 中: 100% (49/49), done.

# 11. 查看详情
ll
》》》总用量 0
》》》drwxr-xr-x 7 root root 226 2月 22 07:44 blog

# 12. 进入项目目录,查看内容
cd blog/
ls
》》》_config.yml package.json Question.md Question_No_Record.md README.md scaffolds source source_bak themes 修改记录.md

# 13. 删除依赖模块,重新强制安装依赖模块
rm -rf node_modules && npm install --force

# 14. 本地使用 80 端口启动 hexo
hexo s -p 80
》》》INFO Start processing
》》》WARN ===============================================================
》》》WARN ========================= ATTENTION! ==========================
》》》WARN ===============================================================
》》》WARN NexT repository is moving here: https://github.com/theme-next
》》》WARN ===============================================================
》》》WARN It's rebase to v6.0.0 and future maintenance will resume there
》》》WARN ===============================================================
》》》INFO Hexo is running at http://localhost:80 . Press Ctrl+C to stop.

# 15. 设置 hexo.sh
cat > /root/hexo.sh << 'EOF'
# 进入目录
cd /app/git/blog

# 更新数据
git pull

# 杀死进程
#kill -9 $(netstat -atlunp | grep 4000 | awk '{print $7}' | cut -d '/' -f 1)
hexo_pid=$(ps aux | grep hexo | grep -v grep | grep -v sh | awk '{print $2}')
kill -9 ${hexo_pid}

# 重新生成 index(搜索 algolia)
export HEXO_ALGOLIA_INDEXING_KEY="fd04620183f122e80ec0d57ea40bfb2f";hexo algolia

# 重新后台启动(默认 4000端口)
hexo s &


EOF

# 16. 以后再执行 git pull 就需要输入账号、密码
vi /app/git/blog/.git/config
#[remote "origin"]
# url = https://username:password@gitee.com/taopanfeng/blog.git

绑定域名

详情参考:注册阿里云域名,并绑定自己的服务器

后续操作问题

2022-02-09 19:41:28
1、本地编辑文件,使用 idea commit,push

2、登录服务器,进行 sh hexo.sh 更新即可

后续操作问题(废弃)

1、本地编辑文件,使用 idea commit,push

2、登录服务器,进行 sh hexo.sh 更新即可

3、可能会存在 hexo 不稳定的情况。(不知道为什么,我执行 hexo s -p 80 &,明明后台运行,但是不是会结束进程)
解决方法:写一个脚本,定时去判断是否启动,如果未启动,就让它启动就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# EOF 加单引号,防止 EOF的 body 进行转义
cat > ~/hexo.sh << 'EOF'
#!/bin/bash
# 自动部署博客,防止自动关闭
# * * * * * /usr/bin/sh /root/hexo.sh 每分钟执行一次此脚本 crontab -e

# 2021-03-26 16:39:33
# 更新博客
cd /app/git/blog/
git pull

# 2021-02-23 10:12:29
# 为空,再启动一次
if [ -z "$(netstat -atlunp | grep 80 | grep hexo)" ];then
#echo "不存在,启动"
cd /app/git/blog ; hexo s -p 80 &
fi

EOF

使用 crontab -e 添加一行内容:* * * * * /usr/bin/sh /root/hexo.sh

运行本地容器:
docker run --name centos01 -p 18019:18019 -itd --privileged=true --restart=always a1697752105/centos /usr/sbin/init