快速搭建BLOG

环境

ECS服务器(用来存放hexo编译后部署的代码)
ECS服务器安装:nodejs、git、nginx
客户端(PC电脑:用来编辑自己的hexo相关配置文件以及主题)
PC端安装:hexo、git

ECS服务器

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
# 安装需要使用的程序
$ yum -y install nodejs git nginx
Complete!
$ adduser git
$ adduser www
$ su - git
# 配置提交代码时免密
$ ssh-keygen -t rsa(生成密钥)
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sss/.ssh/id_rsa):
Created directory '/home/sss/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sss/.ssh/id_rsa.
Your public key has been saved in /home/sss/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0HHKX1Ns9KwD8VwVwFgXjaPCy0zrtqLfuUoxu32XHH0 sss@centos
The key's randomart image is:
+---[RSA 2048]----+
| . . ==+=B|
| o + . **+.|
| . +. +oo.o|
| . .+..o . |
| S+.+ o. |
| += ..E|
| o. . o.|
| ..+o.. + |
| .o++*+ . |
+----[SHA256]-----+
$ chmod 600 authorized_keys
$ chmod 700 .ssh/ #这个步骤主要是为了防止配置上pc与ecs免密登陆,在登陆时还需要再次输入密码。

生成git仓库(git用户下操作)

1
2
3
4
5
# 在对应的git账户下初始化一个仓库
$ git init --bare test.git
Initialized empty Git repository in /home/git/test.git/
$ ls
test.git

自动部署(git用户)

1
2
3
4
5
6
7
8
9
10
11
12
$ cd /test.git/hooks
# 配置自动发布
$ vim post-receive(添加一下内容)
#!/bin/bash
echo "post-receive hook is running ..."
GIT_REPO=/home/git/test.git #自己博客服务器中的地址
TMP_GIT_CLONE=/tmp/test #编译后的静态配置文件目录
PUBLIC_WWW=/data/nginx/root/test #静态页面存放目录
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

编辑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
$ vim /etc/nginx/nginx.conf
user www www;
worker_processes 2;
worker_cpu_affinity 00000001 00000010;
error_log /data/error.log;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 20m;
server_tokens off;
proxy_intercept_errors on;
#default_type application/octet-stream;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
log_format log_json '{"@timestamp": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"up_resp_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';

access_log /data/nginx/logs/access.log log_json;
sendfile on;
tcp_nopush on;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
tcp_nodelay on;
keepalive_timeout 10;
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 9;
gzip_http_version 1.1;
gzip_min_length 1k;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
############################ Include ############################
include vhost/*.conf;
}

配置PC与服务器中git账户的免密

1
2
3
4
5
6
查看PC端当前用户下 ".ssh/id_rsa.pub"文件,并将该文件的内容复制到git用户下的 "authorized_keys" 文件中
测试是否能够登陆
$ ssh git@IP:PORT(默认22端口时直接通过: git@IP连接即可)
Last login: Wed May 11 15:50:20 2022
Welcome to Alibaba Cloud Elastic Compute Service !
$ git@HOSTNAME

PC端配置

创建hexo相关配置文件存放的目录

1
2
3
4
5
6
# hexo需要依赖于nodejs环境
$ brew install nodejs
# 安装hexo
$ npm install hexo-cli -g
# 初始化目录用于存放自己博客相关的文件
$ hexo init blog (blog:自己博客存放的目录)

Hexo 配置

1
2
3
4
5
6
$ cd blog
# 配置通过什么类型去发布自己的博客
$ vim _config.yml(最后一行)
deploy:
type: 'git'
repo: ssh://git@IP:PORT/home/git/test.git