本地配置

配置用户名邮箱

1
2
3
#配置过Git的请省略以下两步
git config --global user.name "你的用户名"
git config --global user.email "你的电子邮箱"

配置SSH公钥

1
2
3
4
cd ~
mkdir .ssh
cd .ssh
ssh-keygen -t rsa

服务器配置Git库

安装Git

1
2
# CentOS用yum安装
sudo apt-get install git

建立网站目录

1
2
3
4
5
# 我的博客目录在/home/www/jx,你们自行修改成自己目录
cd /home
mkdir www
cd www
mkdir jx

在WWW文件夹里建议Git库

1
git init --bare jx.git

配置 Git Hooks

1
cd jx.git/hooks
编辑post-receive文件:
1
vim post-receive
1
2
3
4
5
6
7
8
9
10
# Git仓库
GIT_REPO=/home/www/jx.git
# 临时目录
TMP_GIT_CLONE=/tmp/hexo
# 网站目录
PUBLIC_WWW=/home/www/jx
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
赋予执行权限
1
chmod +x post-receive

服务器安装与配置Nginx

安装Nginx

1
2
# CentOS用yum安装
sudo apt-get install nginx

配置Nginx

1
vim /etc/nginx/nginx.conf
我的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
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
# 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/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 2048;

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;

# 上面可以不用管,以下是重点

# Http服务,server_name 后填域名,一个空格一个,root填网站根目录

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.jixian.io jixian.io;
root /home/www/jx;


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

location / {
}

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

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


# 如果没有SSL证书或者不打算配Https,以下请忽略,也不用复制进去!!!!
# 我的是腾讯云免费的SSL证书,在腾讯云下载证书后我把crt和key文件放到了/etc/nginx/zs/目录下

# Settings for a TLS enabled server.
#
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name jixian.io www.jixian.io;
root /home/www/jx;

ssl_certificate "/etc/nginx/zs/wwwjixianio.crt";
ssl_certificate_key "/etc/nginx/zs/wwwjixianio.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

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

location / {
}

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

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

}

重启Nginx

1
service nginx restart

上传本地公钥

1
ssh-copy-id -i ~/.ssh/id_rsa.pub root@服务器ip地址

Hexo配置文件

1
2
3
4
deploy:
type: git
repo: root@你的IP:/home/www/jx.git
branch: master