概述
本帖针对Centos6/REHL6系统
Gitlab的安装过程主要包括以下组件的配置:
# 修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled ,然后重启电脑
# sestatus -v 查看selinux状态
Current mode: permissive #说明已关闭selinux
1.安装软件包及解决依赖项
添加EPEL源:
1
2
3
4
5
6
|
wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL- 6 https:
rpm -- import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL- 6
# 安装`epel-release-latest- 6 .noarch.rpm`包,启用EPEL
rpm -Uvh http:
yum groupinstall "Development tools"
yum install autoconf automake bison build-essential byacc checkinstall cmake cpio crontabs curl curl-devel db4-devel expat-devel gcc-c++ gdbm-devel gettext gettext-devel glibc-devel libcurl4-openssl-dev libexpat1-dev libffi libffi-dev libffi-devel libgdbm-dev libicu libicu-dev libicu-devel libkrb5-dev libncurses5-dev libreadline-dev libssl-dev libtool libxml2 libxml2-dev libxml2-devel libxslt libxslt-dev libxslt-devel libyaml libyaml-dev libyaml-devel libz-dev logrotate logwatch make ncurses-devel openssh-server openssl-devel patch pcre-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-Time-HiRes pkg-config postfix python-devel python-docutils readline readline-devel sqlite-devel sudo system-config-firewall-tui tcl-devel vim wget zlib1g-dev zlib-devel
|
安装git
如果已经用yum安装过git,并且版本低于2.7.4,要先卸载掉旧的版本
使用源码编译安装git
1
2
3
4
5
6
7
8
9
10
11
12
|
mkdir /tmp/git && cd /tmp/git
curl -O --progress https:
tar zxvf git- 2.8 . 5 .tar.gz
cd git- 2.8 . 5
./configure
make prefix=/usr/local all
# 安装到/usr/local/bin
sudo make prefix=/usr/local install
# 验证git版本号
git --version
#创建软连接
ln -s /usr/local/bin/git /usr/bin/git
|
2.添加系统用户
我们添加一个用来管理运行Gitlab的用户git
1
2
3
4
5
6
7
|
adduser --system --shell /bin/bash --comment 'GitLab' --create-home --home-dir /home/git/ git
# 修改git用户的环境变量PATH,以root用户运行
visudo
# 找到下面一行
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
#修改为
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
|
3.安装ruby环境
在Gitlab生产环境使用Ruby版本管理工具RVM,rbenv或者chruby常常会带来很多疑难杂症.比如Gitlab-shell版本管理器调用OpenSSH的功能以防止越过ssh对仓库进行pull和push操作.而前面提到的三个版本管理器不支持这样的功能,所以我们强烈建议大家按照下面的方式来安装Ruby.
Note: The current supported Ruby (MRI) version is 2.3.x. GitLab 9.0 dropped
support for Ruby 2.1.x.
-
如果系统上存在旧的Ruby1.8,先删除掉:
-
下载Ruby源码,编译安装:
1
2
3
4
5
6
7
8
9
10
11
|
mkdir /tmp/ruby && cd /tmp/ruby
# 这里替换官方文档的下载地址为mirrors.ustc.edu.cn提供的镜像地址
curl -O --progress https:
tar zxvf ruby- 2.3 . 3 .tar.gz
cd ruby- 2.3 . 3
./configure --disable-install-rdoc
make
sudo make install
安装完成后,重新登录终端确保$PATH生效,检测ruby的安装成功与否:
ruby -v
ln -s /usr/local/bin/ruby /usr/bin/ruby
|
-
国内使用Ruby的Gem和Bundler必须要做的事情:
1
2
3
4
5
6
7
|
# 修改git用户gem安装源为淘宝
sudo -u git -H gem sources --add https:
# 确保git用户当前gems源为淘宝
sudo -u git -H gem sources -l
*** CURRENT SOURCES ***
备:gems源中科大: https:
|
-
安装bundle包(root用户)
1
|
sudo gem install bundler --no-ri --no-rdoc
|
4.安装GO
从Gitlab8.0开始,Git的HTTP请求由gitlab-git-http-server来处理.我们需要Go编译器来安装gitlab-git-http-server.下面一系列的指令都将假定你用的是64位的Linux系统.你也可以在GoLang官方网站下载其他平台的Go编译器.
1
2
3
4
5
6
7
8
9
10
11
|
#删除旧的文件夹
sudo rm -rf /usr/local/go
mkdir /tmp/go && cd /tmp/go
curl -O --progress https:
tar -C /usr/local/ -xzf go1. 8.3 .linux-amd64.tar.gz
sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/
rm go1. 8.3 .linux-amd64.tar.gz
#验证go是否安装正确
# go version
go version go1. 8.3 linux/amd64
|
5.安装Node
因为 GitLab 8.17, GitLab 需要使用node >= v4.3.0 编译javascript 资产和 yarn >= v0.17.0 to 管理 javascript
的依赖.在许多发行版的官方套件库提供的版本已经过时了,所以我们需要通过以下命令安装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# install node v7.x
curl --silent --location https:
# 设置镜像
npm config set registry=http:
# 安装nodejs
sudo yum install nodejs
#验证安装情况
node -v
npm -v
# install yarn
sudo wget https:
sudo yum install yarn
#验证安装情况
yarn --version
|
6.安装数据库
Gitlab官方建议我们用PostgreSQL数据库.如果喜欢用Mysql请前往Gitlab使用Mysql数据库的安装说明.
配置postgresql安装源:
https://wiki.postgresql.org/wiki/YUM_Installation#Configure_your_YUM_repository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# 修改/etc/yum.repos.d/CentOS-Base.repo,在[base]和[update]段落添加下面的配置
exclude=postgresql*
# 安装postgresql源
yum localinstall http:
# 安装postgresql
yum install postgresql95-server postgresql95-devel postgresql95-contrib
# 默认情况下,postgresql的数据库文件存放在
/ var /lib/pgsql/ 9.5 /data
# 初始化
mv /etc/init.d/{postgresql- 9.5 ,postgresql}
service postgresql initdb
# 启动postgresql
service postgresql start
# 配置postgresql自启动
chkconfig postgresql on
# 为Gitlab创建一个用户,用户名为git
cd /home
sudo -u postgres psql -d template1 -c "CREATE USER git CREATEDB;"
#创建pg_trgm扩展 (required for GitLab 8.6 +):
sudo -u postgres psql -d template1 -c "CREATE EXTENSION IF NOT EXISTS pg_trgm;"
# 创建Gitlab生产环境数据库并赋予git用户属主权限
sudo -u postgres psql -d template1 -c "CREATE DATABASE gitlabhq_production OWNER git;"
# 用git用户测试下是否能登录刚才创建的数据库
sudo -u git -H psql -d gitlabhq_production
#检查是否启用 pg_trgm 扩展:
SELECT true AS enabled
FROM pg_available_extensions
WHERE name = 'pg_trgm'
AND installed_version IS NOT NULL;
如果启用了扩展,这将产生以下输出:
enabled
---------
t
( 1 row)
# 退出数据库会话
gitlabhq_production> \q
# 创建pg_config的软连接
ln -s /usr/pgsql- 9.5 /bin/pg_config /usr/bin/pg_config
|
7.Redis
版本要求: redis版本不低于2.8.
添加redis用户和组
1
|
groupadd redis && useradd -g redis redis -s /sbin/nologin
|
-
编译安装redis
1
2
3
4
5
6
7
|
mkdir /tmp/redis && cd /tmp/redis
curl -O --progress http:
tar zxvf redis- 3.2 . 9 .tar.gz
cd redis- 3.2 . 9
make && make install
mkdir -p /etc/redis
cp redis.conf /etc/redis/
|
-
修改redis配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig
# 把 'post' 设置为 0 以禁止监听TCP端口
sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf
# 让redis以socket方式启动
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf
#给所有成员或redis组赋予权限
echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf
# 启动守护进程
sed -i 's/daemonize no/daemonize yes/g' /etc/redis/redis.conf
# 创建存放socket的目录
mkdir / var /run/redis
sudo chown redis:redis / var /run/redis
sudo chmod 755 / var /run/redis
# Persist the directory which contains the socket, if applicable
if [ -d /etc/tmpfiles.d ]; then
echo 'd /var/run/redis 0755 redis redis 10d -' | sudo tee -a /etc/tmpfiles.d/redis.conf
fi
# 把git用户加入redis组
sudo usermod -aG redis git
# 下载redis init 脚本
curl -L http:
chmod +x /etc/init.d/redis-server
|
-
启动Redis
1
2
3
4
5
6
|
# 启动redis服务
service redis-server start
# 将redis加入自启动
chkconfig redis-server on
# 查看redis进程是否启动
ps aux |grep redis
|
8.安装GitLab-CE
1
2
|
# 我们将gitlab安装到git用户的HOME目录
cd /home/git
|
克隆gitlab-ce源码
1
|
sudo -u git -H git clone https:
|
Note: 你可以修改9-3-stable为master,这样就可以体验到最新的版本,但是生产环境不要用master分支哦
配置GitLab-CE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# 进入gitlab目录
cd /home/git/gitlab
# 复制gitlab.yml(Gitlab的主配置文件)
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
# 修改gitlab.yml
sudo -u git -H vim config/gitlab.yml
####修改第32行 host: localhost为 host: 你的域名或者ip
####修改第524行 bin_path: /usr/bin/git 为bin_path: /usr/local/bin/git
# 复制 secrets 文件
sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml
# 修改 log/ 和 tmp/ 文件夹权限
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/
# 修改 tmp/pids/ 和 tmp/sockets/ 文件夹权限
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/
# 创建 public/uploads/ 文件夹
sudo -u git -H mkdir public/uploads/
# 修改 public/uploads/ 文件夹权限,只有git用户有访问权限
sudo chmod 0700 public/uploads
# 修改 CI build traces are stored 文件夹的权限
sudo chmod -R u+rwX builds/
# 修改shared/artifacts/文件夹的权限
sudo chmod -R u+rwX shared/artifacts/
# 修改shared/pages/文件夹的权限
sudo chmod -R ug+rwX shared/pages/
# 复制 Unicorn 配置文件
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
# 查询CPU核心数
nproc
# 如果你想搭建一个高负载的Gitlab实例,可启用集群模式.
# 修改'worker_processes'参数,至少要跟cpu核心数一样.
# 修改监听地址和端口,要和下文 gitlab-shell/config.yml 中配置一致
sudo -u git -H vim config/unicorn.rb
worker_processes 3
listen "your_IP:8080", :tcp_nopush => true
# 复制Rack attack 配置文件
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
# 为 git 用户配置用户和邮件
sudo -u git -H git config --global user.name "Gitlab"
sudo -u git -H git config --global user.email "gitlab@your_domain_name"
# 'autocrlf' 需要Web编辑器
sudo -u git -H git config --global core.autocrlf input
# 禁止 'git gc --auto' 因为需要时 GitLab 已经运行 'git gc'
sudo -u git -H git config --global gc.auto 0
# Enable packfile bitmaps
sudo -u git -H git config --global repack.writeBitmaps true
# 复制 Redis 连接配置文件
sudo -u git -H cp config/resque.yml.example config/resque.yml
# 如果之前修改过redis socket的路径,在这个配置文件里面修改为当前的路径.
sudo -u git -H vim config/resque.yml
|
修改GitLab DB 设置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 仅限于PostgreSQl:
sudo -u git cp config/database.yml.postgresql config/database.yml
# 仅限于Mysql:
###sudo -u git cp config/database.yml.mysql config/database.yml
# 以下修改针对MySQL和远程PostgreSQL,修改username/password.
# 修改 'secure password' 为你设置的密码,没单独设置则不改
sudo -u git -H vim config/database.yml
# PostgreSQL MySQL都适用:
# 修改database.yml的权限,确保git用户可以读取该文件.
sudo -u git -H chmod o-rwx config/database.yml
|
安装Gems包
这个步骤是很多新手头疼的问题,不过你只要严格按照本文关于Ruby环境的搭建来做.还是可以保证你顺利的安装下来的.
Note: 自bundler1.5.2起,你可以使用bundle install -jN(N就是cpu核心数)安装Gems,速度比之前要快大约60%.详细的内容可以点此处查看.不过首先要确保你的bundler版本>=1.5.2(运行bundle -v查看).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# 进入gitlab目录
cd /home/git/gitlab
gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
# 修改 Gemfile 和 Gemfile.lock
vim Gemfile(Gemfile.lock)
更改
source https://rubygems.org/
为:
source 'https://ruby.taobao.org/'
# 确保只有 https://ruby.taobao.org/
gem sources -l
https://ruby.taobao.org/
# 升级gem
gem update --system
gem -v
####一定要注意选择自己用的数据库的命令
# PostgreSQL (note, the option says "without ... mysql")
sudo -u git -H bundle install --deployment --without development test mysql aws kerberos
# 如果使用 MySQL,执行下面的命令 (note, the option says "without ... postgres")
###sudo -u git -H bundle install --deployment --without development test postgres aws kerberos
笔记: 如果你想去用 Kerberos 做用户认证, 然后在--without选项中省略Kerberos
|
1
2
3
|
----------以下不需要设置,草稿---------------------------
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
----------以上不需要设置,草稿---------------------------
|
安装Gitlab-shell
GitLab Shell是专为GitLab开发的ssh访问和仓库管理的软件.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 修改gitlab 安装 gitlab-shell的rake任务脚本
sudo -u git -H sed -i 's/https:\/\/gitlab.com\/gitlab-org\/gitlab-shell.git/https:\/\/git.oschina.net\/qiai365\/gitlab-shell.git/g' /home/git/gitlab/lib/tasks/gitlab/shell.rake
# 运行安装gitlab shell的任务 (根据自己的redis安装情况修改`REDIS_URL`),这里如果你事先没有clone gitlab-shell的仓库,就会自动clone官方的仓库进行安装:
sudo -u git -H mkdir -p /home/git/repositories
sudo chmod -R ug+rwX,o-rwx /home/git/repositories
sudo chmod -R ug-s /home/git/repositories
sudo find /home/git/repositories -type d -print0 | sudo xargs -0 chmod g+s
# Run the installation task for gitlab-shell (replace `REDIS_URL` if needed):
sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production SKIP_STORAGE_VALIDATION=true
# 默认情况下,gitlab-shell的配置是根据Gitlab的配置生产的.
# 你可以运行下面的命令查看和修改gitlab-shell的配置,
# 监听端口要和/home/git/gitlab/config/unicorn.rb中配置一致
sudo -u git -H vim /home/git/gitlab-shell/config.yml
gitlab_url: http://your_IP:8080
|
Note: Make sure your hostname can be resolved on the machine itself by either a proper DNS record or an additional line in /etc/hosts (“127.0.0.1 hostname”). This might be necessary for example if you set up GitLab behind a reverse proxy. If the hostname cannot be resolved, the final installation check will fail with “Check GitLab API access: FAILED. code: 401” and pushing commits will be rejected with “[remote rejected] master -> master (hook declined)”.
安装成功如图所示:

