一、将Hexo部署到gitee上

在本地的hexo博客目录中通过npm安装插件:

1
$ sudo cnpm install hexo-deployer-git --save

修改配置文件_config.yml

1
2
3
4
deploy:
type: git
repo: https://gitee.com/z2huo/hexo-blog.git
branch: master

之后在博客文件夹下执行一系列命令,就会将博客部署到gitee上面:

1
2
3
4
5
$ hexo clean
...
$ hexo generate
...
$ hexo deploy

然后在gitee上面仓库中操作gitee pages服务即可。

部署成功之后,就可以通过gitee提供的链接访问网站了,链接如下:https://z2huo.gitee.io/hexo-blog/

但是遇到了一个问题,网站中所有的css文件都404了,获取不到,从网上面查阅资料了解到,gitee上面部署博客的仓库的名称需要与注册gitee的用户名一致,即z2huo

所以新建与gitee用户同名的仓库,重复一遍上述的步骤,所有的css文件就能够访问到了。

这里发现了一个gitee上面的特点:如果仓库名和gitee用户名称一致的话,gitee pages服务提供的网站地址会与其他情况有所不同:

可以看到上面两个截图中网站地址的差异。

二、将Hexo部署到阿里云ECS上

1、前提条件

1.1 登陆服务器为root用户

以下的操作建立在root用户权限之下,如果权限不足请申请root权限或采用sudo关键字

1.2 为ECS实例分配密钥对

本地连接ECS实例的方式可以选择用户密码,我接下来的操作因为在购买ECS的时候即分配了密钥对,所以在连接ECS实例的时候都通过ssh链接的方式。

新建密钥对并绑定密钥对到ECS实例后,会将公钥保存在服务器的~/.ssh/authorized_keys中,然后让用户下载私钥到本地,我们放到本地机器的~/.ssh/目录下。

1.3 本地存在多个密钥对的情况

我本地除了有用来登陆ECS实例的密钥对外,还有登陆github的密钥对,还有登陆gitee的密钥对,可以通过在本地用户home根目录下创建~/.ssh/config文件来指定需要连接的主机名(IP地址)的证书位置,config文件示例如下:

1
2
3
4
5
6
7
8
9
10
11
Host github
HostName github.com
Port 22
User git
IdentityFile ~/.ssh/github

Host aliyun
HostName 111.111.111.111
Port 22
User root
IdentityFile ~/.ssh/aliyun/cpu2memory2gbECS.pem

这样在通过ssh命令连接远程主机时就可以省略一些参数了,比如在没有config的情况下,连接ECS时不通过-i参数指定证书文件地址,输出如下:

1
2
3
$ sudo ssh root@111.111.111.111
Password:
root@111.111.111.111: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

配置了config之后,可以通过如下方式直接连接到ECS实例:

1
2
3
4
5
6
7
8
9
10
$ ssh aliyun

Welcome to Alibaba Cloud Elastic Compute Service !

Updates Information Summary: available
2 Security notice(s)
2 Important Security notice(s)
Run "dnf upgrade-minimal --security" to apply all updates.More details please refer to:
https://help.aliyun.com/document_detail/416274.html
Last login: Mon Jan 15 22:26:30 2024 from 221.221.221.221

在实际操作中,上面的配置文件中有一个问题,证书文件的位置中,通过~来代指用户home目录,连接时会有问题,所以将其替换为绝对路径,即/Users/myAccount/.ssh/github

1.4 需要安装git和nginx

需要安装git和nginx,操作如下:

1
2
$ apt install git nginx
$ yum install git nginx

2、在ECS上创建git仓库

建立文件路径。

1
$ mkdir /var/repo/

修改目录权限,因为我是通过root用户登陆的服务器,所以不需要修改目录的权限。当然,如果新增一个用户来专门用来执行部署hexo博客的操作的话,可以单独新增一个用户,之后修改目录的归属权等,之后的修改目录权限的操作都是在这个前提之下。

1
2
$ chown -R $USER:$USER /var/repo/
$ chmod -R 755 /var/repo

创建远程git仓库

1
2
$ cd /var/repo/
$ git init --bare hexo-blog.git

3、配置Nginx托管文件目录

创建文件路径。

1
$ mkdir -p /var/www/hexo/

修改目录权限。

1
2
$ chown -R $USER:$USER /var/www/hexo
$ chmod -R 755 /var/www/hexo

修改nginx配置文件中的root,指向上面新增的目录。

1
$ vim /etc/nginx/nginx.conf

修改内容如下:

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
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80;
listen [::]:80;
server_name _;
# root /usr/share/nginx/html;
root /var/www/hexo;

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

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

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

重启nginx服务

1
$ service nginx restart

此时,就搭建好了nginx服务器,因为修改了root指向的目录为/var/www/hexo,想要测试的话在该目录下新增index.html文件,之后通过ECS服务器的ip访问即可。

4、创建Git钩子文件

在新增的hexo-blog.git仓库中新增一个钩子文件。

1
$ vim /var/repo/hexo-blog.git/hooks/post-receive

输入文件内容如下:

1
2
3
#!/bin/bash

git --work-tree=/var/www/hexo --git-dir=/var/repo/hexo-blog.git checkout -f

保存文件后赋予该文件执行权限。

1
$ chmod +x /var/repo/hexo-blog.git/hooks/post-receive

5、将hexo博客部署到ECS的远程git上

修改本地的hexo博客的_config.yml文件,最后内容如下:

1
2
3
4
deploy:
type: git
repo: root@111.111.111.111:/var/repo/hexo-blog.git
branch: master

之后在本地执行hexo博客的部署操作

1
2
3
$ hexo clean
$ hexo generate
$ hexo depoly

6、通过ECS的ip来访问部署的博客

相关链接

Commands | Hexo

OB tags

#Hexo #SSH #Nginx #Git #建站