centos7搭建个人的git pages

Git Pages是结合git服务操作的web网页托管平台。通过git提交再结合git hook脚本就能很好的将提交的文件上传到web服务虚拟目录里。但是Github、Netlify、Coding等已经提供了免费git pages服务,为什么还要自己在vps上折腾搭建git pages呢?因为这些服务商提供的git pages是有限制的,比如空间容量相对较小、对动态网页支持不完善或者没有、访问速度较慢等。那么自建的git pages的优势就显现出来了。下面就介绍怎么一步步搭建该服务。

1、安装Git

1
2
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
yum install git

接下来我们创建一个git用户组和用户,用来运行git服务:

1
2
3
4
5
6
7
#创建git用户组和git用户
groupadd git
useradd git -g git

#修改git用户的shell类型,设置为git-shell以使其活动限制在Git操作相关范围内,从而禁止git用户ssh登录进入系统。
vim /etc/passwd
git:x:1000:1000::/home/git:/usr/bin/git-shell

2、配置ssh秘钥登录

收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件中,把我们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。

如果没有该文件创建它:

1
2
3
4
5
mkdir -p  /home/git/.ssh
chmod 755 /home/git/.ssh
touch /home/git/.ssh/authorized_keys
chmod 644 /home/git/.ssh/authorized_keys
chown -R git:git /home/git

客户机

1
2
3
4
#copy客户端的秘钥到vps的/home/git/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub
#查看ssh秘钥是否配置成功
ssh -T git@服务器ip

3、搭建git仓库

1
2
3
mkdir -p /opt/blog/git
git init --bare /opt/blog/git/blog.git
vim /opt/blog/git/blog.git/hooks/post-receive
1
2
#!/bin/bash
git --work-tree=/opt/blog/web --git-dir=/opt/blog/git/blog.git checkout -f
1
2
3
4
5
chown -R git:git /opt/blog/git && chgrp -R git /opt/blog/git && chmod -R 755 /opt/blog/git
chmod +x /opt/blog/git/blog.git/hooks/post-receive

mkdir -p /opt/blog/web
chmod -R 777 /opt/blog/web

4、搭建web服务

需要安装好nginx,可以参考我的另一篇文章《nginx学习笔记》

这里已经提前通过编译安装的方式把nginx安装到了/opt/nginx目录,然后只需配置nginx即可。

1
vim /opt/nginx/conf/conf.d/blog.conf
1
2
3
4
5
6
7
8
server {
listen 80;
server_name qcmoke.site; #填写个人域名
location / {
root /data/blog; #配置web根目录
index index.html;
}
}
1
2
3
4
#启动nginx(如果没有启动的话)
/opt/nginx/sbin/nginx
#重新加载nginx配置文件
/opt/nginx/sbin/nginx -s reload

5、配置https服务

使用 https 就需要配置 SSL 证书 ,SSL证书有免费的和付费的,这里使用的是免费的 Let’ s Encrypt SSL证书。

具体操作参考《通过certbot工具生成ssl证书》。参考该教程使用 certbot 工具通过HTTP 验证模式申请签发多域名证书。

配置nginx web服务

1
vim /opt/nginx/conf/conf.d/blog.conf

需求:http://qcmoke.sitehttp://www.qcmoke.sitehttps://qcmoke.site都重定向到https://www.qcmoke.site,并且图片都能压缩传输。

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

server {
server_name qcmoke.site;
listen 80;
listen 443 ssl;
root html;

# 配置域名所属的SSL证书和私钥文件
ssl_certificate /etc/letsencrypt/live/qcmoke.site/fullchain.pem; # 此处由于使用了多域名证书,故可多域名可共用同一个证书
ssl_certificate_key /etc/letsencrypt/live/qcmoke.site/privkey.pem;

# certbot 工具通过HTTP 验证模式申请证书,Let's Encrypt CA(ACME 服务器)需要验证 .well-known/acme-challenge 目录下的临时文件以确定申请用户对域名的所有权
# 处理 .well-known/acme-challenge/ 目录下的文件,如果访问的文件存在,则直接返回该文件的内容;否则返回404状态码
location ^~ /.well-known/acme-challenge/ {
try_files $uri =404;
}

# 其他请求全部跳转到 HTTPS
location / {
#rewrite ^ https://www.$server_name$request_uri? permanent;
#rewrite ^(.*)$ https://www.$server_name$1 permanent;
return 301 https://www.$server_name$request_uri;
}
}


server {
server_name www.qcmoke.site;
listen 80;
root html;

location ^~ /.well-known/acme-challenge/ {
try_files $uri =404;
}
location / {
#rewrite ^ https://$server_name$request_uri? permanent;
#rewrite ^(.*)$ https://$server_name$1 permanent;
return 301 https://$server_name$request_uri;
}
}

server {
listen 443 ssl;
server_name www.qcmoke.site;
charset utf-8;
root /opt/blog/web;
index index.html index.htm;


location ~ .*\.(jpg|png|gif)$ {
# root /opt/blog/web/images;
#传输压缩,压缩本身比较耗费服务端性能,但给带宽带来更好的传输。恰当的使用会增强资源的访问效率。
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
#压缩的文件类型,一般按需选择,但这里为了未来方便添加文件类型多选一些。具体配置参考文件/etc/nginx/mime.types
gzip_types gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#设置静态资源文件在客户端的缓存时间,除非客户清楚缓存或者关闭缓存或者强制访问才会再访问。
expires 5h;
}

#access_log /var/log/nginx/demo.mydomain.com_access.log;
#error_log /var/log/nginx/demo.mydomain.com_error.log;

# 配置域名所属的SSL证书和私钥文件
ssl_certificate /etc/letsencrypt/live/qcmoke.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/qcmoke.site/privkey.pem;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets on;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 一般推荐使用的ssl_ciphers值: https://wiki.mozilla.org/Security/Server_Side_TLS
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128:AES256:AES:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK';
ssl_prefer_server_ciphers on;

}

重载配置

1
/opt/nginx/sbin/nginx -s reload

6、测试

客户机

1
2
3
4
5
6
git clone git@qcmoke.site:/opt/blog/git/blog.git
cd blog/
echo "<h1>Qcmoke Bolg</h1>" >> index.html
git add .
git commit -m "init my blog"
git push -u origin master

之后浏览器访问http://qcmoke.site就能访问到push到服务器的index.html页面了。

📚 参考

https://www.jianshu.com/p/23aa1eef5b23

https://juejin.im/post/5c935d7c6fb9a070b24b11a6



----------- 本文结束 -----------




如果你觉得我的文章对你有帮助,你可以打赏我哦~
0%