部署GitLab

记录部署GitLab的过程,利用网关来反向代理。

一、规划及环境准备

1.1 规划

使用GitLab提供的Yum源,在CentOS9安装GitLab,并且使用另一台Nginx作为网关,同时使用网关的 2222 端口反代 GitLab 的22 端口。

主机名 Nginx-Server GitLab-Server
系统版本 CentOS 9 Stream CentOS 9 Stream
IP地址 192.168.10.5 192.168.10.199
开放端口 80、443、2222 80、22
配置 2C、2G、20GB 2C、4G、60GB
域名 gitlab.qiaoxiong.cc
用途 网关 GitLab服务器

1.2 环境准备

1.2.1 配置清华源

Nginx-Server、GitLab-Server 都需要的配置。(可选)

二、安装GitLab

官方文档

  1. 选择社区版的 YUM
1
curl "https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh" | sudo bash
  1. 设置域名的同时安装
    注意:使用 http 而不是 https ,由Nginx进行 SSL证书相关的配置。
1
sudo EXTERNAL_URL="http://gitlab.qiaoxiong.cc" dnf install gitlab-ce
  1. 编辑配置文件
1
vim /etc/gitlab/gitlab.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1. 这里必须填 HTTPS,因为这是给用户看的地址,也是 Clone 代码的地址
external_url 'https://gitlab.qiaoxiong.cc'

# 2. 告诉 GitLab 自带的 Nginx:虽然上面写了 https,但你别开 SSL,别找证书
nginx['listen_https'] = false

# 3. 强制让它监听 80 端口 (否则它看见 https 就会默认去听 443)
nginx['listen_port'] = 80

# 4. 这一步非常关键!告诉 GitLab 相信来自 Gateway 的流量
# 否则它会觉得 "这流量怎么是 HTTP 进来的?不安全!" 然后无限重定向
# 把下面的 IP 换成你那台 Nginx Gateway 的内网 IP
nginx['trusted_proxies'] = ['192.168.10.xx']


### gitlab_rails['trusted_proxies'] = ['192.168.10.5']
### 实际上我找到的这个

# 5. 告诉 GitLab,外网用户访问 SSH 时,请显示这个端口
gitlab_rails['gitlab_shell_ssh_port'] = 2222
  1. 重载配置
1
gitlab-ctl reconfigure
  1. 放行防火墙,并立即刷新
1
2
firewall-cmd --permanent \
--add-rich-rule='rule family="ipv4" source address="192.168.10.5" port port="80" protocol="tcp" accept'
1
firewall-cmd --reload

三、安装Nginx并安装

3.1 安装Nginx

  1. 设置为Nginx官方维护的 YUM
1
vi /etc/yum.repos.d/nginx.repo
1
2
3
4
5
6
7
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
  1. 安装Nginx
1
yum install nginx

3.2 申请SSL证书

  1. 创建文件夹
1
mkdir -p /etc/nginx/ssl/qiaoxiong.cc
  1. 下载 acme.sh,用于自动申请证书
1
wget https://g.bravexist.cn/https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh
  1. 添加可执行权限
1
chmod +x acme.sh
  1. 安装 acme.sh
1
./acme.sh --install -m admin@qiaoxiong.cc
  1. 使用Cloudflare DNS验证,补齐缺失的脚本(不能直连Github在线安装)
1
mkdir -p ~/.acme.sh/dnsapi
1
curl -o dnsapi/dns_cf.sh https://g.bravexist.cn/https://raw.githubusercontent.com/acmesh-official/acme.sh/master/dnsapi/dns_cf.sh
1
chmod +x ~/.acme/dnsapi/dns_cf.sh
  1. 生成Cloudflare的Token,并写入内存环境变量
1
2
3
4
5
# 1. 设置 Token 专用变量
export CF_Token="xxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxx-xxxxx"

# 2. 设置 Account ID
export CF_Account_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  1. 申请泛域名证书,找不到脚本的话,刷新一下环境变量
1
. .bashrc
1
acme.sh --issue --server letsencrypt --dns dns_cf -d qiaoxiong.cc -d '*.qiaoxiong.cc' --debug
  1. 安装证书到之前创建的文件夹
