gitlab7.14(中文版|英文版)编译安装部署文档(全)

摘要

第一次安装gitlab的时候本来以为整个过程会很简单,结果发现网上的文档或多或少的都有着各种各样的坑,还有就是因为时间的问题,很多文档都是老版本的,总之就是各种各样的问题。于是,在自己填平了好多坑之后,一点一点整理出一套完整、可靠的安装过程,可是一直没有时间好好修饰一番,直到现在。于是,就有了这篇文档。

本文档基于网络上一些文档以及自己的一些操作整合而成,本教程并非离线安装,请确保服务器能够正常连接网络

关于gitlab

GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。

它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。

GitLab 5.0以前版本要求服务器端采用 Gitolite 搭建,5.0版本以后不再使用 Gitolite ,采用自己开发的 gitlab-shell 来实现。如果你觉得安装麻烦可以使用 GitLab Installers 一键安装。

版本说明:

本文安装的是gitlab7.14的版本,目前gitlab最新版本为8.2,而目前网上最多的教程都是6版本的gitlab,7.14版本的安装和6版本大同小异,本文只是做一个整理。

gitlab7相对于6界面变动很大,到现在的8界面也几乎没有变化;而7的安装过程和6基本只有版本的区别,这块8版本的因为加入了go的依赖等,安装过程照比之前变化很大。

1
2
3
4
5
6
7
8
* 系统版本:centos6.5_x86_64
* gitlab:7-14中文版|英文版
* ruby:2.1.6
* mysql:5.5
* nginx:1.9.14
* git: 2.8.2
* redis:yum 版本
* 本文使用ruby-2.1.6,mysql-5.5.39,nginx-1.8.0以及yum安装的redis

安装前准备工作

基本配置

基本的环境配置,关闭防火墙、关闭selinux等,这里不做太多介绍,下面的命令实现永久关闭SELinux

1
sed -i 's/^SELINUX=.*/#&/;s/^SELINUXTYPE=.*/#&/;/SELINUX=.*/a SELINUX=disabled' /etc/sysconfig/selinux

下面的命令实现临时关闭SELinux

1
setenforce 0

安装epel源

1
2
3
4
yum -y install epel*
或者
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm

导入EPEL源的key

1
rpm --import https://www.fedoraproject.org/static/0608B895.txt

修改配置

download.fedoraproject.org域名已经失效
去掉#号将download.fedoraproject.org变更为dl.fedoraproject.org

1
2
3
vim /etc/yum.repos.d/epel.repo
baseurl=http://dl.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch

安装PUIAS源和导入对应key

1
2
3
wget -qO- https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/install/centos/PUIAS_6_computational.repo > /etc/yum.repos.d/PUIAS_6_computational.repo
rpm --import http://springdale.math.ias.edu/data/puias/6/x86_64/os/RPM-GPG-KEY-puias

重新建立源缓存和检测key导入是否成功

1
2
3
yum clean all && yum makecache
rpm -qa gpg*| grep '0608b895'
rpm -qa gpg*| grep '41a40948'

安装Development Tools开发组包和一些软件所需依赖包

不要用yum安装ruby、git、nginx和mysql;redis可以使用yum安装的版本

1
yum -y install readline readline-devel ncurses-devel gdbm-devel glibc-devel tcl-devel expat-devel db4-devel byacc sqlite-devel libyaml libyaml-devel libffi libffi-devel libxml2 libxml2-devel libxslt libxslt-devel libicu libicu-devel system-config-firewall-tui python-devel redis wget crontabs logwatch logrotate perl-Time-HiRes gettext gettext-devel openssl-devel zlib-devel gcc gcc-c++ make autoconf tk-devel python-pip patch pcre-devel curl curl-devel sudo yum-plugin-fastestmirror cmake perl-CPAN nodejs automake libxml* libmcrypt* libtool-ltdl-devel* yum-utils

卸载已经安装的包

1
yum remove ruby git nginx mysql mysql-server

Development Tools开发组包我没有安装

1
yum -y groupinstall 'Development Tools'

同步时间

