使用安装包方式安装GitLab

之前的文章介绍了使用docker-compose安装GitLab,本篇文章介绍使用安装包形式安装GitLab。


参考文档

GitLab 官网 CentOS 安装文档 ,使用国内IP访问时,会跳转到极狐GitLab

极狐GitLab CentOS 安装文档

极狐GitLab 中文文档

徐晓伟 gitLab-k8s

前言

极狐GitLab 是国内版的 GitLab,与 GitLab 相同,都提供代码托管与软件安装镜像

本文以 GitLab EE 为例(非极狐GitLab)

本文使用的IP为 192.168.56.104(原因:如果使用域名,必须拥有这个域名的所有权,并增加解析才可以,要不然在 Docker 容器中,无法使用域名检出代码,因为根据域名找不到DNS记录

安装GitLab

安装命令补全工具

1
yum install -y bash-completion

安装和配置必须的依赖项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 安装必要的依赖
# 参见文档:
# https://about.gitlab.com/install/#centos-7
# https://gitlab.cn/install/#centos-7
# https://packages.gitlab.com/gitlab
# https://packages.gitlab.com/gitlab/gitlab-ee/packages/el/7/gitlab-ee-15.5.4-ee.0.el7.x86_64.rpm
sudo yum install -y curl policycoreutils-python openssh-server perl
sudo systemctl enable sshd
sudo systemctl start sshd
# 开启 http 端口:GitLab 默认端口
# 开启 https 端口:GitLab 默认端口
# 防火墙如果被关闭了话,请手动开启
sudo systemctl start firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
# 重载防火墙
sudo systemctl reload firewalld
# 查看防火墙已开放的端口与服务
sudo firewall-cmd --list-all

安装 Postfix 以发送电子邮件通知(可选项)

  1. 用户使用新IP登录时发送邮件

  2. 用户使用邮件找回密码

  3. 用户PR邮件提示等

如果您想使用其他方式发送邮件,可以跳过此步骤,并在安装GitLab后配置外部SMTP服务器。

在安装 Postfix 的过程中可能会出现一个配置界面,在该界面中选择“Internet Site”并按下回车。把“mail name”设置为您服务器的外部 DNS 域名并按下回车。如果还有其它配置界面出现,继续按下回车以接受默认配置。

1
2
3
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

配置 GitLab EE 软件源镜像

1
2
3
mkdir -p /opt/gitlab
cd /opt/gitlab
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash

下载安装 GitLab EE

1
2
3
4
5
6
7
8
9
10
11
sudo EXTERNAL_URL="http://192.168.56.104" yum install -y gitlab-ee

# 或者使用
# sudo yum install -y gitlab-ee
# 由于上述安装命令未指定域名,需要手动执行一次配置
# 需要进入/etc/gitlab/gitlab.rb修改相应地址
# sudo gitlab-ctl reconfigure

# 或者使用
# 可以指定域名安装,避免手动配置
# sudo EXTERNAL_URL="http://gitlab.xiaojingge.com" yum install -y gitlab-ee

安装好了,访问:http://192.168.56.104

管理员 root 用户操作相关

查看用户初始密码

1
2
# 用户名为 root
sudo cat /etc/gitlab/initial_root_password

使用初始密码登录root,进入系统后会让修改密码,输入初始密码和两次新密码保存修改后重新登录即可。

将语言调整为中文,网址:http://<GitLab服务器IP>/-/profile/preferences,在 Localization 一栏将 Language 修改为 Chinese, Simplified - 简体中文,保存修改后刷新页面即可显示中文。

至此,GitLab就安装完成了。

相关命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看 GitLab 状态
sudo systemctl status gitlab-runsvdir.service

# 停止 GitLab
sudo systemctl stop gitlab-runsvdir.service

# 重启 GitLab
sudo systemctl restart gitlab-runsvdir.service

# 启动 GitLab
sudo systemctl start gitlab-runsvdir.service

# 查看 GitLab 开机自启状态
sudo systemctl list-unit-files | grep gitlab-runsvdir.service

# 关闭 GitLab 开启自启
sudo systemctl disable gitlab-runsvdir.service

# 开启 GitLab 开启自启
sudo systemctl enable gitlab-runsvdir.service

# 查看 GitLab 各服务的状态
sudo gitlab-ctl status

GitLab https配置

说明

GitLab https 使用的是 nginx 实现的

本文使用的IP是 192.168.56.104(原因:如果使用域名,必须拥有这个域名的所有权,并增加解析才可以,要不然在 Docker 容器中,无法使用域名检出代码,因为根据域名找不到DNS记录)

如果使用自己生成的证书,git 检出代码、推送代码会失败,原因是无法验证证书的有效性,可以使用名 git config --global http.sslVerify false 禁用ssl的验证

生成证书

如果有域名,可以使用域名申请免费的证书,下载 Nginx 证书即可

阿里云SSL(https)证书免费申请

腾讯云SSL(https)证书免费申请

华为云SSL(https)证书免费申请

百度云SSL(https)证书免费申请

如果没有域名,可使用下列命令在 CentOS 上生成

创建证书文件夹

1
2
mkdir -p /etc/gitlab/ssl
cd /etc/gitlab/ssl

生成证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 如果出现 -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.56.104/emailAddress=2427259171@qq.com" -out ca.crt -utf8
# openssl req -new -x509 -days 3650 -key ca.key -subj "/C=CN/ST=江苏/L=南京/O=筱晶哥哥/OU=筱晶哥哥/CN=gitlab.xiaojingge.com/emailAddress=2427259171@qq.com" -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.56.104" -out server.csr -utf8
# openssl req -newkey rsa:2048 -nodes -keyout server.key -subj "/C=CN/ST=江苏/L=南京/O=筱晶哥哥/CN=gitlab.xiaojingge.com" -out server.csr -utf8

# 生成 ca.srl、server.crt
# subjectAltName:设置 DNS、IP
openssl x509 -req -extfile <(printf "subjectAltName=IP:192.168.56.104") -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
# openssl x509 -req -extfile <(printf "subjectAltName=DNS:gitlab.xiaojingge.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.crtserver.key 就是 Nginx 使用的证书。

配置Https

安装vim

1
yum install -y vim

编辑 gitlab.rb 文件

1
vim /etc/gitlab/gitlab.rb

修改内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 填写你的域名,注意是https
external_url 'https://192.168.56.104'
# external_url 'https://gitlab.xiaojingge.com'

# 对应上方域名的证书
# 将证书放在 /etc/gitlab/ssl 文件夹中
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

1
sudo gitlab-ctl reconfigure

查看GitLab各服务的状态

1
sudo gitlab-ctl status

修改DNS(或者在本地 hosts 将域名指向 GitLab服务器IP),访问https://<GitLab域名> 即可(如果使用自己生成的证书,可能会提示您的连接不是私密连接,忽略即可)。

如果是用 IP 方式配置的直接访问 https://IP地址 即可。

到此 GitLab 的 https 访问就配置好了。

到此 GitLab 安装包方式安装就好了,后面还需要使用 GitLab Runner,就暂时不在这展开了。

点击查看

本文标题:使用安装包方式安装GitLab

文章作者:LiJing

发布时间:2023年02月27日 - 10:20:42

最后更新:2023年06月03日 - 09:59:42

原始链接:https://blog-next.xiaojingge.com/posts/1938919770.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------