部署GitLab单机版
记录部署GitLab单机版的过程。
一、规划及环境准备
1.1 规划
使用GitLab提供的Yum源,在CentOS9安装GitLab,配合acme.sh自动获取SSL证书。
| 主机名 |
GitLab-Server |
| 系统版本 |
CentOS 9 Stream |
| IP地址 |
192.168.10.66 |
| 开放端口 |
80、22 |
| 配置 |
2C、4G、80GB |
| 域名 |
gitlab.007890.xyz |
| 用途 |
本地GitLab服务器 |
1.2 环境准备
二、安装
2.1 安装GitLab
- 选择社区版的
YUM 源(二选一)
1
| curl "https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh" | sudo bash
|
- 使用清华源(二选一)
1
| vim /etc/yum.repos.d/gitlab-ce.repo
|
1 2 3 4 5
| [gitlab-ce] name=Gitlab CE Repository baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/ gpgcheck=0 enabled=1
|
设置域名的同时安装
安装时先使用http安装。
1
| sudo EXTERNAL_URL="http://gitlab.007890.xyz" dnf install gitlab-ce
|
2.2 配置HTTPS
- 编辑配置文件
1
| vim /etc/gitlab/gitlab.rb
|
1 2 3 4
| external_url 'https://gitlab.007890.xyz' nginx['redirect_http_to_https'] = true nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.007890.xyz/server.crt" nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.007890.xyz/server.key"
|
1
| egrep -v '^$|^#' /etc/gitlab/gitlab.rb
|
- 申请证书并安装
1
| mkdir /etc/gitlab/ssl/gitlab.007890.xyz
|
1 2 3
| acme.sh --install-cert -d gitlab.007890.xyz --ecc \ --key-file /etc/gitlab/ssl/gitlab.007890.xyz/server.key \ --fullchain-file /etc/gitlab/ssl/gitlab.007890.xyz/server.crt
|
注意,需要和 /etc/gitlab/gitlab.rb 配置中对应。
- 重载配置,重启
gitlab
1 2
| gitlab-ctl reconfigure gitlab-ctl restart
|
- 放行防火墙,并重载
1 2
| firewall-cmd --permanent --add-port=22/tcp --add-port=80/tcp --add-port=443/tcp firewall-cmd --reload
|
- 查看默认密码
1
| cat /etc/gitlab/initial_root_password
|
- 重置密码
1
| gitlab-rake "gitlab:password:reset"
|
三、备份恢复
3.1 备份配置文件
/etc/gitlab
1 2
| mkdir /backup/gitlab/ -p tar -zcvf /backup/gitlab/gitlab-conf-$(date+%F).tar.gz /etc/gitlab/
|
3.2 修改配置文件
- 修改相关的配置
1
| vim /etc/gitlab/gitlab.rb
|
1
| egrep -v '^$|^#' /etc/gitlab/gitlab.rb
|
1 2 3 4
| gitlab_rails['manage_backup_path'] = true gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" gitlab_rails['backup_archive_permissions'] = 0644 gitlab_rails['backup_keep_time'] = 604800
|
1 2 3 4 5 6 7 8 9 10
| [root@GitLab-Server ~]# egrep -v '^$|^#' /etc/gitlab/gitlab.rb external_url 'https://gitlab.007890.xyz' gitlab_rails['manage_backup_path'] = true gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" gitlab_rails['backup_archive_permissions'] = 0644 gitlab_rails['backup_keep_time'] = 604800 nginx['redirect_http_to_https'] = true nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.007890.xyz/server.crt" nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.007890.xyz/server.key" [root@GitLab-Server ~]#
|
- 重载配置,重启
gitlab
1 2
| gitlab-ctl reconfigure gitlab-ctl restart
|
3.3 手动备份
1
| gitlab-rake gitlab:backup:create
|
3.4 手动恢复
- 停止进程写入
1 2
| gitlab-ctl stop unicorn gitlab-ctl stop sidekiq
|
- 查看所有的备份
1
| ls -l /var/opt/gitlab/backups
|
1 2 3 4
| [root@GitLab-Server ~]# ls -l /var/opt/gitlab/backups total 752 -rw-r--r--. 1 git git 768000 Dec 8 21:49 1765201779_2025_12_08_18.6.1_gitlab_backup.tar [root@GitLab-Server ~]#
|
- 恢复
不能携带tar 的后缀
>=12.2,需要输入两次yes
1765201779_2025_12_08_18.6.1_gitlab_backup.tar
1
| gitlab-backup restore BACKUP=1765201779_2025_12_08_18.6.1
|
1
| itlab-rake gitlab:backup:restore BACKUP=
|
- 重启
3.5 备份脚本
1 2 3 4 5 6 7 8 9 10 11 12
| #!/bin/bash
gitlab-backup create
tar zcf /backup/gitlab/gitlab-conf-$(date+%F).tar.gz /etc/gitlab/
rsync xxx
00 03 * * * sh /server/scripts/backup-gitlab.sh &>/dev/null
|
3.6 忘记密码
1
| gitlab-rake "gitlab:password:reset"
|
四、参考资料