跳至正文
首页 » 在树莓派4上搭建 Gitlab 服务器

在树莓派4上搭建 Gitlab 服务器

本文将介绍 在树莓派4上搭建 Gitlab 服务器 。相对于Github,Gitlab 提供了可以自行搭建服务的社区版本,可以摆脱网速限制更加流畅地享受 Git服务。Gitlab 消耗的资源较多,推荐使用 4GB 以上 RAM 的版本。如果性能不达标可以使用 Gogs 替代。

install-gitlab-on-raspberry-pi_gitlab-logo

1 配置交换空间(Swap)

使用 free -m 查看交换空间,推荐配置4GB以上的交换空间,以减少内存压力。若交换空间容量过少,使用 增大Linux Swap空间 中的方法创建交换空间。

2 安装和配置依赖

目前 gitlab 对树莓派仅支持官方系统( Raspberry Pi OS),其它第三方系统无法安装。

sudo apt-get install curl openssh-server ca-certificates apt-transport-https
curl https://packages.gitlab.com/gpg.key | sudo apt-key add -

添加软件源

sudo curl -sS https://packages.gitlab.com/install/repositories/gitlab/raspberry-pi2/script.deb.sh | sudo bash

安装 gitlab,将其中 https://gitlab.beekc.top 替换为实际服务器域名。gitlab会自动使用 Let’s Encrypt 申请 https 证书。

sudo EXTERNAL_URL="https://gitlab.beekc.top" apt-get install gitlab-ce

3 配置 HTTPS 证书

服务器 80 暴露在公网环境时,可以使用 Gitlab 自带的Let’s Encrypt 工具自动申请和更新证书,否则需要需要手动申请证书证书后导入。本文使用 OHTTPS 申请证书,可以避免对公网主机的需求,仅使用 DNS 即可进行验证。

OHTTPS 生成的私钥、证书文件分别命名为 cert.keycert.cer。将其重命名为gitlab.beekc.top.keygitlab.beekc.top.crt(将文件名中的域名修改为实际申请的域名),替换 /etc/gitlab/ssl 中对应的文件。

默认 Let’s Encrypt 处于开启状态,每次 Gitlab 配置都会重新申请证书,所以在应用证书前需要禁用 Let’s Encrypt 。在 /etc/gitlab/gitlab.rb 中设置 letsencrypt['enable'] = false

使用 sudo gitlab-ctl reconfigure 重新配置Gitlab,应用证书。

4 性能提升

/etc/gitlab/gitlab.rb 中设置

# Reduce the number of running workers to the minimum in order to reduce memory usage
puma['worker_processes'] = 2
sidekiq['concurrency'] = 9
# Turn off monitoring to reduce idle cpu and disk usage
prometheus_monitoring['enable'] = false

再使用 sudo gitlab-ctl reconfigure 应用配置。

5 报错 SSL certificate problem: unable to get local issuer certificate

在配置完成 Https 证书后,在浏览器中已经可以进行正常的 https 的访问,但是在使用 git 工具进行访问的时候,仍然出现了证书认证。本身是局域网的网站访问,所以也不用太在意网站认证问题,所以我在客户端中直接关闭了 git 的 SSL的认证。

当前用户

git config --global http.sslverify false

所有用户

git config --system http.sslverify false

当前仓库

git config http.sslverify false

6 配置 Nginx

本地同时搭载多个服务时,自建 web 服务器更加方便管理。

sudo apt-get install nginx

安装完成后,访问服务器 ip 就可以看到 Nginx 的欢迎页了。接下来关闭 Gitlab 服务中自带的 Nginx。编辑 /etc/gitlab/gitlab.rb,设置 nginx['enable'] = false

使用ps aux | grep nginx 查看 Nginx 所属用户。在/etc/gitlab/gitlab.rb 中设置 web_server['external_users'] = ['www-data'],其中 www-data 为 Nginx 所属用户,根据实际情况填写。

配置结束后,使用 sudo gitlab-ctl reconfigure 应用配置。

官方提供了 Nginx 配置文件,根据实际服务是 HTTP 还是 HTTPS 选择配置,将配置保存为 /etc/nginx/sites-enabled/gitlab.conf (如果 /etc/nginx/sites-enabled/default 没有删除可能会造成错误)。配置完成后,使用 nginx -s reload 更新配置。

7 故障排查

更新后网站无法访问(502)

通过查看 NGINX 错误日志,报错

2022/02/13 08:13:50 [crit] 22240#22240: *1 connect() to unix:/var/opt/gitlab/gitlab-workhorse/socket failed (2: No such file or directory) while connecting to upstream, client: 192.168.50.150, server: gitlab.beekc.top, request: "GET / HTTP/2.0", upstream: "http://unix:/var/opt/gitlab/gitlab-workhorse/socket/sockets/socket:/", host: "gitlab.beekc.top"

更新后,socket 文件更新了位置。在 NGINX 网站配置文中,将 socket 文件位置从 unix:/var/opt/gitlab/gitlab-workhorse/socket 更新至 server unix://var/opt/gitlab/gitlab-workhorse/sockets/socket

更新时签名错误

错误信息

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://packages.gitlab.com/gitlab/raspberry-pi2/raspbian buster InRelease: The following signatures were invalid: EXPKEYSIG 3F01618A51312F3F GitLab B.V. (package repository signing key) <packages@gitlab.com>
W: Failed to fetch https://packages.gitlab.com/gitlab/raspberry-pi2/raspbian/dists/buster/InRelease  The following signatures were invalid: EXPKEYSIG 3F01618A51312F3F GitLab B.V. (package repository signing key) <packages@gitlab.com>
W: Some index files failed to download. They have been ignored, or old ones used instead.

使用下面命令更新密钥

curl -s https://packages.gitlab.com/gpg.key | sudo apt-key add -

8 参考

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注