适用于:✅ RHEL 10, ✅ RHEL 9, ✅ RHEL 8 及其衍生版


I. 环境配置

1.1 开放相关端口

要打开所需的防火墙端口(80、443、22)并能够访问 GitLab:

1.启用并启动 OpenSSH 服务器守护进程:

1sudo systemctl enable --now sshd

2.打开防火墙相关服务端口:

1sudo firewall-cmd --permanent --add-service=http
2sudo firewall-cmd --permanent --add-service=https
3sudo firewall-cmd --permanent --add-service=ssh
4sudo systemctl reload firewalld

II. 安装 GitLab

2.1 在线安装

2.1.1 添加包存储库

1.安装所需软件包:

1sudo dnf install -y curl

2.使用以下脚本添加 GitLab 仓库:

  • 企业版
1curl "https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh" | sudo bash
  • 社区版
1curl "https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh" | sudo bash

2.1.2 安装 GitLab

使用您系统的软件包管理器安装 GitLab。

  • 企业版
1sudo dnf install gitlab-ee
  • 社区版
1sudo dnf install gitlab-ce

2.2 离线安装

2.2.1 支持平台

GitLab 为不同平台提供 Linux 软件包,建议下载前查看不同平台官方最后支持的 GitLab 版本

2.2.2 最新版本

如果你当前使用的平台未超过官方最后支持的 GitLab 版本,建议安装官方最新发布的版本

2.2.3 下载离线包

GitLab 仓库 筛选你计划安装的离线安装包并下载。

PIC_20260129_212700.png

2.2.4 安装依赖

1.查看当前版本 GitLab 需要哪些依赖包👇

PIC_20260129_213131.png

2.查看安装平台是否已经安装依赖包

1dnf list installed <软件名>

3.安装依赖包

1sudo dnf install -y perl-interpreter policycoreutils-python-utils

2.2.5 安装 GitLab

1# 请将文件名替换为你实际下载的文件名
2sudo dnf install gitlab-ee-18.x.x-ee.0.el8.x86_64.rpm

2.3 查看 GitLab 状态

1systemctl status gitlab-runsvdir

III. 配置 GitLab

3.1 配置 URL

官方文档:Configuration options for Linux package installations | GitLab Docs


编辑 /etc/gitlab/gitlab.rb 文件,将 external_url 修改为您首选的 URL 。

1external_url 'https://gitlab.example.com'

3.2 配置 SSL

官方文档:Configure SSL for a Linux package installation | GitLab Docs

中文文档:NGINX settings - GitLab Omnibus - GitLab 文档中心

3.3 完整配置

本文使用 Nginx 反向代理 GitLab ,由于外部 Nginx 会处理 SSL,我们需要让内置 Nginx 只监听 HTTP 请求。

 1# GitLab 对外访问地址(必须是公网域名)
 2external_url 'https://gitlab.example.com'
 3
 4# 禁用 Let’s Encrypt 集成功能
 5letsencrypt['enable'] = false
 6
 7################################################################################
 8## GitLab NGINX
 9##! Docs: https://docs.gitlab.com/omnibus/settings/nginx.html
10################################################################################
11
12# 告知 GitLab 它处于反向代理之后, 用于接收外部 Nginx 转发的流量
13nginx['listen_port'] = 80
14nginx['listen_https'] = false
15
16# 允许 GitLab 接收代理头信息
17nginx['proxy_set_headers'] = {
18  "X-Forwarded-Proto" => "https",
19  "X-Forwarded-Ssl" => "on"
20}
21
22# 添加反向代理 IP(需修改)
23nginx['real_ip_trusted_addresses'] = [ '172.16.18.11' ]
24
25# 其他 real_ip 配置选项
26nginx['real_ip_header'] = 'X-Forwarded-For'
27nginx['real_ip_recursive'] = 'on'
28
29################################################################################
30## gitlab.yml configuration
31##! Docs: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/gitlab.yml.md
32################################################################################
33
34## 按需配置 SSH,并做好暴露 SSH 安全防护!
35
36# 配置 ssh 地址
37gitlab_rails['gitlab_ssh_host'] = 'gitlab.example.com'
38
39# 允许接收来自前置 Nginx 的代理头信息, 填写前置代理 IP
40gitlab_rails['trusted_proxies'] = ['172.16.18.11']
41
42# SSH 端口(如需改)
43gitlab_rails['gitlab_shell_ssh_port'] = 56343
44
45# 服务监听方式
46gitlab_workhorse['listen_network'] = "tcp"
47
48# 服务监听地址
49gitlab_workhorse['listen_addr'] = "127.0.0.1:50080"

📢注意:GitLab 会在每次重新配置时尝试更新任何 Let’s Encrypt 证书。若您计划使用自行手动创建的证书,必须禁用 Let’s Encrypt 集成功能,否则自动更新机制可能导致证书被覆盖。