1
2
3
4
5
6
7
8
rm -rf /etc/localtime
ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum -y install ntp
ntpdate pool.ntp.org
echo '*/20 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1' >>/var/spool/cron/root

安装git

下载源码包

1
2
3
4
5
6
7
8
9
wget https://www.kernel.org/pub/software/scm/git/git-2.5.4.tar.gz
wget https://www.kernel.org/pub/software/scm/git/git-2.8.2.tar.gz
tar zxvf git-2.8.2.tar.gz && cd git-2.8.2
./configure && make prefix=/usr/local all && make prefix=/usr/local install
ln -fs /usr/local/bin/git* /usr/bin/

找到git的路径添加到环境变量

1
2
3
4
5
6
which git
echo 'export PATH="$PATH:/usr/local/bin"' >> /root/. bashrc
echo 'export PATH="$PATH:/usr/local/bin"' >> /etc/profile
source /etc/profile

执行之后有版本信息证明gitserver安装成功

1
git --version

在执行git –version的时候有可能会报错找不到这个库文件

1
2
ln -s /usr/local/lib/libcharset.so.1 /lib/libcharset.so.1
ldconfig

安装ruby

下载ruby源码包

1
wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.gz

编译安装ruby

1
2
3
tar zfvx ruby-2.2.4.tar.gz && cd ruby-2.2.4
./configure --disable-install-rdoc
make clean && make && make prefix=/usr/local install

安装bundler需要添加rubygems的国内镜像

1
2
3
#gem source --remove|-r --add|-a
gem source -r https://rubygems.org/
gem source -a https://ruby.taobao.org/

确保gem源中只有淘宝源

1
2
3
4
5
6
7
gem sources -l
*** CURRENT SOURCES ***
https://ruby.taobao.org/
ln -s /usr/local/bin/ruby /usr/bin/ruby
ln -s /usr/local/bin/gem /usr/bin/gem
ln -s /usr/local/bin/bundle /usr/bin/bundle

安装bundler

1
gem install bundler --no-ri --no-rdoc

安装nginx

1
2
3
4
5
6
7
wget http://nginx.org/download/nginx-1.9.14.tar.gz
tar xvf nginx-1.9.14.tar.gz && cd nginx-1.9.14
./configure --prefix=/usr/local/nginx/ --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre
make && make install

安装gitlab

创建一个git用户供gitLab使用

之前已经搭建过git Server不需要再次创建git用户,如果要纯净安装的话可以删除之前的用户.

1
userdel -r git

这里需要注意的是用户名一定要是git,如果换成别的需要改很多配置文件,不建议这样做

因为gitlab创建仓库以后所有文件都是存在git用户下的,如果git家目录不够大是不行的,而git很多配置文件中都有/home/git这个路径,如果要修改git默认的家目录需要修改很多配置文件,切记

因我的home空间不是很大,这里我对git指定了家目录,后面有需要修改的配置文件操作,以下面的两行#注释

1
2
3
4
5
6
##################################
useradd --comment 'gitLab' git -d /data/git
##################################
useradd --comment 'gitLab' git
passwd git

为了方便添加git用户拥有root权限 [sudoers文件默认没有写权限需要强制保存:wq!],或者直接把sudo信息添加到sudo文件中

1
2
3
echo "git ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
chmod -R 755 /data/git
echo "export git_SSL_NO_VERIFY=1" >> /home/git/. bash_profile

不添加变量的话使用https链接会报如下错误,不过本文没有使用https

1
fatal: unable to access 'https://github.com/gitlabhq/grit.git/': Peer certificate cannot be authenticated with known CA certificates

配置redis使用socket

备份之前配置,修改端口号,添加socket配置,添加socket权限(>= redis 2.4.0).,启动服务

1
2
3
4
5
cp /etc/redis.conf /etc/redis.conf.orig
sed 's/^port .*/port 0/' /etc/redis.conf | sudo tee /etc/redis.conf
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis.conf
sudo sed -i '/# unixsocketperm/ s/^# unixsocketperm.*/unixsocketperm 0775/' /etc/redis.conf
sudo service redis restart

将git用户添加到redis组

1
2
sudo usermod -aG redis git
chkconfig redis on

