0%

Hexo博客部署到腾讯云服务器

由于之前一直将博客托管到 Coding 上,现在好像是要收费了,加上老的域名过期了,我重新申请了一个腾讯云的服务器,换上了新的域名,开始折腾将博客迁移到腾讯云服务器,关于购买服务器的步骤此处省略,安装官网上一步一步进行就可以了。

Git 配置

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
# 1.安装git依赖库
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel

# 2.安装编译工具
yum install gcc perl-ExtUtils-MakeMaker package

# 3.此目录存放git 安装包
cd /usr/local/src

# 4.在当前目录下(/usr/local/src),下载源码包
wget https://www.kernel.org/pub/software/scm/git/git-2.31.0.tar.gz

# 5.解压
tar -zvxf git-2.31.0.tar.gz

# 6.进入目录
cd git-2.31.0

# 7.执行编译
make all prefix=/usr/local/git

# 8.安装git
make install prefix=/usr/local/git

# 9.设置环境变量
echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/bashrc

# 10.刷新环境变量
source /etc/bashrc

# 11.查看git版本
git --version

# 12.创建博客网站目录
mkdir /home/git/

# 13.修改网站目录权限
chown -R $USER:$USER /home/git/
chmod -R 755 /home/git/

# 14.创建git仓库
cd /home/git/
git init --bare blog.git

# 15.创建 git 钩子,用于自动部署
vim /home/git/blog.git/hooks/post-receive

# 16.进入编辑模式后(按i),输入下面内容
#!/bin/bash
git --work-tree=/home/blog --git-dir=/home/git/blog.git checkout -f

# 17.修改文件权限
chmod +x /home/git/blog.git/hooks/post-receive

Nginx 安装

1
2
3
4
5
6
7
8
#  安装
sudo yum -y install nginx

sudo systemctl enable nginx # 设置开机启动
sudo service nginx start # 启动 nginx 服务
sudo service nginx stop # 停止 nginx 服务
sudo service nginx restart # 重启 nginx 服务
sudo service nginx reload # 重新加载配置,一般是在修改过 nginx 配置文件时使用。

当我在执行 sudo service nginx start 时会报这个错误,

1
2
3
[root@VM-0-8-centos ~]# sudo service nginx start
Redirecting to /bin/systemctl start nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

看到 stackoverflow 给出的命令:

1
2
3
sudo fuser -k 80/tcp
sudo fuser -k 443/tcp
sudo service nginx restart

再次执行后就好:

1
2
[root@VM-0-8-centos ~]# sudo service nginx start
Redirecting to /bin/systemctl start nginx.service

配置Nginx

  1. 创建托管文件目录
1
2
3
mkdir /home/blog/
chown -R $USER:$USER /home/blog/
chmod -R 755 /home/blog/
  1. 修改Nginx配置
1
2
nginx -t
vim /etc/nginx/nginx.conf
  1. nginx.conf的修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
listen [::]:80;
server_name www.nnxkcloud.com; #这里修改了
root /home/blog; #这里修改了

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
  1. 重启nginx
1
service nginx restart

配置 hexo

  1. 找到并编辑_config.yml文件里面的 deploy 属性,
1
2
3
4
deploy:
- type: git
repo: root@175.24.24.72:/home/git/blog
branch: master
  1. 保存后,切换到博客命令,执行hexo命令,开始部署。
1
2
3
hexo clean
hexo generate
hexo deploy
  1. 当执行 deploy 命令时会提示输入服务器的密码,
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
joe@joes-MacBook-Pro Blog % npm run deploy
Debugger attached.

> hexo-site@0.0.0 deploy /Users/joe/Documents/Blog
> hexo deploy

Debugger attached.
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
On branch master
nothing to commit, working tree clean
The authenticity of host '175.24.24.72 (175.24.24.72)' can't be established.
ECDSA key fingerprint is SHA256:******.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '175.24.24.72' (ECDSA) to the list of known hosts.
root@175.24.24.72's password: #这里输入服务器密码
Enumerating objects: 178, done.
Counting objects: 100% (178/178), done.
Delta compression using up to 4 threads
Compressing objects: 100% (144/144), done.
Writing objects: 100% (178/178), 2.71 MiB | 8.08 MiB/s, done.
Total 178 (delta 23), reused 0 (delta 0)
To 175.24.24.72:/home/git/blog
* [new branch] HEAD -> master
Branch 'master' set up to track remote branch 'master' from 'root@175.24.24.72:/home/git/blog'.
INFO Deploy done: git
  1. 如果改为git@175.24.24.72:/home/git/blog.git,需要执行sudo passwd git,给git设置密码。
