由于该公司是使用的gitlab做版本控制与代码管理。所以领导让我这个实习生要搭建一遍gitlab。其实这个事情拖了好久只因为前期在做关于saltstack的实验。没有心思去搞git。昨晚将salt拿下。今天就静下心去搞。遇妖降妖,遇魔除魔!


建议:最好用纯净的系统去部署gitlab


一、环境介绍

         (1) centos 6.5

         (2)Ruby (版本: 2.0.0p353)

         (3)创建项目运行用户(创建git账号,方便权限管理)

         (4)GitLab Shell(版本:1.8.0)

         (5)数据库(可以支持mysql和PostgreSQL,这里使用mysql,版本:5.1.17)

         (6)GitLab(版本:6.3.1)

          (7)Web服务器(可支持nginx和apache,这里使用nginx,版本:1.0.15)


二、安装准备

    

   (1)增加EPEL安装源

EPEL,即Extra Packages for Enterprise Linux,这个软件仓库里有很多非常常用的软件,而且是专门针对RHEL设计的,对RHEL标准yum源是一个很好的补充,完全免费使用,由 Fedora项目维护,所以如果你使用的是RHEL,或者CentOS,Scientific等RHEL系的linux,可以非常放心的使用EPEL的 yum源。

下载并安装GPG key

$ sudo wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 https://www.fedoraproject.org/static/0608B895.txt
$ sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

检验下是否安装成功

$ sudo rpm -qa gpg*

安装epel-release-6-8.noarch包

$ sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

提示:不要在意x86_64,在i686的机器上一样能使用。

(2)增加PUIAS安装源

PUIAS Linux是面向桌面和服务器的完整的操作系统,它靠编译Red Hat Enterprise Linux的源代码包来创建。除了这些上游的软件包外,该项目还提供一些其他的软件仓库:“Addons”包含了通常的Red Hat发行中未收入的额外软件包,“Computational”提供专门针对科学计算的软件,“Unsupported”则收入各种各样的测试性软件 包。该发行由美国普林斯顿 大学的高等研究所维护。

创建/etc/yum.repos.d/PUIAS_6_computational.repo,并添加如下内容:

[PUIAS_6_computational]
name=PUIAS computational Base $releasever - $basearch
mirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist
#baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias

下载并安装GPG key

$ sudo wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-puias http://springdale.math.ias.edu/data/puias/6/x86_64/os/RPM-GPG-KEY-puias
$ sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-puias

检验下是否安装成功

$ sudo rpm -qa gpg*

Tips:安装完EPEL和PUIAS两个源后,可以检测下:

$ sudo yum repolist

 (3)依赖关系

         su -

       yum  -y  groupinstall‘Development Tools’

 yum -y install vim-enhanced readline readline-devel ncurses-devel gdbm-devel glibc-devel tcl-devel openssl-devel curl-devel expat-devel db4-devel byacc sqlite-devel gcc-c++ libyaml libyaml-devel libffi libffi-devel libxml2 libxml2-devel libxslt libxslt-devel libicu libicu-devel system-config-firewall-tui python-devel redis sudo wget crontabs logwatch logrotate perl-Time-HiRes git

这些安装包都需要装否则在后续的安装过程中会不顺利!!!


三、配置redis

      

配置redis使其在开机时启动:

$ sudo chkconfig redis on
$ sudo service redis start


四、安装ruby

     

     (1)、下载安装

     

$ su -
$ mkdir /tmp/ruby && cd /tmp/ruby
$ curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p353.tar.gz | tar xz
$ cd ruby-2.0.0-p353
$ ./configure --prefix=/usr/local/
$ make && make install

 

   (2)安装完成后,重新登录终端确保$PATH生效,检测ruby的安装成功与否:

 $ which ruby
 /usr/local/bin/ruby
 $ ruby -v
 ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
 
 
$ sudo gem install bundler --no-ri --no-rdoc


此时可能会出现错误:会出来一系列类似的。。


RROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)    Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org/quick/Marshal.4.8/fir-cli-1.1.5.gemspec.rz)


只要在该命令后出现任何错误,解决办法就是:

sudo gem sources -r https://rubygems.org/

gem sources -a https://ruby.taobao.org/


五、系统用户


$ su -
$ adduser --system --shell /bin/bash --comment 'GitLab' --create-home --home-dir /home/git/ git

因为git用户不需要登录,所以这里不需要设置git的密码。

转发所有邮件

笔者注:因为上面没有配置发送邮件,这里也省略。


六、安装git-shell


GitLab shell是专门为GitLab开发的提供ssh访问和版本管理的软件。

(1)先使用root登录,而后切换成git

$ su -
$ su - git

 (2)克隆gitlab shell

$ git clone https://github.com/gitlabhq/gitlab-shell.git
$ cd gitlab-shell

(3)切换成1.8.0版本,并编辑配置

$ git checkout v1.8.0
$ cp config.yml.example config.yml


(4)安装一些需要的目录和文件

$ ./bin/install


七、安装数据库


(1)安装mysql并设置开机启动:

$ su -
$ yum install -y mysql-server mysql-devel
$ chkconfig mysqld on
$ service mysqld start

(2)设置mysql root账号的密码:

$ /usr/bin/mysql_secure_installation

(3)创建新用户和数据库给gitlab使用

# 登录数据库
$ mysql -u root -p
# 输入root密码
# 为gitlab创建使用用户
CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'gitlab账号的密码';

# 创建gitlaba使用的数据库
CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

# 给予gitlab用户权限
GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

# 登出数据库
\q


八、安装gitlab


(1)将GitLab安装在git的家目录下:

$ su -
$ su - git

(2)克隆GitLab并切换分支到6-3-stable