安装gitlab-workhorse
1
2
3
|
#GitLab-Workhorse 使用 GNU Make. 下面的命令将GitLab-Workhorse 安装在推荐的位置 /home/git/gitlab-workhorse .
cd /home/git/gitlab
sudo -u git -H bundle exec rake "gitlab:workhorse:install[/home/git/gitlab-workhorse]" RAILS_ENV=production
|
1
2
|
#你可以通过提供它作为一个额外的参数来指定一个不同的Git仓库:
sudo -u git -H bundle exec rake "gitlab:workhorse:install[/home/git/gitlab-workhorse,https://example.com/gitlab-workhorse.git]" RAILS_ENV=production
|
1
2
3
4
5
6
7
|
------------------以下为旧安装方法------------------------------------------------
#cd /home/git
#sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
#cd gitlab-workhorse
#sudo -u git -H git checkout 0.6.5
#sudo -u git -H make
------------------以上为旧安装方法------------------------------------------------
|
初始化数据库,激活高级特性
1
2
3
4
5
6
7
8
9
10
11
|
cd /home/git/gitlab
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
# 输入 'yes' 以创建数据库表
# 当看到以下内容,表示已经安装完成
Administrator account created:
login: root
password: your_passwd
== Seed from /home/git/gitlab/db/fixtures/production/010_settings.rb
|
Note: 你能通过提供环境变量设置 Administrator/root 密码和邮箱, 分别为GITLAB_ROOT_PASSWORD 和 GITLAB_ROOT_EMAIL , 如下所示。如果你不能设置密码(它被设置为默认的) 请等待曝光gitlab到公共互联网直到安装完成和你已经登录到服务器的第一时间。 在第一次登录时,您将被迫更改默认密码。.
1
|
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail
|
安全设置 secrets.yml
secrets.yml
文件为每个会话和安全变量存储密钥.把这个文件备份到别的地方,但是不要和数据库备份放在一块,否则你的数据库备份损坏会导致这个文件丢失.
安装Gitlab init脚本
1
2
3
4
5
|
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
#复制下面这个配置文件,如果你的gitlab不是安装在/home/git/gitlab目录,根据自己情况修改这个文件。
sudo cp lib/support/init.d/gitlab.default.example /etc/default/gitlab
|
设置GItlab为自启动
安装Gitaly
1
2
|
# 取用 Gitaly 源 用Git和Go一起编译
sudo -u git -H bundle exec rake "gitlab:gitaly:install[/home/git/gitaly]" RAILS_ENV=production
|
1
2
|
你可以通过提供它作为一个额外的参数来指定一个不同的Git仓库:
sudo -u git -H bundle exec rake "gitlab:gitaly:install[/home/git/gitaly,https://example.com/gitaly.git]" RAILS_ENV=production
|
接下来, 确保gitaly配置:
1
2
3
4
5
6
7
|
# 限制 Gitaly sockets访问
sudo chmod 0700 /home/git/gitlab/tmp/sockets/private
sudo chown git /home/git/gitlab/tmp/sockets/private
# 如果你正在用non-default 设置 你必须升级config.toml
cd /home/git/gitaly
sudo -u git -H vim config.toml
|
For more information about configuring Gitaly see doc/administration/gitaly.
设置Logrotate
1
2
|
cd /home/git/gitlab
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
|
检查GitLab环境配置
1
|
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
|
效果如图

