在团队开发中以前大家都用svn作为版本控制器,现在大家都转用git了,gitlab就是web版的git版本控制器,今天就讲讲gitlab的安装。

1、GitLab安装简介:

安装方法详见:GitLab安装说明,安装时,首先让你选择服务器版本,我这里就选择centos7了,按照官方的安装说明进行安装,

2、开始安装GitLab:

官方安装时第一步的说明是这样的:

sudo yum install curl policycoreutils openssh-server openssh-clients
sudo systemctl enable sshd
sudo systemctl start sshd
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --reload

不过在实际应用中我修改了第一步,安装内容如下:

sudo yum -y install postfix cronie

安装两个包,其中一个是postfix,为接下来GitLabpostfix发邮件通知做准备,还有一个就是cronie,其实就是计划任务,安装完成之后开启postfix服务:

sudo service postfix start

如果提示如下错误:

Job for postfix.service failed. See 'systemctl status postfix.service' and 'journalctl -xn' for details.

请进入/etc/hosts中去掉IPV6的设定:

#::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

然后启动postfix服务,并且设定postfix服务开机启动:

sudo chkconfig postfix on

接下来进行第二步,第二步安装的时候有两种方法:
第一种是使用添加yum源,然后用yum源安装 [推荐这种方法安装],
第二种是直接用rpm包安装,安装方法如下:

①、yum源方法:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum -y install gitlab-ce
②、rpm包方法:

在使用rpm包时要选择自己需要的gitlab的版本,然后在下面的命令中,用版本号替换XXX。

curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-XXX.rpm/download
rpm -i gitlab-ce-XXX.rpm

安装完成, 接下来按照原始配置启动GitLab:

3、 用默认配置配置GitLab并启动:

sudo gitlab-ctl reconfigure

4、按个人需求修改GitLab配置:

因为这里要用已有的Nginx作为GitLab的服务器,所以要先进行配置,GitLab的配置文件位置:

/etc/gitlab/gitlab.rb

我这里因为直接使用https作为连接方式,并且提前配置了gitlab的域名是https://git.xxx.com,接下来修改/etc/gitlab/gitlab.rb以下内容,在修改内容里要注意一点就是web_server设置的值是你nginx的配置

# note the 'https' below
external_url 'https://git.xxx.com'

# Set the web server
web_server['external_users'] = ['www']

# Disable the built-in nginx
nginx['enable'] = false

上面是gitlab配置文件需要修改的地方,接下来添加nginx虚拟主机

5、添加nginx虚拟主机,支持gitlab:

添加gitlab.conf

vi /usr/local/nginx/conf/vhost/gitlab.conf

gitlab.conf中添加以下模板内容:

## GitLab
##
## Modified from nginx http version
## Modified from http://blog.phusion.nl/2012/04/21/tutorial-setting-up-gitlab-on-debian-6/
## Modified from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
##
## Lines starting with two hashes (##) are comments with information.
## Lines starting with one hash (#) are configuration parameters that can be uncommented.
##
##################################
##        CONTRIBUTING          ##
##################################
##
## If you change this file in a Merge Request, please also create
## a Merge Request on https://gitlab.com/gitlab-org/omnibus-gitlab/merge_requests
##
###################################
##         configuration         ##
###################################
##
## See installation.md#using-https for additional HTTPS configuration details.

upstream gitlab-workhorse {
  server unix:/var/opt/gitlab/gitlab-workhorse/socket fail_timeout=0;
}

## HTTPS host
server {
  listen  80;
  listen 443 ssl spdy;
  server_name git.xx.com; ## Replace this with something like gitlab.example.com
  server_tokens off; ## Don't show the nginx version number, a security best practice
  root /opt/gitlab/embedded/service/gitlab-rails/public;
  include rewrite.conf;
  include ssl.conf;
  include pagespeed.conf;
  ## Individual nginx logs for this GitLab vhost
  access_log  /var/log/nginx/gitlab.access.log;
  error_log   /var/log/nginx/gitlab.error.log;
  location / {
    client_max_body_size 0;
    gzip off;

    ## https://github.com/gitlabhq/gitlabhq/issues/694
    ## Some requests take more than 30 seconds.
    proxy_read_timeout      300;
    proxy_connect_timeout   300;
    proxy_redirect          off;

    proxy_http_version 1.1;

    proxy_set_header    Host                $http_host;
    proxy_set_header    X-Real-IP           $remote_addr;
    proxy_set_header    X-Forwarded-Ssl     on;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto   $scheme;
    proxy_pass http://gitlab-workhorse;
  }
}

