linux下搭建gitlab
1.背景
在学习Gitlab的环境搭建之前,首先需要了解Git,Gitlab,GitHub他们三者之间的关系
Git 它是一个源代码版本控制系统,可让您在本地跟踪更改并从远程资源推送或提取更改。
GitHub 是一项公开可用的免费服务,它要求所有代码(除非您有付费帐户)公开。 任何人都可以看到您推送给GitHub的代码并提供改进建议。
GitLab是一种类似github的服务,组织可以使用它来提供git存储库的内部管理。 它是一个自我托管的Git-repository管理系统,可以保持用户代码的私密性,并且可以轻松地部署代码的更改。
简单来说,github是全世界最大的开源的gay友平台,适合免费公开的代码。gitlab是搭建本公司的代码管理平台,适合私有的平台代码管理。
因为本公司的代码不想被其它人看到,这就需要搭建一个本公司的gitlab平台,供公司内部人员使用。
环境准备:centos 7 、gitlab 10
2.安装依赖包
GitLab 10.x之后添加多了一些依赖,并且要启动sshd服务,所以我们先添加依赖,启动sshd,为防火墙添加服务
sudo yum install -y curl policycoreutils-python openssh-server openssh-clients
sudo systemctl enable sshd
sudo systemctl start sshd
postfix的产生是为了替代传统的sendmail.相较于sendmail,postfix在速度。性能和稳定性上都更胜一筹。所以我们这里选择postfix来配置发送邮件
先安装postfix(用于邮件通知)
yum -y install postfix
安装完之后,启动postfix
systemctl start postfix
[root@xiaoxiao ~]# systemctl start postfix Job for postfix.service failed because the control process exited with error code. See "systemctl status postfix.service" and "journalctl -xe" for details. [root@xiaoxiao ~]#
启动过程中报错,error code
解决办法,vim打开/etc/postfix/main.cf,修改如下两项,修改之前可以先备份下

改成:
inet_interfaces = all
inet_protocols = ipv4
再次启动和查看状态:running代表启动成功

3.添加GitLab镜像源并安装gitlab服务器
centos 7系统的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
1.添加gitlab镜像
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm
[root@xiaoxiao ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm --2020-04-03 09:40:28-- https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1 Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 359519811 (343M) [application/x-redhat-package-manager] Saving to: ‘gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm’ 100%[======================================>] 359,519,811 12.7MB/s in 31s 2020-04-03 09:40:59 (11.0 MB/s) - ‘gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm’ saved [359519811/359519811] [root@xiaoxiao ~]# mkdir /usr/local/gitlab [root@xiaoxiao ~]# mv gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm /usr/local/gitlab/ [root@xiaoxiao ~]# cd /usr/local/gitlab/ [root@xiaoxiao gitlab]# rpm -i gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm warning: gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY It looks like GitLab has not been configured yet; skipping the upgrade script. *. *. *** *** ***** ***** .****** ******* ******** ******** ,,,,,,,,,***********,,,,,,,,, ,,,,,,,,,,,*********,,,,,,,,,,, .,,,,,,,,,,,*******,,,,,,,,,,,, ,,,,,,,,,*****,,,,,,,,,. ,,,,,,,****,,,,,, .,,,***,,,, ,*,. _______ __ __ __ / ____(_) /_/ / ____ _/ /_ / / __/ / __/ / / __ \`/ __ \ / /_/ / / /_/ /___/ /_/ / /_/ / \____/_/\__/_____/\__,_/_.___/ Thank you for installing GitLab! GitLab was unable to detect a valid hostname for your instance. Please configure a URL for your GitLab instance by setting `external_url` configuration in /etc/gitlab/gitlab.rb file. Then, you can start your GitLab instance by running the following command: sudo gitlab-ctl reconfigure For a comprehensive list of configuration options please see the Omnibus GitLab readme https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md [root@xiaoxiao gitlab]#
4.配置并启动gitlab-ce
gitlab安装完成后,需要设置一个访问地址(或域名),打开/etc/gitlab/gitlab.rb,将默认的external_url = 'http://git.example.com'修改为自己的IP地址:http://xxx.xx.xxx.xx
vim /etc/gitlab/gitlab.rb

修改成自己定义的url地址,端口自己设置一个,别和已有的冲突了,external_url 'http://xxx.xxx.240:8100'
修改完成后:wq保存退出,执行以下命令,让配置生效:
gitlab-ctl reconfigure
[root@xiaoxiao gitlab]# gitlab-ctl reconfigure
......
Recipe: gitlab::postgres-exporter
* service[postgres-exporter] action restart
- restart service service[postgres-exporter]
* ruby_block[reload postgres-exporter svlogd configuration] action create
- execute the ruby block reload postgres-exporter svlogd configuration
Running handlers:
Running handlers complete
Chef Client finished, 360/515 resources updated in 02 minutes 04 seconds
gitlab Reconfigured!
启动gitlab
gitlab-ctl start
[root@xiaoxiao gitlab]# gitlab-ctl start ok: run: gitaly: (pid 23392) 169s ok: run: gitlab-monitor: (pid 23431) 168s ok: run: gitlab-workhorse: (pid 23406) 168s ok: run: logrotate: (pid 23057) 211s ok: run: nginx: (pid 23035) 213s ok: run: node-exporter: (pid 23092) 205s ok: run: postgres-exporter: (pid 23488) 166s ok: run: postgresql: (pid 22760) 270s ok: run: prometheus: (pid 24737) 0s ok: run: redis: (pid 22700) 278s ok: run: redis-exporter: (pid 23171) 199s ok: run: sidekiq: (pid 22936) 229s ok: run: unicorn: (pid 22898) 231s [root@xiaoxiao gitlab]#
启动完成后,在浏览器输入http://xxx.xx.xx.240:8100,就是gitlab的登录首页了

5.gitlab配置查看
gitlab的配置主要是etc/gitlab/gitlab.rb下的两个地方
external_url 'http://47.104.x.x:8100' unicorn['port'] = 8101

查看更多配置信息可以,在/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml查看具体的详情
## GitLab settings gitlab: ## Web server settings (note: host is the FQDN, do not include http://) host: xx.xx.xx.240 port: 8100 https: false
![]()
6.相关操作
启动服务:gitlab-ctl start 查看状态:gitlab-ctl status 停掉服务:gitlab-ctl stop 重启服务:gitlab-ctl restart 让配置生效:gitlab-ctl reconfigure


浙公网安备 33010602011771号