企业级GitLab仓库环境构建

目录:
1、gitlab简介
2、安装配置gitlab
    2.1、实验环境介绍
    2.2、更改仓库存储位置
    2.3、开启https访问
    2.4、启用SMTP服务
3、GitLab的基本使用
4、备份及恢复
5、总结

1、gitlab简介

GitLab 是一个用于仓库管理系统的开源项目。使用Git作为代码管理工具,并在此基础上搭建起来的web服务。GitLab拥有强大的功能,可实现git仓库管理,代码审查,问题跟踪,WIkI等功能,而且配合GitLab CI能更简单的实现持续集成和自动部署。GitLab的组件主要包括:Packages / Dependencies,Ruby,Go,System Users,Database,Redis,GitLab,Nginx,详细信息请见(http://doc.gitlab.com/ce/install/installation.html)。
2、安装配置gitlab

企业级GitLab仓库环境构建

GitLab的部署方式有两种,一种是源代码方式部署,另一种是用通用包部署,官方称为 “Omnibus package installation”部署方式。源代码部署方式工作量大,且容易出错,官方强烈建议使用通用包的方式来部署。“Omnibus package installation”这种方式部署的GitLab会在有GitLab进程崩溃时会使用Runit来重启GitLab的进程,如果从源代码来安装GitLab,则没有Runit这种管理方式。所以建议大家还是以通用包的方式来部署。

2.1、实验环境介绍
平台:Debian 8.1 x64
IP地址:192.168.207.128
GitLab包:gitlab-ce_8.2.1-ce.0_amd64.deb    软件包下载地址:http://mirror.tuna.tsinghua.edu.cn/gitlab-ce/,请根据自己的平台选择下载,可惜在完成此博客时好像被墙了。
注意:在安装GitLab时请确保主机端口80没有被占用,在一次测试中因主机默认安装了nginx,并在开机时启动,导致当我部署好GitLab后访问首页时只出现nginx的欢迎页面,这个问题困扰了好久,后来才发现是主机默认已监听在了80端口。

root@test1:~/tools# pwd
/root/tools
root@test1:~/tools# ls
gitlab-ce_8.2.1-ce.0_amd64.deb
root@test1:~/tools# dpkg -i gitlab-ce_8.2.1-ce.0_amd64.deb
正在选中未选择的软件包 gitlab-ce。
(正在读取数据库 ... 系统当前共安装有 94237 个文件和目录。)
正准备解包 gitlab-ce_8.2.1-ce.0_amd64.deb  ...
正在解包 gitlab-ce (8.2.1-ce.0) ...
正在设置 gitlab-ce (8.2.1-ce.0) ...
gitlab: Thank you for installing GitLab!
gitlab: To configure and start GitLab, RUN THE FOLLOWING COMMAND:
sudo gitlab-ctl reconfigure
gitlab: GitLab should be reachable at http://test1.cstonline.net
gitlab: Otherwise configure GitLab for your system by editing /etc/gitlab/gitlab.rb file
gitlab: And running reconfigure again.
gitlab: 
gitlab: For a comprehensive list of configuration options please see the Omnibus GitLab readme
gitlab: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
gitlab: 
It looks like GitLab has not been configured yet; skipping the upgrade script.

这样GitLab的安装工作就结束了,在输出信息中可以看到有一个链接(https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md),这个链接很重要的,从这里可以获取到GitLab的所有配置的详细信息,有时间可以好好读读。
GitLab安装好后,可以查看一下它都把程序安装在哪里去了,用如下命令:
root@test1:~/tools# dpkg -L gitlab-ce | less

#输出信息相当多,请加上less分屏显示

2.2、更改仓库存储位置
默认时GitLab的仓库存储位置在“/var/opt/gitlab/git-data/repositories”,在实际生产环境中显然我们不会存储在这个位置,一般都会划分一个独立的分区来存储仓库的数据,我这里规划把数据存放在“/data/git-data”目录下。

root@test1:~/tools# mkdir -pv /data/git-data
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/git-data"
root@test1:~# chown -R git.git /data/git-data  #修改创建目录的属主和属组为git用户
root@test1:~/tools# cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.bak
root@test1:~/tools# vim /etc/gitlab/gitlab.rb
#启用git_data_dir参数,并修改如下:
git_data_dir "/data/git-data"
#并修改external_url的值修改为规划的访问域名
external_url 'http://test.gitlab.net'
root@test1:~/tools# gitlab-ctl reconfigure  #重新编译gitlab.rb文件,使用做的修改生效
重新编辑后,GitLab在仓库目录会自动创建一个repositories文件,如下:
root@test1:~# ls -ld /data/git-data/repositories/
drwxrws--- 2 git git 4096 1月  4 14:15 /data/git-data/repositories/

在Windows主机的hosts里做好域名解析后访问我们的gitlab,如下图:

企业级GitLab仓库环境构建

默认的用户为“root”,密码为“5iveL!fe”,为了安全第一次登陆时会要求你修改登陆密码,如下图:

企业级GitLab仓库环境构建

到这里GitLab就基本可用了,但在企业中运用一般不会直接走http协议,都会用https这种安全协议来访问GitLab服务。

2.3、开启https访问

默认时, omnibus-gitlab没有启用https,假如我们要为test.gitlab.net域名启用https,那我们就需要为GitLab申请一个合法的证书,如果GitLab只是我们企业内部使用,完全可以自建一个CA,并为此GitLab颁发一个证书。
2.3.1、自建CA
root@test1:~# mkdir -pv /etc/ssl/demoCA/{private,newcerts}
mkdir: 已创建目录 "/etc/ssl/demoCA"
mkdir: 已创建目录 "/etc/ssl/demoCA/private"
mkdir: 已创建目录 "/etc/ssl/demoCA/newcerts"
root@test1:~# cd /etc/ssl
root@test1:/etc/ssl# (umask 077;openssl genrsa -out ./demoCA/private/cakey.pem 2048)  #生成密钥
Generating RSA private key, 2048 bit long modulus
................................................................+++
......................+++
e is 65537 (0x10001)
root@test1:/etc/ssl# openssl req -new -x509 -key ./demoCA/private/cakey.pem -out ./demoCA/cacert.pem -days 3650  #生成自签证书
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:ChongQing
Locality Name (eg, city) []:YuBei
Organization Name (eg, company) [Internet Widgits Pty Ltd]:SJKJ
Organizational Unit Name (eg, section) []:Operation Services 
Common Name (e.g. server FQDN or YOUR name) []:test.gitlab.net    #这里是CA的域名,可以不与gitlab的相同
Email Address []:[email protected]
root@test1:/etc/ssl# touch ./demoCA/index.txt
root@test1:/etc/ssl# echo 01 > ./demoCA/serial

2.3.2、为gitlab申请证书、CA进行颁发

root@test1:~# ls /etc/gitlab/
gitlab.rb  gitlab.rb.bak  gitlab-secrets.json

#gitlab会在"/etc/gitlab/ssl"目录去寻找密钥文件和证书文件,并且证书文件和密钥文件名应与访问gitlab的域名相同,我们这里就是"test.gitlab.net"

root@test1:~# mkdir -p /etc/gitlab/ssl    #此目录路径不能随意更改
root@test1:~# cd /etc/gitlab/ssl
root@test1:/etc/gitlab/ssl# (umask 077;openssl genrsa -out test.gitlab.net.key 1024)
Generating RSA private key, 1024 bit long modulus
.....................++++++
.................................++++++
e is 65537 (0x10001)
root@test1:/etc/gitlab/ssl# ls
test.gitlab.net.key
root@test1:/etc/gitlab/ssl# openssl req -new -x509 -key test.gitlab.net.key -out test.gitlab.net.csr  #生成证书签署请求
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:ChongQing
Locality Name (eg, city) []:YuBei
Organization Name (eg, company) [Internet Widgits Pty Ltd]:SJKJ
Organizational Unit Name (eg, section) []:Operation Services
Common Name (e.g. server FQDN or YOUR name) []:test.gitlab.net
Email Address []:[email protected]
A challenge password []:                  #回车
An optional company name []:        #回车
root@test1:/etc/gitlab/ssl# ls
test.gitlab.net.csr  test.gitlab.net.key
root@test1:/etc/gitlab/ssl# openssl ca -in test.gitlab.net.csr -out test.gitlab.net.crt -days 3650  #CA签署证书
Using configuration from /usr/lib/ssl/openssl.cnf
Error opening CA private key ./demoCA/private/cakey.pem
140185766790800:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('./demoCA/private/cakey.pem','r')
140185766790800:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
unable to load CA private key

在签署证书是报错了,根据报错信息得知在签署证书时会在当前目录下去寻找“ ./demoCA/private/cakey.pem”这个CA的密钥文件,所以应该切换到“/etc/ssl”目录下去执行上边的命令,如下:

root@test1:/etc/ssl# openssl ca -in /etc/gitlab/ssl/test.gitlab.net.csr -out /etc/gitlab/ssl/test.gitlab.net.crt -days 3650
Using configuration from /usr/lib/ssl/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jan  6 01:13:40 2016 GMT
            Not After : Jan  3 01:13:40 2026 GMT
        Subject:
            countryName              = CN
            stateOrProvinceName      = ChongQing
            organizationName          = SJKJ
            organizationalUnitName    = Operation Services
            commonName                = test.gitlab.net
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                52:95:81:FD:1D:D9:CE:40:D8:22:9C:95:8D:D0:8D:1C:A8:7D:78:4D
            X509v3 Authority Key Identifier:
                keyid:05:0A:A9:09:33:18:C3:99:71:19:BD:3F:EA:92:EB:A5:D2:30:72:EB
Certificate is to be certified until Jan  3 01:13:40 2026 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
root@test1:/etc/ssl# ls /etc/gitlab/ssl/
test.gitlab.net.crt  test.gitlab.net.csr  test.gitlab.net.key
root@test1:/etc/ssl# rm -f /etc/gitlab/ssl/test.gitlab.net.csr    #证书签署请求文件可以删除
root@test1:/etc/ssl# chmod 700 /etc/gitlab/ssl  #  更改目录权限
root@test1:/etc/ssl# ls -ld /etc/gitlab/ssl
drwx------ 2 root root 4096 1月  6 09:24 /etc/gitlab/ssl

2.3.3、开启GitLab的https支持
root@test1:/etc/ssl# vim /etc/gitlab/gitlab.rb
# note the 'https' below
external_url 'https://test.gitlab.net'
##### open htts #####################
nginx['redirect_http_to_https'] = true                             
nginx['ssl_certificate'] = "/etc/gitlab/ssl/test.gitlab.net.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/test.gitlab.net.key"

"nginx['redirect_http_to_https'] = true"表示将所有的http流量转发到https上, 下边两行代表GitLab和密钥和证书所在路径,从这里我猜测存放密钥和证书的路径是可以自定义的,只要在这里正确指定即可,这个我没有验证。

root@test1:/etc/ssl# gitlab-ctl reconfigure  #重新编译配置

运行上边的命令会重新编译配置文件并会重新启动GitLab的各个组件的服务,现在再来访问一下我们的GitLab,如下:

企业级GitLab仓库环境构建

直接访问“http://test.gitlab.net”也会被强制定向到https的安全链接。

2.3.4、启用SMTP服务

当在GitHub上注册一个新用户时你会收到一封邮件,邮件里会有一些提示性的信息或者点击一个链接让你更改登陆密码,GitLab也一样可以配置这样的邮件提醒功能,如果你没有自己的邮件服务器,那一般都是配置启用第三方SMTP服务。详情请参照官方https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/smtp.md。官方资料中没有介绍怎样配置163邮箱的SMTP配置方法,这里我就以它为例。
123456789101112 root@test1:~# vim /etc/gitlab/gitlab.rb            #把以下内容追加到最后
##### open smtp ############
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "[email protected]"
gitlab_rails['smtp_password'] = "***********"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "[email protected]"

在启用SMTP时花了不少时间,因为各个参数之间要配合使用,而这个也只能自己不断尝试才行。
root@test1:~# gitlab-ctl reconfigure    #重新编译

现在我们的GitLab就启用了SMTP功能。

猜你喜欢

转载自www.linuxidc.com/Linux/2016-01/127810.htm