在阿里云CentOS上安装Gitlab

本次安装使用阿里云ECS, CentOS7.6。

1. 配置yum源

sudo vi  /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

2. 更新本地yum缓存

sudo yum clean all
sudo yum makecache

3. 安装Gitlab

sudo yum -y install gitlab-ce    #自动安装最新版
sudo yum -y install gitlab-ce-12.0.0  #安装指定版本, 本次安装使用版本

4. 修改配置文件

 sudo cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.origin
 sudo vi /etc/gitlab/gitlab.rb

4.1 修改URL

## GitLab URL
external_url 'http://101.132.153.31:7339'

4.2 Email 配置

如想使用Postfix来发送邮件,在安装期间请选择’Internet Site’. 您也可以用sendmai或者 配置SMTP服务使用SMTP发送邮件.

### Email Settings
 gitlab_rails['gitlab_email_enabled'] = true
 gitlab_rails['gitlab_email_from'] = 'XXXXXX@yeah.net'
 gitlab_rails['gitlab_email_display_name'] = 'XXGitHub'
 gitlab_rails['gitlab_email_reply_to'] = 'XXX@yeah.net'
 gitlab_rails['gitlab_email_subject_suffix'] = ''

### GitLab email server settings
gitlab_rails['smtp_enable'] = true
 gitlab_rails['smtp_address'] = "smtp.yeah.net"
 gitlab_rails['smtp_port'] = 465
 gitlab_rails['smtp_user_name'] = "XXXXXX@yeah.net"
 gitlab_rails['smtp_password'] = "XXXXXX"
 gitlab_rails['smtp_domain'] = "yeah.net"
 gitlab_rails['smtp_authentication'] = "login"
 gitlab_rails['smtp_enable_starttls_auto'] = true
 gitlab_rails['smtp_tls'] = true

4.3 备份配置

### Backup Settings
 gitlab_rails['manage_backup_path'] = true
 gitlab_rails['backup_path'] = "/export/home/scm/gitlab/backups"

###! The duration in seconds to keep backups before they are allowed to be deleted
 gitlab_rails['backup_keep_time'] = 604800

5. 配置并重启

 sudo gitlab-ctl reconfigure
 sudo gitlab-ctl restart

6. 测试Email配置

sudo gitlab-rails console -e production
在控制台窗口执行
>Notify.test_email("weiminsun@163.com","hello","gitlab").deliver_now

7. 备份

7.1 手工备份

gitlab-rake gitlab:backup:create

使用以上命令会在备份目录(不改配置的话默认为/var/opt/gitlab/backups)目录下创建一个名称类似为******_gitlab_backup.tar的压缩包, 这个压缩包就是Gitlab整个的完整部分, 其中开头的******是备份创建的日期

-rw------- 1 git  git  979077120 Sep 17 16:00 1568704197_2019_09_17_12.0.0_gitlab_backup.tar

相应的配置文件需要备份:

/etc/gitlab/gitlab.rb 配置文件须备份 
/var/opt/gitlab/nginx/conf nginx配置文件 
/etc/postfix/main.cfpostfix 邮件配置备份

7.2 Gitlab自动备份

实现每天凌晨2点进行一次自动备份:通过crontab使用备份命令实现
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

8. 恢复

既可以是本系统的数据恢复,亦可以在另外一台一样版本的Gitlab上做数据迁移。

8.1 停止相关数据连接服务

sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq

8.2 从1568704197_2019_09_17_12.0.0编号备份中恢复

sudo gitlab-rake gitlab:backup:restore BACKUP=1568704197_2019_09_17_12.0.0

8.3 启动Gitlab

sudo gitlab-ctl start

9. gitlab迁移

