欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Centos7下搭建Gitlab服务器

程序员文章站 2022-04-30 09:24:10
...

1 简介

gitlab是一个基于git实现的在线代码仓库托管软件,一般用于在企业、学校等内部网络搭建专属git仓库,越来越多的企业在使用gitlab作为项目托管仓库。
GitLab是利用Ruby On Rails开发的一个开源版本管理系统,实现了一个自托管的Git项目仓库,是集代码托管,测试,部署于一体的开源git仓库管理软件,可通过web界面来进行访问公开的或私人项目。与Github类似,GitLab能够浏览代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本,并提供一个文件历史库。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后需要的时候查找。

Git的家族成员

名称 说明
Git 一种版本控制系统,是一个命令,是一种工具
Gitlib 用于实现Git功能的开发库
Github 一个基于Git实现的在线代码托管仓库,公开项目是免费的,也可以付费创建私人项目
GitLab 一个基于Git实现的在线代码仓库托管软件,可以用GitLab搭建一套类似Github的系统

GitLab对硬件还是有一定要求的,1核心的CPU基本上可以满足需求,大概支撑100个左右的用户,不过在运行GitLab网站的同时还需要运行多个后台job,就会显得有点捉襟见肘了。需要至少4GB的可寻址内存(RAM交换)来安装和使用GitLab,操作系统和任何其他正在运行的应用程序也将使用内存,因此请记住,在运行GitLab之前,您至少需要4GB的可用空间,如果使用更少的内存,GitLab将在重新配置运行期间给出奇怪的错误。

2 安装配置依赖

2.1 安装启动ssh服务

[aaa@qq.com~]# sudo yum install -y curl policycoreutils-python openssh-server #安装ssh
 
[aaa@qq.com~]# sudo systemctl enable sshd #开机自启
[aaa@qq.com~]# sudo systemctl start sshd   #启动ssh
[aaa@qq.com~]# sudo systemctl status sshd

2.2 配置防火墙

[aaa@qq.com~]# sudo firewall-cmd --state#查看防火墙状态
not running
[aaa@qq.com~]# sudo systemctl start firewalld #没有启动,可以启动防火墙
[aaa@qq.com~]# sudo firewall-cmd --permanent --add-service=http#添加防火墙支持http永久访问
[aaa@qq.com~]# sudo systemctl reload firewalld#重新加载防火墙配置
[aaa@qq.com~]# sudo systemctl status firewalld.service

2.3 安装邮件服务

[aaa@qq.com~]# sudo yum install postfix -y   #安装postfix
 
[aaa@qq.com~]#sudo systemctl enable postfix  #开机自启
[aaa@qq.com~]# sudo systemctl start postfix  #启动postfix

出现错误:

Job for postfix.service failed because the control process exited with error code. See "systemctl status postfix.service" and "journalctl -xe" for details.
[aaa@qq.com~]# sudo systemctl status postfix

解决办法:
编辑/etc/postfix/main.cf文件,将inet_interfaces设置为all,再次启动postfix

[aaa@qq.com~]# vi /etc/postfix/main.cf  #将inet_interfaces设置为all
[aaa@qq.com~]# sudo systemctl status postfix

3 安装配置gitlab

3.1 配置yum源

运行下面指令自动下载当前系统的yum源,/etc/yum.repos.d/gitlab_gitlab-ee.repo

[aaa@qq.com~]# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash

查询已下载的yum源

[aaa@qq.com~]# cd /etc/yum.repos.d/
[aaa@qq.com~ yum.repos.d]# ll

国外镜像安装时十分慢,可以把刚下载的gitlab_gitlab-ee.repo删除掉,将yum换成清华的镜像。
注:gitlab_gitlab-ee.repo删除后无法下载gitlab-ee,因此不应删除

[aaa@qq.com~ yum.repos.d]# rm -rf gitlab_gitlab-ee.repo
[aaa@qq.com~ yum.repos.d]# vim gitlab-ce.repo #清华镜像yum源配置
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

3.2 yum安装

[aaa@qq.com~ yum.repos.d]# sudo yum makecache # 刷新yum缓存
#安装最新版本
[aaa@qq.com~ yum.repos.d]# sudo yum install –y gitlab-ee   
#按照指定版本
[aaa@qq.com~ yum.repos.d]# curl -LJO https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-9.5.9-ce.0.el7.x86_64.rpm
[aaa@qq.com~ yum.repos.d]# rpm -i gitlab-ce-9.5.9-ce.0.el7.x86_64.rpm

3.3 配置访问地址

编辑/etc/gitlab/gitlab.rb,修改extend_url
邮件配置参考:https://docs.gitlab.com/omnibus/settings/smtp.html

[aaa@qq.com~ yum.repos.d]# vim /etc/gitlab/gitlab.rb
external_url 'http://47.105.147.6:8081'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "aaa@qq.com"
gitlab_rails['smtp_password'] = "lcl0412"
gitlab_rails['smtp_domain'] = "exmail.qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = aaa@qq.com

3.4 重新配置应用

[aaa@qq.com~ yum.repos.d]# gitlab-ctl reconfigure #执行此命令重新加载配置且同时启动gitlab所有服务

3.5 启动gitlab

[aaa@qq.com~ yum.repos.d]# gitlab-ctl status #查看是否启动
 