建立gitlab数据库并授权

编译安装mysql-5.5.39

添加mysql用户

1
2
3
4
groupadd mysql
mkdir -pv /data/mysql
useradd -r -g mysql -d /data/mysql -s /sbin/nologin mysql
chown -R mysql.mysql /data/mysql

获取安装包

1
2
3
wget https://downloads.mariadb.com/archives/mysql-5.5/mysql-5.5.39.tar.gz
tar -xvf mysql-5.5.39.tar.gz && cd mysql-5.5.39

编译安装

1
2
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.5 -DDEFAULT_CHARSET=utf8 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DWITH_EXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DDEFAULT_COLLATION=utf8_general_ci
make && make install

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
chown -R mysql.mysql /usr/local/mysql5.5
cd /usr/local/mysql5.5/support-files/
cp my-medium.cnf /data/mysql/my.cnf
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
cd /usr/local/mysql5.5/scripts/
./mysql_install_db --user=mysql --basedir=/usr/local/mysql5.5 --datadir=/data/mysql
vim /data/mysql/my.cnf
datadir=/data/mysql
/etc/init.d/mysqld start
chkconfig mysqld on

将mysql添加到环境变量

1
2
3
4
5
6
7
echo 'export PATH="$PATH:/usr/local/bin:/usr/local/mysql5.5/bin"' >> /root/. bashrc
echo 'export PATH="$PATH:/usr/local/bin:/usr/local/mysql5.5/bin"' >> /etc/profile
source /etc/profile
mysql -u root

添加git连接mysql的用户以及数据库

1
2
3
>CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
>GRANT SELECT,LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost' IDENTIFIED BY 'gitlab';
>flush privileges;

测试git用户是否可以使用刚才创建的mysql用户登录

1
2
su - git
mysql -u gitlab -p -D gitlabhq_production

安装gitLab-Shell

切换git用户

1
su - git

克隆gitlab的shell

1
2
3
4
git clone https://gitlab.com/gitlab-org/gitlab-shell.git -b v2.7.2
cd gitlab-shell/
cp config.yml.example config.yml
vi config.yml

配置自己的gitlab域名和端口

1
gitlab_url: "http://git.cass.com:8050"

#下面修改的内容为git指定家目录后的操作,如果是默认的/home/git则不需要修改,后面不再提示

1
2
3
4
##################################
repos_path: "/data/git/repositories"
auth_file: "/data/git/.ssh/authorized_keys"
##################################

配置gitlab-shell使用reidis-socket

1
2
3
4
5
6
7
8
redis:
bin: /usr/bin/redis-cli
# host: 127.0.0.1
# port: 6379
# pass: redispass # Allows you to specify the password for Redis
database: 0
socket: /var/run/redis/redis.sock # Comment out this line if you want to use TCP
namespace: resque:gitlab

完成配置

1
2
3
4
5
6
7
8
9
./bin/install
###sudo -u git -H ./bin/install###
mkdir -p /home/git/repositories: OK
mkdir -p /home/git/.ssh: OK
chmod 700 /home/git/.ssh: OK
touch /home/git/.ssh/authorized_keys: OK
chmod 600 /home/git/.ssh/authorized_keys: OK
chmod ug+rwX,o-rwx /home/git/repositories: OK

克隆gitLab源

这两个克隆的目录是可以打包多次使用的,网速慢的话可以选择打包留存一下

1
2
3
4
5
6
7
8
9
su - git && cd ~
git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-14-stable gitlab
##中文版7.14##以下url都可以
git clone https://gitlab.com/jieme/gitlab.git -b 7-14-zh gitlab
git clone https://git.oschina.net/Yxnt/gitlab.git -b 7-14-zh gitlab # 这个比较快
git clone https://gitlab.com/ooppwwqq0/gitlab.git -b 7-14-zh gitlab
cd ~/gitlab
cp config/gitlab.yml.example config/gitlab.yml

修改配置文件

1
2
3
4
5
6
7
8
9
10
vim config/gitlab.yml
################
:%s/home/data/g
################
## gitLab settings
gitlab:
## Web server settings
host: git.cass.com
port: 8050
https: false