3.3 应用配置

1sudo gitlab-ctl reconfigure

3.4 检查服务状态

1sudo gitlab-ctl status

💡Tips:systemctlgitlab-ctl 的关系

在 GitLab 的架构中,systemctl 处于最高层级,而 gitlab-ctl 是它的“项目经理”。

  • systemctl:负责管理 GitLab 的“总开关”(即 runsvdir 守护进程)。如果这个总开关关了,GitLab 的所有子服务(Nginx, Redis, PostgreSQL 等)都会全部强制停止。

  • gitlab-ctl:这是 GitLab 自带的管理工具,它通过 runit 监控并管理内部的 20 多个子服务。

IV. SSL 证书申请

你可以通过 Certbotacme.sh 申请 SSL 证书,本文不再赘述。

V. Nginx 配置

📢注意:GitLab 服务器和 Nginx 服务器需在防火墙放行相关端口。如果是云服务器(如阿里云、腾讯云),除了系统内部防火墙,还需要在云平台的安全组/防火墙设置中放行相关端口。

5.1 配置 Web 代理

/etc/nginx/conf.d/gitlab.conf 中创建:

 1upstream gitlab-workhorse {
 2    # GitLab 服务器的内网 IP 和端口
 3    server 172.16.18.12:80;
 4}
 5
 6# HTTP 重定向到 HTTPS
 7server {
 8    listen 80;
 9    server_name gitlab.example.com;
10    # 强制跳转 HTTPS
11    return 301 https://$server_name$request_uri;
12}
13
14# HTTPS 代理
15server {
16    listen 443 ssl;
17    server_name gitlab.example.com;
18
19    # SSL 证书路径
20    ssl_certificate /path/to/fullchain.pem;
21    ssl_certificate_key /path/to/privkey.pem;
22
23    # 其它 SSL 配置
24    ssl_session_timeout   60m;
25    ssl_session_cache     shared:MozSSL:10m;
26    ssl_protocols         TLSv1.2 TLSv1.3;
27    ssl_ciphers           ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
28    ssl_prefer_server_ciphers on;
29
30    # 关键代理参数
31    location / {
32        proxy_pass http://gitlab-workhorse;
33        proxy_set_header Host $http_host;
34        proxy_set_header X-Real-IP $remote_addr;
35        proxy_set_header X-Forwarded-Proto $scheme;
36        # 注意,反向代理一定需要该项,否则会出现登录 422 的问题!
37        proxy_set_header X-Forwarded-Ssl on;
38        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
39
40        # 增加上传限制,否则无法推送大型 Git 仓库
41        client_max_body_size 500m;
42    }
43}

5.2 配置 SSH 代理

这是开启 SSH 转发的关键。编辑 Nginx 主配置文件 /etc/nginx/nginx.conf。注意:stream 块必须在 http 块之外。

 1user nginx;
 2worker_processes auto;
 3# ... 其他配置 ...
 4
 5http {
 6    # 这里是原本的 http 配置,包含上面的 include /etc/nginx/conf.d/*.conf;
 7}
 8
 9# 添加以下 stream 块进行 TCP 转发
10stream {
11    upstream gitlab_ssh_upstream {
12        # 转发到 GitLab 服务器的 SSH 端口
13        server 172.16.18.12:56343;
14    }
15
16    server {
17        # 互联网访问的 SSH 端口
18        listen 53742;
19        proxy_pass gitlab_ssh_upstream;
20        proxy_connect_timeout 60s;
21        proxy_timeout 300s;
22    }
23}

5.3 测试并重启 Nginx

1sudo nginx -t
2sudo systemctl restart nginx

VI. 初始登录

6.1 初始密码

GitLab 为 root 管理员账户生成随机密码和电子邮件地址,并将其存储在 /etc/gitlab/initial_root_password 文件中,保存时限为 24 小时。出于安全考虑,该文件将在 24 小时后自动删除。

1sudo cat /etc/gitlab/initial_root_password

6.2 初始登录

安装 GitLab 后,请访问您设置的 URL,并使用以下凭据登录:

  • 用户名:root

  • 密码:请查看 /etc/gitlab/initial_root_password


📢注意:登录后,请修改您的密码和电子邮件地址。

VII. 后续步骤

参考文档:Steps after installing GitLab | GitLab Docs

重点关注:Secure GitLab | GitLab Docs


7.1 关闭遥测数据

登录后进入 Admin Area -> Settings -> Metrics and profiling -> Usage statistics。

取消勾选 Enable usage ping。

VIII. 参考文档

Installation methods | GitLab Docs

Offline GitLab | GitLab Docs

通过 Nginx 代理实现互联网访问…GitLab