温馨提示×

Linux GitLab如何配置SSL

小樊
45
2026-07-14 11:59:52
栏目: 云计算

在 Linux 上给 GitLab 配置 SSL(HTTPS),常见有三种方式,按推荐程度从高到低说明。下面以 Omnibus GitLab(官方包安装) 为主,这是最常见、最简单的方式。


一、使用 Let’s Encrypt(推荐 ✅)

适合:公网服务器、有域名、80/443 端口可访问。

1️⃣ 前提条件

  • 已安装 GitLab(Omnibus)
  • 有公网域名,如 gitlab.example.com
  • 域名已解析到服务器 IP
  • 80 / 443 端口未被占用

2️⃣ 修改 GitLab 配置

编辑配置文件:

sudo vim /etc/gitlab/gitlab.rb

修改或添加:

external_url "https://gitlab.example.com"

letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']
letsencrypt['auto_renew'] = true

3️⃣ 使配置生效

sudo gitlab-ctl reconfigure

GitLab 会自动:

  • 申请 Let’s Encrypt 证书
  • 配置 Nginx
  • 自动续期(cron)

4️⃣ 验证

浏览器访问:

https://gitlab.example.com

二、使用自定义 SSL 证书(企业 / 内网常用 ✅)

适合:

  • 内网 GitLab
  • 使用企业 CA
  • 已有证书(.crt + .key

1️⃣ 准备证书文件

假设:

  • 证书:gitlab.crt
  • 私钥:gitlab.key

放到安全目录:

sudo mkdir -p /etc/gitlab/ssl
sudo cp gitlab.crt gitlab.key /etc/gitlab/ssl/
sudo chmod 600 /etc/gitlab/ssl/gitlab.key

2️⃣ 修改 GitLab 配置

sudo vim /etc/gitlab/gitlab.rb
external_url "https://gitlab.example.com"

nginx['enable'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.key"

3️⃣ 应用配置

sudo gitlab-ctl reconfigure

4️⃣ HTTP 自动跳转 HTTPS(可选)

nginx['redirect_http_to_https'] = true

三、使用自签名证书(不推荐 ⚠️)

仅用于测试环境。

生成自签名证书

sudo openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/gitlab/ssl/gitlab.key \
  -out /etc/gitlab/ssl/gitlab.crt

然后按 方式二 配置即可。

⚠️ 浏览器会提示不安全,Git 客户端需额外配置信任。


四、常见问题排查

❌ 80 / 443 端口被占用

sudo netstat -tulnp | grep :80

❌ Let’s Encrypt 申请失败

原因:

  • 域名未解析
  • 防火墙未放行 80/443
  • 服务器在内网

✅ 解决方案:

sudo ufw allow 80
sudo ufw allow 443

❌ Git 客户端不信任证书

git config --global http.sslVerify false

(仅测试环境)


五、证书自动续期(Let’s Encrypt)

Omnibus GitLab 已内置:

sudo gitlab-ctl renew-le-certs

六、总结

场景 推荐方式
公网服务器 ✅ Let’s Encrypt
内网 / 企业 ✅ 自定义证书
本地测试 ⚠️ 自签名

如果你愿意,可以告诉我:

  • ✅ GitLab 安装方式(Omnibus / Docker / 源码)
  • ✅ 是否公网
  • ✅ 使用的 Linux 版本

我可以给你 精确到命令级别 的配置方案。

0