迁移如同备份与恢复的步骤一样, 只需要将老服务器备份目录下的备份文件拷贝到新服务器上的备份目录/var/opt/gitlab/backups即可(如果你没修改过默认备份目录的话).
需要注意的是新服务器上的Gitlab的版本必须与创建备份时的Gitlab版本号相同. 比如新服务器安装的是12.0.0版本的Gitlab, 那么迁移之前, 最好将老服务器的Gitlab 升级为12.0.0在进行备份

/etc/gitlab/gitlab.rb gitlab配置文件须迁移,迁移后需要调整数据存放目录
/var/opt/gitlab/nginx/conf nginx配置文件目录须迁移

/etc/gitlab/gitlab-secrets.json # 复制新服务器相同的目录下
/etc/ssh/key # 复制到新服务器相同目录下,解决ssh key认证不成功问题

# sudo gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
# sudo gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
#  sudo chmod 777 /var/opt/gitlab/backups/1568704197_2019_09_17_12.0.0_gitlab_backup.tar  
或 # sudo chown git:git /var/opt/gitlab/backups/1568704197_2019_09_17_12.0.0_gitlab_backup.tar
# sudo gitlab-rake gitlab:backup:restore BACKUP=1568704197_2019_09_17_12.0.0

10. Gitlab升级

10.1 关闭gitlab服务

sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
sudo gitlab-ctl stop nginx

10.2 备份gitlab

sudo gitlab-rake gitlab:backup:create

10.3 下载gitlab的RPM包并进行升级

sudo curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum update gitlab-ce

或者直接安装高版本

sudo yum install gitlab-ce-12.2.5-ce.0.el7.x86_64

或者上官网下载最新版本 gitlab对应软件包 gitlab官网

使用

rpm -Uvh gitlab-ce-12.2.5-ce.0.el7.x86_64

报错.
Error executing action run on resource 'ruby_block[directory resource: /var/opt/gitlab/git-data/repositories]'
解决方法:

sudo chmod 2770 /var/opt/gitlab/git-data/repositories

10.4 启动并查看gitlab版本信息

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
#sudo  head -1 /opt/gitlab/version-manifest.txt
gitlab-ce 12.2.5

10.5 gitlab更改默认Nginx

更换gitlab自带Nginx,使用自行编译Nginx来管理gitlab服务。
编辑gitlab配置文件禁用自带Nignx服务器

sudo vi /etc/gitlab/gitlab.rb
#设置nginx为false,关闭自带Nginx
nginx['enable'] = false

检查默认nginx配置文件,并迁移至新Nginx服务

/var/opt/gitlab/nginx/conf/nginx.conf #nginx配置文件,包含gitlab-http.conf文件
/var/opt/gitlab/nginx/conf/gitlab-http.conf #gitlab核心nginx配置文件
重启 nginx、gitlab服务

$ sudo gitlab-ctl reconfigure
$ sudo service nginx restart

访问报502。原因是nginx用户无法访问gitlab用户的socket文件。 重启gitlab需要重新授权

chmod -R o+x /var/opt/gitlab/gitlab-rails

11. 附录:

11.1 启动脚本

sudo cat /etc/systemd/system/gitlab.service
[Unit]
Description=gitlab
 
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/gitlab-ctl start
ExecStop=/bin/gitlab-ctl stop
 
[Install]
WantedBy=multi-user.target
sudo cat /usr/lib/systemd/system/gitlab-runsvdir.service
[Unit]
Description=GitLab Runit supervision process
After=basic.target
 
[Service]
ExecStart=/opt/gitlab/embedded/bin/runsvdir-start
Restart=always
 
[Install]
WantedBy=basic.target

11.2 防火墙

/sbin/iptables -I INPUT -p tcp --dport 7339 -j ACCEPT
service iptables save
systemctl restart iptables.service

11.3 reconfigure报错

sudo systemctl restart gitlab-runsvdir

12. 参考

https://docs.gitlab.com/ee/raketasks/backup_restore.html
https://docs.gitlab.com/omnibus/settings/backups.html

推荐阅读更多精彩内容