Linux 搭建个人的 gitpages 服务

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

1. 搭建 git 服务器仓库

1)安装 git

1
2
apt install -y git
yum install -y git

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

1
2
3
4
#创建git用户组
groupadd git
#创建git用户并设置登录的shell为git-shell(通过设置git-shell,用户可以通过SSH连接到系统,但只能与远程仓库进行Git交互,而不能执行其他命令,从而确保git用户操作的安全性)
useradd git -g git -s /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

配置客户端的 ssh key 到服务端中,操作如下:

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)其他方式搭建 git 服务器仓库

以上方式是通过 git 原始支持的方式搭建 git 服务器仓库,此外还可以通过搭建开源的 git 仓库托管平台(如:Gitlab、Gogs 等)来创建服务器仓库,其使用体验会更好。

对于在 Gogs 中使用 Git 钩子来部署网站,则操作如下(设 Gogs 是通过 Docker 安装的):

1
2
3
4
5
docker exec -it gogs bash
rm -rf /opt/cache/myblog
mkdir -p /opt/cache/myblog
chmod -R 777 /opt/cache/myblog
exit

可在 Gogs 后台面板中的对应仓库,并找到“管理 Git 钩子”,修改 post-receive,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
#
# An example hook script for the "post-receive" event.
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master

while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" = "$branch" ]; then
# Do something
git --work-tree=/opt/cache/myblog --git-dir=/data/git/gogs-repositories/qcmoke/myblog.git checkout -f
fi
done

提示:以上的 /data/git/gogs-repositories/qcmoke/myblog.git 是对应仓库在 Gogs 容器中的路径,其中 /data/git/gogs-repositories 是固定的,而 /qcmoke/myblog.git 则需根据自己的情况进行修改。

2. 搭建 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 example.com; #填写个人域名
location / {
root /data/blog; #配置web根目录
index index.html;
}
}
1
2
3
4
#启动nginx(如果没有启动的话)
nginx
#重新加载nginx配置文件
nginx -s reload

3. 配置 https 服务

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

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

配置nginx web服务

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

需求:http://example.comhttp://www.example.comhttps://example.com都重定向到https://www.example.com,并且图片都能压缩传输。

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
server {
listen 80;
listen 443 ssl;
server_name example.com;
# 配置域名所属的SSL证书和私钥文件
ssl_certificate /path/fullchain.pem;
ssl_certificate_key /path/privkey.pem;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
charset utf-8;

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

server {
listen 80;
listen 443 ssl;
server_name www.example.com;
# 配置域名所属的SSL证书和私钥文件
ssl_certificate /path/fullchain.pem;
ssl_certificate_key /path/privkey.pem;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
charset utf-8;

root /opt/blog/web;
#root /opt/cache/myblog;
index index.html index.htm;
error_page 404 /404.html;

# 请求全部跳转到 https
if ($scheme = http) {
return 301 https://$host$request_uri;
}

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;
}
}

重载配置

1
nginx -s reload

4. 测试

客户机

1
2
3
4
5
6
git clone [email protected]:/opt/blog/git/blog.git
cd blog/
echo "<h1>My Bolg</h1>" >> index.html
git add .
git commit -m "init my blog"
git push -u origin master

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

📚 参考

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

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



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




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