在模板内容中需要修改以下几个地方:

server_name

在上面配置文件中引用了三个额外的配置文件:
https强制跳转配置文件:

rewrite.conf;

https访问配置文件:

ssl.conf;

pagespeed加速配置文件:

pagespeed.conf;

这三个配置文件是按照博客中LNMP环境搭建及配置https访问LNMP环境中生成的配置文件,如果没有这三个配置文件请去博客中找到这三个配置文件,ssl.conf可能有点特殊,因为在之前的记录中也没有详细写出配置文件的内容我这里把它的内容写出来:

ssl on;
add_header Strict-Transport-Security "max-age=31536000;includeSubdomains; preload;";
ssl_certificate /usr/local/nginx/conf/1_xxx.com_bundle.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!LOW:!aNULL:!eNULL;
ssl_session_cache    shared:SSL:20m;
ssl_session_timeout  20m;

我这里用到的crt和key文件是从ssl站点生成完成之后下载下来的,修改完之后保存gitlab.conf,接下来使配置生效:

6、使GitLab配置和Nginx配置生效:

使GitLab配置生效:

sudo gitlab-ctl reconfigure

重启Nginx:

/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s reopen

7、设置邮件系统,使用postfix发送邮件通知:

在第一步我们安装并启动了postfix,现在测试一下用postfix发送一封邮件给自己:

echo "Test mail from postfix" | mail -s "Test Postfix" 你的邮件地址

如果没有错误提示,表示发送成功,接下来登录你的邮箱,查看收到的邮件,邮件的主题是Test Postfix,内容是Test mail from postfix,当邮箱收到系统发送来的邮件时,将系统的地址复制下来,如:root@iZu136qbxolZ.localdomain,打开/etc/gitlab/gitlab.rb,修改以下内容:

gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'root@iZu136qbxolZ.localdomain'
gitlab_rails['gitlab_email_display_name'] = 'git.xxx.com'

保存之后重新配置GitLab

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

重启nginx

nginx -s reload
nginx -s reopen

配置完成之后,就有了邮件通知,如果为了更好的使用邮件通知系统,也可以使用smtp方式发送邮件,使用方法如下,继续修改配置文件/etc/gitlab/gitlab.rb

# Use smtp instead of sendmail/postfix.
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "需要的QQ邮箱"
gitlab_rails['smtp_password'] = "密码"
gitlab_rails['smtp_domain'] = "smtp.qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true

smtp配置文件中根据实际情况修改对应的smtp服务信息,详细配置可看官方说明:GitLab配置SMTP

8、GitLab在阿里云ECS上的优化

使用GibLab一段时间后发现服务器越来越卡,尤其是阿里云的ECS,由于开启swap分区会导致硬盘IO性能下降,因此阿里云服务器初始状态未配置swap,这样就导致GitLab偶尔还会出现500/502错误,原因就是内存不足,可以使用如下方法修改阿里云的swap空间:

①、需要添加2G的SWAP分区/var/swap,则获取2G的文件块:
dd if=/dev/zero of=/var/swap bs=1024 count=2097152
②、创建swap文件:
/sbin/mkswap /var/swap
③、激活swap文件:
/sbin/swapon /var/swap
④、查看一下swap是否正确:
/sbin/swapon -s
⑤、加到fstab文件中让系统引导启动:
echo "/var/swap swap swap defaults 0 0" >>/etc/fstab

PS.阿里云默认在启动项里关闭了SWAP分区,在/etc/rc.d/rc.local文件里有一行 swapoff -a,表示禁用swap,想要使用SWAP分区把这行删掉就好了,如果没有就不用管。

安装完成之后访问git.xx.com,要求输入用户名密码,安装完成后GitLab默认的管理员用户是:

root

默认密码是:

5iveL!fe

更新GitLab:

sudo yum -y update gitlab-ce

9、GitLab设置是否开机启动

禁止GitLab开机自启动:

sudo systemctl disable gitlab-runsvdir.service

启用Gitlab开机自启动:

sudo systemctl enable gitlab-runsvdir.service

本文讲解到此为止,GitLab的基本配置已经完成,在使用过程中遇到的问题,GitLab一般都会有说明,更深层次的问题可以去官网索取答案。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