创建自己的gitlab服务,提升代码安全
以下演示在debian11、debian12上操作,其它linux类似
16.3.3版本(2023.9.18更新)
一、下载安装
1.下载
以下操作在/usr/local下操作
cd /usr/local
从国内镜像下载
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/debian/pool/bullseye/main/g/gitlab-ce/gitlab-ce_16.3.3-ce.0_amd64.deb
2.安装依赖并配置postfix
安装所有依赖
apt-get install policycoreutils curl openssh-server ca-certificates postfix
配置postfix,如下图。两个Enter(回车)即可
启动、开机启动postfix
systemctl enable postfix && systemctl start postfix && systemctl status postfix
3.安装
dpkg -i gitlab-ce_16.3.3-ce.0_amd64.deb
安装完成后会提示还没有配置文件,并现实配置文件位置如下位置
configuration in /etc/gitlab/gitlab.rb file.
4.配置gitlab
授权配置文件并打开
chmod 600 /etc/gitlab/gitlab.rb && vi /etc/gitlab/gitlab.rb
取消前面的#号注释并修改参数
修改external_url,改为部署服务的内网地址(从路由那里看)和端口(根据自己需要,这里是9999端口)
注意:必须包含http://或https://
external_url 'http://192.168.xxx.xxx:9999'
修改git_data_dir数据保存位置,我这里改为/var/gitlab,如下:
git_data_dir({
"default" => {
"path" => "/var/gitlab"
}
})
保存退出配置文件
5.创建数据目录
mkdir /var/gitlab && chmod -R 750 /var/gitlab
6.应用配置
首次配置时间较长
gitlab-ctl reconfigure
提示如下:
7.获取密码,查看状态
查看root密码,这个密码文件24小时后会清除,请及时查看,拿个本本记下来
cat /etc/gitlab/initial_root_password
查看状态,gitlab会开机自动启动,无需操心
gitlab-ctl status
浏览器web登录刚刚设置的ip:port即可访问,如下,输入root+刚刚查询的密码即可访问
二、配置卸载优化
1.调整配置、卸载
先暂停gitlab
gitlab-ctl stop
修改配置文件后,重新配置
gitlab-ctl reconfigure
启动
gitlab-ctl start
卸载gitlab
卸载主程序
apt remove gitlab-ce
删除文件,逐条运行下面命令
rm -rf /opt/gitlab
rm -rf /etc/gitlab
rm -rf /var/log/gitlab
rm -rf /var/gitlab
2.优化
gitlab比较吃内存,如果使用人数少,可以修改线程数,降低对硬件资源的消耗
打开配置文件
vi /etc/gitlab/gitlab.rb
如果前面有#号,取消前面的#号注释并修改参数
找到puma[‘worker_processes’]设置为1
找到puma[‘per_worker_max_memory_mb’] 取消注释
找到unicorn[‘worker_timeout’] = 60
找到postgresql[‘max_worker_processes’]改为1
找到postgresql[‘shared_buffers’] = “256MB"改为"64MB”
找到sidekiq[‘max_concurrency’] = 3
应用新配置并重新启动,逐条运行下面命令
gitlab-ctl stop
gitlab-ctl reconfigure
gitlab-ctl start
3.迁移到新服务器
注意,这种方式只能大版本一致的才行。
导出
gitlab-rake gitlab:backup:create
会提示手动复制2个重要文件
文件默认备份到了/var/opt/gitlab/backups
将导出的.tar文件下载并上传到新服务的/var/opt/gitlab/backups目录下
另外两个重要的配置文件(在/etc/gitlab/)复制到相同位置
导入
注意,原来后面的gitlab_backup.tar不要写进去,具体的备份名称填写自己的
gitlab-rake gitlab:backup:restore BACKUP=1695041661_2023_09_18_15.1.3
重启服务
gitlab-ctl reconfigure
gitlab-ctl restart