生成GitLab前端资源
1
2
|
sudo -u git -H yarn install --production --pure-lockfile
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
|
编译GetText PO文件
1
|
sudo -u git -H bundle exec rake gettext:compile RAILS_ENV=production
|
通过修改/home/git/gitlab/config/unicorn.rb
的listen端口,然后重启gitlab服务,就可以直接访问服务器ip加端口来访问gitlab了

启动GitLab
1
2
3
|
sudo service gitlab start
# 或者
sudo /etc/init.d/gitlab restart
|
Done!再检查一次Gitlab的所有组件
1
2
|
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
# 如果上面的检查有错误,按照提示修复下,再重启GitLab即可
|
8.安装Nginx
Note: Nginx is the officially supported web server for GitLab. If you cannot or do not want to use Nginx as your web server, have a look at the GitLab recipes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
mkdir /tmp/nginx && cd /tmp/nginx
wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install nginx
# 站点配置,复制示例站点配置
cd /home/git/gitlab
sudo cp lib/support/nginx/gitlab /etc/nginx/conf.d/gitlab.conf
# 编辑配置文件。如果其他用户安装Git,修改gitlab路径
sudo vim /etc/nginx/conf.d/gitlab.conf
server_name YOUR_SERVER_FQDN; #修改你的域名地址
listen 80 default_server;
#检查nginx配置
sudo nginx -t
#启动nginx
sudo service nginx restart
|
恭喜,安装完成!
修改MySQL 字符串的限制,仅限mysql数据库:
bundle exec rake add_limits_mysql
参考:https://bbs.gitlab.cc/topic/35/gitlab-ce-8-7-%E6%BA%90%E7%A0%81%E5%AE%89%E8%A3%85%E6%89%8B%E5%86%8C-centos6-rehl6/2
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md
本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1767438如需转载请自行联系原作者
qianghong000