# 克隆GitLab
$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab
# 进入gitlab目录
$ cd /home/git/gitlab
# 切换到6-3-stable分支
$ git checkout 6-3-stable


(3)修改配置文件

# 复制配置文件
$ cp config/gitlab.yml.example config/gitlab.yml

# 修改配置文件中的访问域名
(your_domain_name为项目的访问域名)

vim gitlab.yml

将里面的localhost换成你的域名。我没有设置域名直接写的IP地址

# 设定log和tmp目录所有者和权限
$ chown -R git log/
$ chown -R git tmp/
$ chmod -R u+rwX log/
$ chmod -R u+rwX tmp/

# 创建gitlab-satellites目录
$ mkdir /home/git/gitlab-satellites

# 创建tmp/pids/和tmp/sockets/目录,确保gitlab有相应的权限
$ mkdir tmp/pids/
$ mkdir tmp/sockets/
$ chmod -R u+rwX tmp/pids/
$ chmod -R u+rwX tmp/sockets/

# 创建public/uploads目录
$ mkdir public/uploads
$ chmod -R u+rwX public/uploads

# 复制unicorn配置
$ cp config/unicorn.rb.example config/unicorn.rb

# 编辑unicorn配置
(笔者这里采用默认配置)
$ vim config/unicorn.rb

# 配置git的用户和邮件
$ git config --global user.name "GitLab"
$ git config --global user.email "gitlab@your_domain_name"
$ git config --global core.autocrlf input

这边的配置比较复杂,细心些就行了。

(4)配置数据库访问文件

$ cp config/database.yml.mysql config/database.yml

编辑config/database.yml,设置其中连接数据库的账号密码,笔者的配置部分如下:

#
# PRODUCTION
#
production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: gitlabhq_production
  pool: 10
 
 
  # host: localhost
  # socket: /tmp/mysql.sock

修改其中username和password就可以了,其中密码就是上面数据库步骤中创建gitlab用户的密码。

确保该文件只有git账号有权限读取。

$ chmod o-rwx config/database.yml
九、安装gem
$ su -
$ gem install charlock_holmes --version '0.6.9.4'
$ exit
(1)安装mysql包
$ cd /home/git/gitlab/
$ bundle install --deployment --without development test postgres 
在此处应该会出现错误:
Fetching source index from http://rubygems.org/
Fetching https://github.com/gitlabhq/grit.git
Could not find modernizr-2.6.2 in any of the sources
#出现错误:Could not find modernizr-2.6.2 in any of the sources
解决办法:vim Gemfile
source "https://rubygems.org"改为source "http://ruby.taobao.org"
 gem "modernizr", "2.6.2"
更改改为:
 gem "modernizr-rails",  "2.7.1"
 
 vim Gemfile.lock
 
      modernizr (2.6.2)
更改为:
     modernizr-rails (2.7.1)
 modernizr (= 2.6.2)
更改改为:
  modernizr-rails (= 2.7.1)
  接下来就可以再次执行了!!
  bundle install --deployment --without development test postgres

(2)

初始化数据和激活高级功能

$ cd /home/git/gitlab
$ bundle exec rake gitlab:setup RAILS_ENV=production

这步完成后,会生一个默认的管理员账号:

admin@local.host
5iveL!fe

(3)置 GitLab 启动服务

cd /home/git/gitlab/

sudo update-rc.d gitlab defaults 21

设置 GitLab 使用 Logrotate 备份 Log

cd /home/git/gitlab/
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

(4)检查GitLab及其环境的配置是否正确:

cd /home/git/gitlab/
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production

[git@test gitlab]$ bundle exec rake gitlab:env:info RAILS_ENV=production


System information

System:CentOS release 6.5 (Final)

Current User:git

Using RVM:no

Ruby Version:2.0.0p353

Gem Version:2.0.14

Bundler Version:1.11.2

Rake Version:10.1.0


GitLab information

Version:6.3.1

Revision:ae862b5

Directory:/home/git/gitlab

DB Adapter:mysql2

URL:http://10.10.20.140

HTTP Clone URL:http://10.10.20.140/some-project.git

SSH Clone URL:git@10.10.20.140:some-project.git

Using LDAP:no

Using Omniauth:no


GitLab Shell

Version:1.8.0

Repositories:/home/git/repositories/

Hooks:/home/git/gitlab-shell/hooks/

Git:/usr/bin/git

(5)启动 GitLab 服务

/etc/init.d/gitlab restart
Shutting down both Unicorn and Sidekiq.
GitLab is not running.
Starting both the GitLab Unicorn and Sidekiq..
The GitLab Unicorn web server with pid 17771 is running.
The GitLab Sidekiq job dispatcher with pid 17778 is running.
GitLab and all its components are up and running


最后编译一下。

 bundle exec rake assets:precompile RAILS_ENV=production

此时证明你的gitlab已经正常运行了。。。但是要是能够访问还需要配置个web服务。


九、配置nginx

(1)安装nginx

yum install nginx

(2)配置

配置 Nginx


 chkconfig nginx on
$ mkdir /etc/nginx/sites-available
$ mkdir /etc/nginx/sites-enabled


cd /home/git/gitlab
sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitl
vim /etc/nginx/sites-available/gitlab

重点需要修该的是:server_name:

                  listen:


编辑/etc/nginx/nginx.conf,将 include /etc/nginx/conf.d/*.conf; 替换成 include /etc/nginx/sites-enabled/*;,就是修改额外加载的配置文件目录。

重新启动nginx: service nginx restart

http://server_name:port

gitlab 搭建完毕!!!!

参考文档:http://www.open-open.com/lib/view/open1399684894447.html

          http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/