GitLab 部署和配置指南

GitLab 部署和配置指南

1. GitLab 容器配置概述

在部署 GitLab 时,可能涉及以下几部分内容:

  • 内置 Nginx 的配置和调整。
  • HTTPS 证书的申请、使用和续期。
  • 通过 FRP 映射内网服务到外部服务器。
  • 阿里云服务器的 Nginx 配置和反向代理。

本文将完整涵盖这些配置,帮助您成功部署和维护 GitLab 服务。


2. FRP 配置

2.1 FRP 客户端配置文件(群晖/内网)

使用以下示例配置文件,通过 FRP 将 GitLab 的 HTTP 和 HTTPS 服务暴露到外部。

HTTP 配置
toml 复制代码
[[proxies]]
name = "GitLab-HTTP"
type = "tcp"
local_ip = "127.0.0.1"
local_port = 6680  # 群晖上 GitLab 的 HTTP 端口
remote_port = 8080 # 阿里云服务器上的 HTTP 映射端口
HTTPS 配置
toml 复制代码
[[proxies]]
name = "GitLab-HTTPS"
type = "tcp"
local_ip = "127.0.0.1"
local_port = 6443  # 群晖上 GitLab 的 HTTPS 端口
remote_port = 8443 # 阿里云服务器上的 HTTPS 映射端口

3. 阿里云 Nginx 配置

阿里云服务器需要配置 Nginx,代理到 FRP 服务端暴露的端口。

3.1 HTTP 配置

nginx 复制代码
server {
    listen 80;
    server_name gitlab.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_connect_timeout 60s;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;
    }
}

3.2 HTTPS 配置

nginx 复制代码
server {
    listen 443 ssl;
    server_name gitlab.example.com;

    ssl_certificate /etc/letsencrypt/live/gitlab.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/gitlab.example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass https://127.0.0.1:8443;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_connect_timeout 60s;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;
    }
}

4. GitLab 容器内 HTTPS 配置

4.1 编辑 gitlab.rb 文件

gitlab.rb 是 GitLab 配置的核心文件,位于容器内的 /etc/gitlab/gitlab.rb

ruby 复制代码
# 设置外部 URL 为 HTTPS
external_url 'https://gitlab.example.com'

# 启用内置 Nginx
nginx['enable'] = true

# 客户端上传文件大小限制
nginx['client_max_body_size'] = '250m'

# 强制 HTTP 重定向到 HTTPS
nginx['redirect_http_to_https'] = true
nginx['redirect_http_to_https_port'] = 443

# SSL 证书和私钥路径
nginx['ssl_certificate'] = "/etc/letsencrypt/live/gitlab.example.com/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/gitlab.example.com/privkey.pem"

# 启用的 TLS 协议和加密算法
nginx['ssl_protocols'] = "TLSv1.2 TLSv1.3"
nginx['ssl_ciphers'] = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384"
nginx['ssl_prefer_server_ciphers'] = "on"

4.2 应用配置并重启服务

在容器中运行以下命令:

bash 复制代码
gitlab-ctl reconfigure
gitlab-ctl restart

5. 证书申请和自动续期

5.1 使用 Certbot 申请证书

停止占用端口的服务

如果 GitLab 的内置 Nginx 服务占用了 80 端口,Certbot 无法运行。需要停止 Nginx:

bash 复制代码
gitlab-ctl stop nginx
使用 Certbot 申请证书

运行以下命令生成证书:

bash 复制代码
certbot certonly --standalone -d gitlab.example.com

提示: 如果生成失败,请查看日志 /var/log/letsencrypt/letsencrypt.log

确认证书文件路径
bash 复制代码
ls /etc/letsencrypt/live/gitlab.example.com/

应包含以下文件:

  • fullchain.pem
  • privkey.pem
重新启动 Nginx 服务
bash 复制代码
gitlab-ctl start nginx

5.2 自动续期配置

检查现有证书
bash 复制代码
certbot certificates

输出示例:

复制代码
Certificate Name: gitlab.example.com
Domains: gitlab.example.com
Expiry Date: 2024-01-15 23:59:59+00:00 (VALID: 59 days)
测试续期任务
bash 复制代码
certbot renew --dry-run

成功输出示例:

复制代码
Congratulations, all renewals succeeded!
配置自动续期任务

在容器内设置定时任务:

bash 复制代码
apt install cron -y
crontab -e

添加以下内容:

复制代码
0 2 * * * certbot renew --deploy-hook "gitlab-ctl restart nginx"

启动 Cron 服务:

bash 复制代码
cron

验证任务是否正常运行:

bash 复制代码
cat /var/log/cron.log

6. 常见问题排查

问题 1:证书续期失败

  • 现象: Certbot 报错。

  • 解决方案:

    1. 检查端口占用:
    bash 复制代码
    netstat -tuln | grep :80
    1. 手动续期:
    bash 复制代码
    certbot renew

问题 2:通过 HTTP 拉取代码

  • 现象: 配置 HTTPS 后仍使用 HTTP 拉取代码。

  • 解决方案:

    修改 gitlab.rb

    ruby 复制代码
    external_url 'https://gitlab.example.com'

    然后运行:

    bash 复制代码
    gitlab-ctl reconfigure

7. 总结与注意事项

  1. 阿里云 Nginx 配置的区别:

    • HTTP 使用:proxy_pass http://127.0.0.1:8080;
    • HTTPS 使用:proxy_pass https://127.0.0.1:8443;
    • 请确保配置一致且符合实际需求,避免混淆。
  2. FRP 的 HTTP 和 HTTPS 配置需区分:

    • HTTP 和 HTTPS 使用不同的远程端口。
    • 确保 FRP 客户端和服务器配置一致,并分别映射。
  3. 申请证书前的前提条件:

    • 域名必须先能通过 HTTP 访问成功,否则无法申请证书。
    • 如果 HTTP 无法访问或 FRP 配置有误,Certbot 将报错且申请失败。
  4. 为何配置 HTTPS 的 external_url:

    • 如果 external_url 未配置为 https://,即使 HTTPS 配置成功,GitLab 界面生成的 URL 仍然是 HTTP。
    • 这会导致代码克隆或推送使用 HTTP,而不是安全的 HTTPS。
  5. 通过 HTTP 判断 FRP 服务状态:

    • 成功通过 HTTP 访问,意味着 FRP 服务端和客户端均已正常运行。
    • 进一步通过 HTTPS 测试远程访问功能。
相关推荐
一世一生命20 小时前
Windows下Gitlab多账号(3个及以上)SSH配置
运维·ssh·gitlab
裁二尺秋风1 天前
CI/CD — Pipeline的使用以及Blue Ocean多分支流水线的使用方法
ci/cd·gitlab·jenkins
北海之灵2 天前
docker desktop部署本地gitlab服务
docker·容器·gitlab
兔老大RabbitMQ2 天前
GitHub vs GitLab 全面对比报告(2025版)
gitlab·github
CC码码3 天前
管理你的多个 Git 密钥(多平台多账号)
git·gitlab·github
CC码码3 天前
管理你的多个 Git 密钥(单平台多账号)
git·gitlab·github
兔老大RabbitMQ3 天前
GitLab详细分析
gitlab
小道仙9716 天前
gitlab对接,gitlabRestApi,gitlab4j-api
java·git·gitlab
tswddd16 天前
项目:Gitlab HSD CI/CD总结
ci/cd·kubernetes·gitlab
许_安17 天前
docker中部署gitlab
docker·eureka·gitlab