如果准备使用smtp方式发送邮件,这里可以修改为发送邮件的账号,如果使用系统sendmail就不用修改,本人使用smtp方式

1
2
3
email_from: noreply@cass.com
email_display_name: GitLab
email_reply_to: noreply@cass.com

这里是默认的用户创建仓库数量,无需修改,创建账号的时候可以修改

1
2
## User settings
default_projects_limit: 10

ldap配置,本文没有配置整合ldap,需要的自己尝试一下吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ldap:
enabled: false
host: '_your_ldap_server'
base: '_the_base_where_you_search_for_users'
port: 636
uid: 'sAMAccountName'
method: 'ssl' # "ssl" or "plain"
bind_dn: '_the_full_dn_of_the_user_you_will_bind_with'
password: '_the_password_of_the_bind_user'
allow_username_or_email_login: true
##################################
path: /data/git/gitlab-satellites/
gitlab_shell:
path: /data/git/gitlab-shell/
# REPOS_PATH MUST NOT BE A SYMLINK!!!
repos_path: /data/git/repositories/
hooks_path: /data/git/gitlab-shell/hooks/
##################################

ssh端口,需要的自行修改

1
#ssh_port: 22

下面是编译安装git后的路径,默认文件最大大小和超时时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
git:
bin_path: /usr/local/bin/git
max_size: 524288000 # 5.megabytes
timeout: 300
cp config/unicorn.rb.example config/unicorn.rb
vim config/unicorn.rb
###################################
grep "/data/git/" config/unicorn.rb
working_directory "/data/git/gitlab" # available in 0.94.0+
listen "/data/git/gitlab/tmp/sockets/gitlab.socket", :backlog => 64
pid "/data/git/gitlab/tmp/pids/unicorn.pid"
stderr_path "/data/git/gitlab/log/unicorn.stderr.log"
stdout_path "/data/git/gitlab/log/unicorn.stdout.log"
###################################

默认8080端口,如果被占用自行修改,我也不确定这个是干什么用的

1
2
listen "127.0.0.1:8060", :tcp_nopush => true
timeout 300

修改连接redis配置

1
cp config/resque.yml.example config/resque.yml

修改配置文件

1
2
3
4
vim config/resque.yml
development: redis://localhost:0
test: redis://localhost:0
production: unix:/var/run/redis/redis.sock

修改连接数据库配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
cp config/database.yml.mysql config/database.yml
vim config/database.yml
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: gitlabhq_production
pool: 5
username: gitlab
password: "gitlab"
# host: localhost
# socket: /tmp/mysql.sock

创建所需目录以及配置相应权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd ~/gitlab
mkdir /home/git/gitlab-satellites
chown -R git log/
chown -R git tmp/
chmod -R u+rwX log/
chmod -R u+rwX tmp/
chmod -R u+rwX tmp/pids/
chmod -R u+rwX tmp/sockets/
chmod -R u+rwX public/uploads
cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
git config --global user.name "GitLab"
git config --global user.email "gitlab@cass.org"
git config --global core.autocrlf input

安装gems

1
2
3
4
5
6
7
8
9
10
11
12
cd ~/gitlab
sudo gem install charlock_holmes --version '0.6.9'
vim Gemfile
source "https://rubygems.org" 改为
source "https://ruby.taobao.org"
vim Gemfile.lock
remote: https://rubygems.org/ 改为
remote: https://ruby.taobao.org/
#执行:
bundle install --deployment --without development test postgres puma aws

对数据库进行初始化

1
2
3
4
5
6
7
8
9
bundle exec rake gitlab:setup RAILS_ENV=production --verbose
This will create the necessary database tables and seed the database.
You will lose any previous data stored in the database.
Do you want to continue (yes/no)? yes
#最后初始化成功后会获得账号和密码
Administrator account created:
login.........root
password......5iveL!fe

