0%

hexo博客部署到服务器

安装git

在Centos系统上安装git

1
yum install git

检测是否安装成功

1
2
$ git --version
git version 1.8.3.1

添加git用户

1
adduser git

添加公钥

1
2
3
4
5
6
7
8
su git
cd ~
mkdir .ssh
chmod 777 .ssh
cd .ssh
touch authorized_keys
chmod 644 authorized_keys
vim authorized_keys

将公钥id_rsa.pub复制进去
在这里插入图片描述
禁止git用户使用ssh登陆

1
vim /etc/passwd

修改

1
git:x:1000:1000::/home/git:/bin/sh

1
git:x:1000:1000::/home/git:/usr/bin/git-shell

配置git仓库

1
2
mkdir /var/repo
cd /var/repo

初始化git仓库

1
git init --bare blog.git

赋予git用户权限

1
chown -R git:git blog.git

通过git hook将仓库的内容共享到*/data/www/zhanghanlun*目录,
在hook目录下添加post-receive文件

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

添加如下内容

1
2
#!/bin/sh
git --work-tree=/data/www/zhanghanlun --git-dir=/var/repo/blog.git checkout -f

赋予可执行权限

1
chmod +x post-receive

创建/data/www/zhanghanlun并改变该文件夹的用户为git

1
2
3
mkdir /data/www/zhanghanlun
cd /data/www
chown -R git:git zhanghanlun

配置nginx

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
64
65
66
server
{
listen 80;
listen 443 ssl http2;
server_name www.zhanghanlun.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/www.zhanghanlun.com;

#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
ssl_certificate /www/server/panel/vhost/cert/www.zhanghanlun.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/www.zhanghanlun.com/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;

#SSL-END

#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END

#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-56.conf;
#PHP-INFO-END

#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/www.zhanghanlun.com.conf;
#REWRITE-END

#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}

#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}

location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
access_log /www/wwwlogs/www.zhanghanlun.com.log;
error_log /www/wwwlogs/www.zhanghanlun.com.error.log;
}

修改hexo配置文件

修改_config.yml

1
2
3
4
deploy:
type: git
repo: git@IP:/var/repo/blog.git
branch: master
原创技术分享,您的支持将鼓励我继续创作。