1
2
3
4
acme.sh --install-cert -d qiaoxiong.cc \
--key-file /etc/nginx/ssl/qiaoxiong.cc/qiaoxiong.key \
--fullchain-file /etc/nginx/ssl/qiaoxiong.cc/fullchain.cer \
--reloadcmd "systemctl force-reload nginx"

3.3 设置反向代理

需要反向代理 ssh、http两种服务。

3.3.1 四层反向代理

  1. 添加Stream模块
1
vim /etc/nginx/nginx.conf
1
2
3
4
# 和http块平级
stream{
include /etc/nginx/stream.conf.d/*.conf;
}
  1. 创建 Stream子配置目录
1
mkdir /etc/nginx/stream.conf.d
  1. 配置四层反向代理,代理SSH服务
1
vim /etc/nginx/stream.conf.d/stream_gitlab_qiaoxiong_cc.conf
1
2
3
4
5
6
7
8
9
10
upstream gitlab_ssh_backend {
server 192.168.10.199:22;
}

server {
listen 2222;
proxy_pass gitlab_ssh_backend;
proxy_connect_timeout 1h;
proxy_timeout 1h;
}

3.3.2 七层反向代理

  1. 编辑配置文件
1
vim /etc/nginx/conf.d/gitlab_qiaoxiong_cc.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
# 1. 监听 80 端口,强制跳转到 HTTPS
server {
listen 80;
server_name gitlab.qiaoxiong.cc;
return 301 https://$host$request_uri;
}


server {
listen 443 ssl;
http2 on;
server_name gitlab.qiaoxiong.cc;

# SSL 证书路径 (刚才 acme.sh 安装的位置)
ssl_certificate /etc/nginx/ssl/qiaoxiong.cc/fullchain.cer;
ssl_certificate_key /etc/nginx/ssl/qiaoxiong.cc/qiaoxiong.key;

# SSL 协议优化 (工业级配置,可以直接抄)
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA2
0-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;

location / {
# 指向 GitLab 机器的 80 端口
proxy_pass http://192.168.10.199:80;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# 告诉 GitLab:最外面是 HTTPS 的!(配合 gitlab.rb 里的设置)
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
}
}

3.4 配置SELinux

  1. 检查 2222 端口的标签
1
semanage port -l | grep 2222
  1. 安装 semanage
1
2
dnf provides "semanage"
dnf install policycoreutils-python-utils-3.6-3.el9.noarch
  1. 给端口设置标签
1
semanage port -a -t http_port_t -p tcp 2222
  1. 允许Nginx发起网络连接
1
setsebool -P httpd_can_network_connect 1

3.5 配置防火墙

1
firewall-cmd --permanent --add-port=80/tcp --add-port=443/tcp --add-port=2222/tcp
1
firewall-cmd --reload

3.6 检查nginx配置及启动测试访问

1
nginx -t
1
systemctl restart nginx

四、验证

设置hosts文件

1
192.168.10.5 gitlab.qiaoxiong.cc

默认账号密码

1
root
1
cat /etc/gitlab/initial_root_password

五、遇到的问题

5.1 SSH无法连接

CentOS9 安装openssl ,重启机器后,sshd 无法启动。

1
2
3
4
5
# 升级前版本
OpenSSL 3.0.1 14 Dec 2021 (Library: OpenSSL 3.0.1 14 Dec 2021)

# 升级后版本
OpenSSL 3.5.1 1 Jul 2025 (Library: OpenSSL 3.5.1 1 Jul 2025)

排错
sshd放到前台,并使用调试模式。

1
sshd -D -d

输出信息

1
OpenSSL version mismatch. Built against 30000010, you have 30500010

解决:升级openssh-server

1
yum install openssh-server -y

补充

  1. 依赖更新了:你可能执行了 dnf update,系统更新了底层的 openssl-libs 库。
  2. 主程序没跟上:但是 openssh-server 的二进制文件还是旧的,或者内存里运行的 sshd 进程还是基于旧库加载的。
  3. ABI 不兼容:SSHD 启动时(或 fork 子进程处理连接时)会校验链接的 OpenSSL 库版本。如果发现编译时的版本(Built against)和当前系统库的版本(you have)差异过大(主版本号变了,或者接口变了),出于安全考虑(防止调用错误的内存地址),它会选择自杀

六、参考资料