本文作者 徐曉偉
說明
- 極狐GitLab https 使用的是 nginx 實現的
- 本文使用的域名是IP 192.168.80.14(原因:如果使用域名,必須擁有這個域名的所有權,并增加解析才可以,要不然在 Docker 容器中,無法使用域名檢出代碼,因為根據域名找不到DNS記錄)
- 如果使用自己生成的證書,git 檢出代碼、推送代碼會失敗,原因是無法驗證證書的有效性,可以使用名
git config --global http.sslVerify false
禁用ssl的驗證
生成證書
-
如果有域名,可以使用域名申請免費的證書,下載 Nginx 證書即可
- 阿里云SSL(https)證書免費申請
- 騰訊云SSL(https)證書免費申請
- 華為云SSL(https)證書免費申請
- 百度云SSL(https)證書免費申請
-
如果沒有域名,可使用下列命令在 CentOS 上生成
-
創建證書文件夾
mkdir -p /etc/gitlab/ssl cd /etc/gitlab/ssl
-
生成證書
# 以 CentOS 為例 # 如果出現 -bash: openssl: command not found,請安裝 openssl:yum -y install openssl# 生成指定位數的 RSA 私鑰:ca.key openssl genrsa -out ca.key 2048# 根據 RSA 私鑰,生成 crt 證書:ca.crt # CN:設置你要使用的域名 # -utf8:支持中文 openssl req -new -x509 -days 3650 -key ca.key -subj "/C=CN/ST=山東/L=青島/O=徐曉偉工作室/OU=徐曉偉工作室/CN=192.168.80.14/emailAddress=xuxiaowei@xuxiaowei.com.cn" -out ca.crt -utf8 # openssl req -new -x509 -days 3650 -key ca.key -subj "/C=CN/ST=山東/L=青島/O=徐曉偉工作室/OU=徐曉偉工作室/CN=gitlab.example.com/emailAddress=xuxiaowei@xuxiaowei.com.cn" -out ca.crt -utf8# 生成 server.csr、server.key # CN:設置你要使用的域名 # -utf8:支持中文 openssl req -newkey rsa:2048 -nodes -keyout server.key -subj "/C=CN/ST=山東/L=青島/O=徐曉偉工作室/CN=192.168.80.14" -out server.csr -utf8 # openssl req -newkey rsa:2048 -nodes -keyout server.key -subj "/C=CN/ST=山東/L=青島/O=徐曉偉工作室/CN=gitlab.example.com" -out server.csr -utf8# 生成 ca.srl、server.crt # subjectAltName:設置 DNS、IP openssl x509 -req -extfile <(printf "subjectAltName=IP:192.168.80.14") -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt # openssl x509 -req -extfile <(printf "subjectAltName=DNS:gitlab.example.com") -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
-
最終生成了:ca.crt、ca.key、ca.srl、server.crt、server.csr、server.key,其中 **server.crt **和 server.key 就是 Nginx 使用的證書
配置https?
-
安裝 vim
yum -y install vim
-
編輯 gitlab.rb 文件
vim /etc/gitlab/gitlab.rb
-
修改內容如下
# 填寫你的域名,注意是https external_url 'https://192.168.80.14' # 如果使用的是域名,填寫域名 # external_url 'https://gitlab.example.com'# 對應上方域名的證書 # 將證書放在 /etc/gitlab/ssl 文件夾中 # 如果使用的是阿里云等平臺頒發的證書,此處可以使用 Nginx 證書,ssl_certificate 使用 .pem 文件 nginx['ssl_certificate'] = "/etc/gitlab/ssl/server.crt" nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/server.key"# http 重定向到 https nginx['redirect_http_to_https'] = true# 禁用 Let's Encrypt 頒發證書 letsencrypt['enable'] = false# 限制GitLab實例使用的IP(在使用域名時使用,注意瀏覽器緩存問題) # nginx['listen_addresses'] = ['192.168.80.14']
-
重新配置 GitLab
sudo gitlab-ctl reconfigure
-
查看GitLab各服務的狀態
sudo gitlab-ctl status
-
日志
sudo gitlab-ctl tail nginx
-
修改DNS(或者在本地 hosts 將域名指向 GitLab服務器IP),訪問
https://GitLab域名
即可(如果使用自己生成的證書,可能會提示* 您的連接不是私密連接*,忽略即可)。
更多關于極狐GitLab 信息和最佳實踐,請搜索【極狐GitLab】。