1
2
3
4
deploy:
- type: git
repo: git@175.24.24.72:/home/git/blog.git
branch: master

这时在本地博客项目中执行 hexo d 会报这些错误:

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
joe@joes-MacBook-Pro Blog % hexo d       
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
[master dee1457] Site updated: 2021-08-17 10:05:07
1 file changed, 1 insertion(+), 1 deletion(-)
git@175.24.24.72's password: #输入上面设置的git密码
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 365 bytes | 365.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: error: insufficient permission for adding an object to repository database ./objects
remote: fatal: failed to write object
error: remote unpack failed: unpack-objects abnormal exit
To 175.24.24.72:/home/git/blog.git
! [remote rejected] HEAD -> master (unpacker error)
error: failed to push some refs to 'git@175.24.24.72:/home/git/blog.git'
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (/Users/joe/Documents/Blog/node_modules/hexo-util/lib/spawn.js:51:21)
at ChildProcess.emit (events.js:314:20)
at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)

或者这个错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
joe@joes-MacBook-Pro Blog % hexo d
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
On branch master
nothing to commit, working tree clean
git@175.24.24.72's password: #输入上面设置的git密码
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 365 bytes | 365.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
fatal: Unable to create '/home/git/blog.git/refs/heads/master.lock': ????
fatal: the remote end hung up unexpectedly
fatal: the remote end hung up unexpectedly
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess.<anonymous> (/Users/joe/Documents/Blog/node_modules/hexo-util/lib/spawn.js:51:21)
at ChildProcess.emit (events.js:314:20)
at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)

这是没有权限的报错,在服务器命令行中以下执行后再次 hexo d

sudo chmod 777 -R blog.git/objects
sudo chmod 777 -R blog.git/refs
1
2
3
4
5
6


### 添加`https`访问

去[腾讯云](https://console.cloud.tencent.com/ssl)上将下载好的证书放在`/etc/nginx/cert`目录下,

[root@VM-0-8-centos cert]# ls -l 总用量 8 -rw-r--r-- 1 root root 1704 8月 17 13:20 www.nnxkcloud.com.key -rw-r--r-- 1 root root 3761 8月 17 13:20 www.nnxkcloud.com.pem [root@VM-0-8-centos cert]# pwd /etc/nginx/cert
1
2
3

再次编辑 vi nginx.conf,

server { # 服务器端口使用443,开启ssl, 这里ssl就是上面安装的ssl模块 listen 443 ssl; # 域名,多个以空格分开 server_name www.nnxkcloud.com; root /home/blog; # ssl证书地址 ssl_certificate /etc/nginx/cert/www.nnxkcloud.com.pem; # pem文件的路径 ssl_certificate_key /etc/nginx/cert/www.nnxkcloud.com.key; # key文件的路径 # ssl验证相关配置 ssl_session_timeout 5m; #缓存有效期 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议 ssl_prefer_server_ciphers on; #使用服务器端的首选算法 location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root /home/images/;#指定图片存放路径 #access_log /usr/local/websrv/nginx-1.9.4/logs/images.log;#日志存放路径 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /home/images/;#图片访问路径 proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; if ( !-e $request_filename){ proxy_pass http://127.0.0.1;#默认80端口 } } error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
1
2
3

保存后,查看占用的80端口,kill掉,然后重启nginx

[root@VM-0-8-centos nginx]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32349/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 968/sshd tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 32349/nginx: master tcp6 0 0 :::3306 :::* LISTEN 934/mysqld tcp6 0 0 :::80 :::* LISTEN 32349/nginx: master [root@VM-0-8-centos nginx]# kill 32349 [root@VM-0-8-centos nginx]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 968/sshd tcp6 0 0 :::3306 :::* LISTEN 934/mysqld [root@VM-0-8-centos nginx]# service nginx restart Redirecting to /bin/systemctl restart nginx.service ``` 最后,在浏览器输入 [www.nnxkcloud.com](https://www.nnxkcloud.com)可以看到了。