前言
这里简单记录下
GitLab
的搭建教程
安装环境
阿里云服务器 CentOS 7.5 64位
1 | $ uname -a |
安装必要的依赖
安装ssh协议
1
2
3
4
5
6# 安装ssh
$ sudo yum install -y curl policycoreutils-python openssh-server
# 开机自启
$ sudo systemctl enable sshd
# 启动ssh
$ sudo systemctl start sshd安装防火墙
1
2
3
4
5
6
7
8
9
10# 安装防火墙
$ yum install firewalld systemd -y
# 开机自启
$ systemctl enable firewalld
# 安装防火墙
$ service firewalld start
# 添加HTTP服务到firewalld
$ sudo firewall-cmd --permanent --add-service=http
# 重启防火墙
$ sudo systemctl reload firewalld安装postfix以发送邮件
1
2
3
4
5
6# 安装postfix
$ sudo yum install postfix
# 开机自启
$ sudo systemctl enable postfix
# 启动postfix
$ sudo systemctl start postfix如果启动失败,报错
Failed to start Postfix Mail Transport Agent.
,修改如下配置1
2$ vim /etc/postfix/main.cf
inet_interfaces = all
安装GitLab服务
下载gitlab镜像 清华大学开源镜像站
1
$ wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.9.2-ce.0.el7.x86_64.rpm
安装GitLab
1
$ rpm -i gitlab-ce-12.9.2-ce.0.el7.x86_64.rpm
修改服务ip和端口
1
$ vim /etc/gitlab/gitlab.rb
将
external_url
修改为http://ip:port
,例如http://127.0.0.1:8090
将
8090
端口添加到防火墙中1
$ firewall-cmd --zone=public --add-port=8090/tcp --permanent
重启防火墙
1
$ sudo systemctl reload firewalld
重置GitLab
1
$ gitlab-ctl reconfigure
启动GitLab
1
$ gitlab-ctl restart
开机自启
1
$ systemctl enable gitlab-runsvdir.service
访问GitLab
浏览器直接输入http://ip:port
访问即可
升级GitLab
下载gitlab镜像 清华大学开源镜像站
1
$ wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.9.3-ce.0.el7.x86_64.rpm
关闭部分GitLab服务
1
2
3
4
5$ gitlab-ctl stop unicorn
$ gitlab-ctl stop sidekiq
$ gitlab-ctl stop nginx
# 或者 && 执行
$ gitlab-ctl stop unicorn && gitlab-ctl stop sidekiq && gitlab-ctl stop nginx升级GitLab
1
$ rpm -Uvh gitlab-ce-12.9.3-ce.0.el7.x86_64.rpm
重新配置gitlab
1
$ gitlab-ctl reconfigure
重启gitlab
1
$ gitlab-ctl restart
查看版本号
1
2$ cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
$ gitlab-rake gitlab:env:info
配置自动备份
配置文件
/etc/gitlab/gitlab.rb
1
2
3
4gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/gitlab/backups" #备份目录
gitlab_rails['backup_archive_permissions'] = 0644 #生成的文件权限
gitlab_rails['backup_keep_time'] = 864000 #保留10天手动执行备份
1
$ gitlab-rake gitlab:backup:create
注:需要手动备份如下相关配置文件:
1
2/etc/gitlab/gitlab.rb
/etc/gitlab/gitlab-secrets.json备份文件生成至
/data/gitlab/backups
文件名例如:1587048780_2020_04_16_12.9.2_gitlab_backup.tar
自动定时备份
1
2
3$ crontab -e
# 每天5点执行备份
0 5 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1恢复备份
1
2
3
4
5
6
7
8
9
10
11
12
13# 停止连接数据库的进程
$ sudo gitlab-ctl stop unicorn
$ sudo gitlab-ctl stop sidekiq
# 拷贝备份至默认路径
$ cp 1587048780_2020_04_16_12.9.2_gitlab_backup.tar /var/opt/gitlab/backups/
# 给git用户授权
$ chown git:git 1587048780_2020_04_16_12.9.2_gitlab_backup.tar
# 恢复1587048780这个备份文件,将覆盖GitLab数据库!
$ sudo gitlab-rake gitlab:backup:restore BACKUP=1587048780
# 重启 GitLab
$ sudo gitlab-ctl restart
# 检查 GitLab
$ sudo gitlab-rake gitlab:check SANITIZE=true注:恢复后可能出现web端点击项目报错
500
,则需要将如下配置文件拷贝到恢复环境上,然后gitlab-ctl reconfigure & gitlab-ctl restart
即可1
2/etc/gitlab/gitlab.rb
/etc/gitlab/gitlab-secrets.json
安装问题说明
上述步骤执行后,浏览器访问出现502
自己安装过程中的问题:
安装完成浏览器访问出现
502
,因为看到资料说GitLab
默认端口为8080
,所以配置external_url
时修改为http://ip:8080
,按照上述文章,经过一番折腾,尝试过创建swap
分区,但问题依然没有解决,并反复确认发现8080
并没有被占用,后又执行gitlab-ctl status
,多次执行发现unicorn的pid一直在变大,而其他服务正常,说明unicorn
启动失败,查看日志gitlab-ctl tail unicorn
,然后查看配置文件/etc/gitlab/gitlab.rb
,发现# unicorn['port'] = 8080
,说明unicorn的默认端口为8080
,将external_url
修改为http://ip:8090
,重置GitLab并重启,访问正常。
注:其实参考教程中是将端口设置为
9090
的,由于看到资料说默认端口是8080
才设置的http://ip:8080
导致出现502
,但也对安装过程有了更深刻的印象
启动后内存占用高
修改配置/etc/gitlab/gitlab.rb
参考教程
减小
进程数
1
unicorn['work_processes'] = 2
默认是2,官方建议是CPU核心数加一,可以提高服务器的响应速度。
如果内存只有4G,或者服务器上有其它业务,请勿修改,以免内存不足。
注:这个参数最小值是2,设为1,服务器可能会卡死。减小
postgresql
缓存1
postgresql['shared_buffers'] = "128MB"
默认是256MB,可适当改小
减少
postgresql
并发数1
postgresql['max_worker_processes'] = 4
默认是8,可适当改小
减少
sidekiq
并发数1
sidekiq['concurrency'] = 8
默认是25,可适当改小
启用
swap
分区
教程
修改完配置以后,需要执行下面的命令使配置生效:
1 | $ sudo gitlab-ctl reconfigure |
备注:按照上述方案修改后,阿里云2核4G
的服务器,安装12.x版本Gitlab占用依然达到2G
以上,由于服务器上不止运行Gitlab,并且对Gitlab版本要求不高,所以选择了宝塔上的GitLab中文社区版 8.8.5
。