Docker下Gitlab配置Let’s Encrypt证书

1 参考文档

参考1:极狐GitLab SSL证书管理
参考2:Configure SSL for the GitLab Linux package

参考1是参考2是官方文档的中文简略配置。

2 常见问题

2.1 前置条件

  1. 配置的域名,在公网上可以解析到安装GitLab实例的服务器的IP地址上,且Let's Encrypt服务端可以访问到这个IP地址进行验证;
  2. 只能通过标准的80443端口进行访问,非标准端口不支持;

2.2 不支持ip

Let’s Encrypt 不支持IP形式的地址,只能是域名。否则在更新配置后会报错。

Running handlers:
[2023-05-15T04:50:40+00:00] ERROR: Running exception handlers
There was an error running gitlab-ctl reconfigure:

letsencrypt_certificate[192.168.3.220] (letsencrypt::http_authorization line 6) had an error: Acme::Client::Error::RejectedIdentifier: acme_certificate[staging] (letsencrypt::http_authorization line 43) had an error: Acme::Client::Error::RejectedIdentifier: Error creating new order :: Cannot issue for "192.168.3.220": The ACME server can not issue a certificate for an IP address

2.3 重复签发

Let’s Encrypt第一次对新域名签发正常,重复对同一域名签发可能报错。

Running handlers:
[2023-05-15T06:08:02+00:00] ERROR: Running exception handlers
There was an error running gitlab-ctl reconfigure:

letsencrypt_certificate[真实域名] (letsencrypt::http_authorization line 6) had an error: RuntimeError: acme_certificate[staging] (letsencrypt::http_authorization line 43) had an erro RuntimeError: ruby_block[create certificate for 真实域名] (letsencrypt::http_authorization line 110) had an error: RuntimeError: [真实域名] Validation failed, unable to requt certificate, Errors: [{url: https://acme-staging-v02.api.letsencrypt.org/acme/chall-v3/6491015484/2gCP-Q, status: invalid, error: {"type"=>"urn:ietf:params:acme:error:connection", "detail""域名解析的公网ip: Fetching http://真实域名/.well-known/acme-challenge/10-MktL8xuOo53dqQY3aV1j8Q_9qtUsY19fE-kF9uBU: Connection refused", "status"=>400}} ]

2.4 外网无法访问

因为Gitlab启动较慢,所以可以排查一下是否是Gitlab尚未启动导致的。

※3 内网穿透配置(可选)

frp:

#本地服务
[gitlab]
type = tcp
local_ip = 192.168.3.220
local_port = 443
remote_port = 443
custom_domains = gitlab.domain.com

4 Gitlab 创建并配置Let’s Encrypt证书

因为是用Docker部署的Gitlab来配置Let’s Encrypt证书。这里不需要手动申请Let’s Encrypt证书,只需要在Gitlab的配置正确,Let’s Encrypt证书会自动签发。

Docker搭建配置Gitlab基础上做部分修改即可,这里仅对差别部分做修改记录。

4.1 开放Let’s Encrypt签发所需端口

先开放gitlab使用的linux端口。

  1. 开放服务器端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
  1. 重启防火墙
firewall-cmd --reload
  1. 查询有哪些端口是开启的
firewall-cmd --list-port

4.2 新增存储HTTPS证书文件夹

创建cert文件夹用来存储HTTPS证书。
在这里插入图片描述

4.3 启动Gitlab容器

docker run -itd  \
 -p 443:443 \
 -p 80:80 \
 -p 9922:22 \
 -v /opt/soft/docker/gitlab/cert:/etc/gitlab/ssl \
 -v /opt/soft/docker/gitlab/etc:/etc/gitlab  \
 -v /opt/soft/docker/gitlab/log:/var/log/gitlab \
 -v /opt/soft/docker/gitlab/opt:/var/opt/gitlab \
 --restart always \
 --privileged=true \
 --name gitlab \
 gitlab/gitlab-ce

4.4 设置域名、端口

  1. 进入容器
docker exec -it gitlab /bin/bash
  1. 修改配置文件
vi /etc/gitlab/gitlab.rb

在开头加入以下内容:
注意:external_url 的域名后面不能加端口,否则即使部署成功也无法访问。

#gitlab访问地址,可以写域名。如果端口不写的话默认为80端口
external_url 'https://gitlab.domain.com'
#ssh主机ip
gitlab_rails['gitlab_ssh_host'] = 'gitlab.domain.com'
#ssh连接端口
gitlab_rails['gitlab_shell_ssh_port'] = 9922
#时区
gitlab_rails['time_zone'] = 'Asia/Shanghai'
#开启备份功能
gitlab_rails['manage_backup_path'] = true
#备份文件的权限
gitlab_rails['backup_archive_permissions'] = 0644
#保存备份 60 天
gitlab_rails['backup_keep_time'] = 5184000

# https证书配置
letsencrypt['enable'] = true
#真实邮箱,用于证书过期前通知更新
letsencrypt['contact_emails'] = ['your_email@163.com']
nginx['redirect_http_to_https'] = true

主要修改:

  • external_url,将http改为https
  • 注意external_url配置的是域名,而非IP。
  • 开启letsencrypt相关配置。

4.5 配置邮箱

参见Docker搭建配置Gitlab的4.3 配置邮箱。

4.6 让配置生效

注意:这里只需要执行重启配置即可。不用配置443端口,因为已经自动指向了。

gitlab-ctl reconfigure

4.7 访问

访问地址:https://gitlab.domain.com,注意是域名。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值