检查gitLab及其环境的配置是否正确

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
cd ~/gitlab
bundle exec rake gitlab:env:info RAILS_ENV=production
System information
System: CentOS 6.5
Current User: git
Using RVM: no
Ruby Version: 2.2.4p230
Gem Version: 2.4.5.1
Bundler Version:1.12.3
Rake Version: 10.4.2
Sidekiq Version:3.3.0
GitLab information
Version: 7.14.3
Revision: 12bbae4
Directory: /home/git/gitlab
DB Adapter: mysql2
URL: http://git.cass.com:8050
HTTP Clone URL: http://git.cass.com:8050/some-group/some-project.git
SSH Clone URL: git@git.cass.com:some-group/some-project.git
Using LDAP: no
Using Omniauth: no
GitLab Shell
Version: 2.7.2
Repositories: /home/git/repositories/
Hooks: /home/git/gitlab-shell/hooks/
Git: /usr/local/bin/git
bundle exec rake sidekiq:start RAILS_ENV=production
bundle exec rake gitlab:check RAILS_ENV=production
sudo chmod -R ug+rwX,o-rwx /home/git/repositories/
sudo chmod -R ug-s /home/git/repositories/
find /home/git/repositories/ -type d -print0 | sudo xargs -0 chmod g+s
sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites

创建启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
sudo cp lib/support/init.d/gitlab.default.example /etc/default/gitlab
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
sudo chmod +x /etc/init.d/gitlab
sudo chkconfig --add gitlab
sudo chkconfig gitlab on
##########################
vim /etc/default/gitlab
app_root="/data/$app_user/gitlab"
#无需下载
##sudo wget https://raw.github.com/gitlabhq/gitlab-recipes/master/init/sysvinit/centos/gitlab-unicorn -P /etc/init.d/
##sudo mv /etc/init.d/gitlab-unicorn /etc/init.d/gitlab
##########################

复制配置文件[这里使用nginx做为WEB服务]

1
2
3
4
5
6
7
cd ~/gitlab
sudo mkdir /usr/local/nginx/conf/conf.d/ -p
sudo cp lib/support/nginx/gitlab /usr/local/nginx/conf/conf.d/
cd /usr/local/nginx/conf/conf.d/
sudo chown -R git:git .
cp gitlab gitlab.conf
vim /usr/local/nginx/conf/nginx.conf

nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
user git;
worker_processes 4;
events {
worker_connections 1024;
}
http {
includemime.types;
includeconf.d/*.conf;
......
sudo mkdir /var/log/nginx

gitlab虚拟主机的配置

1
2
3
4
5
6
7
8
9
vim /usr/local/nginx/conf/conf.d/gitlab.conf
server {
listen 0.0.0.0:8050 default_server;
#listen [::]:80 default_server;
server_name git.cass.com;
location ~ ^/(assets)/ {
root /home/git/gitlab/public;
#gzip_static on;

拉取gitLab静态文件

1
2
3
4
su - git && cd ~/gitlab
bundle exec rake assets:precompile RAILS_ENV=production
sudo service gitlab restart
sudo /usr/local/nginx/sbin/nginx

访问测试

logo

初始管理员帐号和密码为:

root
5iveL!fe

登陆成功后如下:

logo

修改邮件发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cd ~/gitlab/
vim config/environments/production.rb
config.action_mailer.delivery_method = :smtp
cp config/initializers/smtp_settings.rb.sample config/initializers/smtp_settings.rb
vim config/initializers/smtp_settings.rb
if Rails.env.production?
Gitlab::Application.config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: "smtp.163.com",
port: 25,
user_name: "xxx@cass.com",
password: "password",
domain: "smtp.163.com",
authentication: :plain,
enable_starttls_auto: true,
#openssl_verify_mode: 'peer' # See ActionMailer documentation for other possible options
}
end

8.x版本的邮件配置和7.x有所不同

1
2
3
4
5
6
7
8
9
10
11
12
cd ~/gitlab/
vim config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.163.com",
:port => "25",
:domain => "smtp.163.com",
:authentication => :plain,
:user_name => "xxx@163.com",
:password => "xxx",
:enable_starttls_auto => true
}

配置好你的邮箱和密码

编辑config/gitlab.yml

1
vi config/gitlab.yml

对应修改一下配置

1
2
3
email_from: xxx@cass.com
email_display_name: GitLab
email_reply_to:xxx@cass.com