[aaa@qq.com~ yum.repos.d]# gitlab-ctl start  #没有启动可以启动
[aaa@qq.com~ yum.repos.d]# gitlab-ctl restart #重新启动下
[aaa@qq.com~ yum.repos.d]# gitlab-ctl stop #停止
[aaa@qq.com~ yum.repos.d]# gitlab-ctl --help #查看更多命令

3.6 防火墙开放端口

[aaa@qq.com~ yum.repos.d]# firewall-cmd --zone=public --list-ports #查看开放端口

[aaa@qq.com~ yum.repos.d]# firewall-cmd --zone=public --add-port=8081/tcp --permanent #开放8081端口
success
[aaa@qq.com~ yum.repos.d]# firewall-cmd --reload   #重新加载配置
success
[aaa@qq.com~ yum.repos.d]# systemctl stop firewalld.service  #如果不需要,也可以直接关闭防火墙

4 登录gitlab

网页输入http://47.105.147.6:8081,则显示无法打开网页,防火墙已开放8081端口,经调查是由于阿里云服务器需配置安全规则,开放相关端口方可访问。

4.1 阿里云配置安全规则

1、选择实例
Centos7下搭建Gitlab服务器
2、选择更多->网络和安全组->安全组配置
Centos7下搭建Gitlab服务器
3、选择配置规则
Centos7下搭建Gitlab服务器
4、选择手动添加,添加端口范围、授权对象等信息
其中,如果表示某个网段,则可以使用0代替,如果允许全部网段则0.0.0.0
Centos7下搭建Gitlab服务器

4.2 修改密码

1、网页登录http://ip:port,被重定向到密码修改界面,修改密码为root的管理员账户
Centos7下搭建Gitlab服务器
2、修改密码后自动跳转到登录页面,用root和修改后密码登录就可以
Centos7下搭建Gitlab服务器
3、登录成功如下,就可以开始自己的操作了
Centos7下搭建Gitlab服务器

5 卸载Gitlab

[aaa@qq.com~]# gitlab-ctl stop  # 停止gitlab

查询gitlab版本,确实是gitlab-ce还是gitlab-ee

[aaa@qq.com~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION #查询版本
13.0.1-ee
[aaa@qq.com~]# rpm –e gitlab-ee  #卸载gitlab
[aaa@qq.com~]# ps -ef|grep gitlab  #查询gitlab进程
[aaa@qq.com~]# kill -9 1196  #杀掉第一个守护进程
 
[aaa@qq.com~]# find / -name *gitlab*|xargs rm -rf   #删除所有包含gitlab的文件及目录
[aaa@qq.com~]# find / -name gitlab |xargs rm –rf  #删除gitlab-ctl uninstall时自动在root下备份的配置文件
[aaa@qq.com~]# ls /root/gitlab*   #看看有没有,有也删除

6 Gitlab忘记root密码

[aaa@qq.com~]# gitlab-rails console production  #进入gitlab控制台
Loading production environment (Rails 4.2.8)
irb(main):001:0> 
irb(main):002:0* user=User.where(id:1).first  #获取第一个用户,可以看到第一个默认为 root
=> #<User id:1 @root>
irb(main):003:0> user.password="lcl181225" #设置新密码
=> "lcl181225"
irb(main):004:0> user.password_confirmation="lcl181225"
=> "lcl181225"
irb(main):005:0> user.save!  #保存密码
Enqueued ActionMailer::DeliveryJob (Job ID: f79fbe6f-9772-4754-ae2a-3a5cdaf2bc8a) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
=> true
irb(main):006:0>quit

7 Gitlab汉化

在已安装Gitlab英文版本的情况下,通过官方提供的GitLab-CE Omnibus安装包安装了GitLab CE 9.2.6实现汉化Gitlab。

[aaa@qq.com~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION #查询版本
13.0.1-ee
[aaa@qq.com~]# git clone https://gitlab.com/xhang/gitlab.git  #克隆Gitlab汉化源代码仓库
[aaa@qq.com~]# cd /home/gitlab  #进入克隆仓库得到的gitlab目录
[aaa@qq.com gitlab]# git tag   # 查看tag版本,选择合适的***本
……
v9.5.9
v9.5.9-zh
v9.6.0.pre
[aaa@qq.com gitlab]# git diff v9.5.9..v9.5.9-zh > /tmp/9.5.9.diff    # 对比不同,这里比较的是tag,v9.5.9为英文原版,v9.5.9-zh为***本。diff结果是汉化补丁。
[aaa@qq.com gitlab]# gitlab-ctl stop  #停止gitlab
[aaa@qq.com gitlab]# cd /opt/gitlab/embedded/service/gitlab-rails
[aaa@qq.com gitlab-rails]# git apply /tmp/9.5.9.diff   # 应用汉化补丁
[aaa@qq.com gitlab-rails]# gitlab-ctl start  # 启动gitlab
[aaa@qq.com gitlab-rails]# gitlab-ctl reconfigure  # 重新配置gitlab

8 参考文献

CentOS7下搭建Gitlab : https://blog.csdn.net/lzxlfly/article/details/83351577
邮件配置参考:https://docs.gitlab.com/omnibus/settings/smtp.html
GitLab汉化指南:https://www.jianshu.com/p/6606aed59a56