Gitlab安装和配置
系统环境:ubuntu 14.04.1_64bit
参考网址:
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md

一、    设置apt源及DNS:
//配置apt源
# vim /etc/apt/sources.list
deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse

deb http://mirrors.sohu.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ trusty-backports main restricted universe multiverse

deb http://mirrors.oschina.net/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.oschina.net/ubuntu/ trusty-backports main restricted universe multiverse
deb http://mirrors.oschina.net/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.oschina.net/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.oschina.net/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.oschina.net/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.oschina.net/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.oschina.net/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.oschina.net/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.oschina.net/ubuntu/ trusty-updates main restricted universe multiverse
//修改dns
#sudo vim /etc/resolv.conf
nameserver 202.106.0.20
nameserver 8.8.8.8
nameserver 202.96.134.133
nameserver 202.96.128.68
#sudo vim /etc/hosts
115.28.122.210  mirrors.aliyun.com
112.124.140.210 mirrors.aliyun.com
#sudo apt-get update -y
#sudo apt-get upgrade -y

二、安装依赖包:
#sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake libkrb5-dev

三、    安装git:
//若原来的git版本太低,可以卸载重新安装:
#sudo apt-get remove git-core
//安装依赖包:
#sudo apt-get install -y libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev build-essential
//下载安装git:
#sudo mkdir /opt/gitlab
#cd /opt/gitlab
#wget https://www.kernel.org/pub/software/scm/git/git-2.1.2.tar.gz
#tar –zxvf git-2.1.2.tar.gz
#cd git-2.1.2
#sudo ./configure
#sudo make prefix=/usr/local all
#sudo make prefix=/usr/local install #安装git到/usr/local/下
//给gitlab创建git用户:
# sudo adduser --disabled-login --gecos 'GitLab' git

//安装postfix邮件服务器
# sudo apt-get install -y postfix
 

四、    安装ruby:
//移除旧版本,安装新版本:
# sudo apt-get remove ruby1.8
#cd /opt
#wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz
#tar –zxvf ruby-2.1.5.tar.gz
#cd ruby-2.1.5
#sudo ./configure --disable-install-rdoc
#sudo make
# sudo make install
//安装Bundler Gem:
# sudo gem install bundler --no-ri --no-rdoc

五、安装mysql数据库
#sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
# mysql –version
//安装设置mysql的root密码:
# sudo mysql_secure_installation
# mysql -u root –p
//创建一个gitlab数据库用户:
mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY 'git';
//设定存储引擎为innodb,若报错,检查/etc/mysql/*.cnf、/etc/mysql/conf.d/*中"innodb = off"的设置:
mysql> SET storage_engine=INNODB;
//创建gitlab生产环境使用的数据库:
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES ON `gitlabhq_production`.* TO 'git'@'localhost';
mysql> \q;
//使用新用户登录新数据库:
# sudo -u git -H mysql -u git -p -D gitlabhq_production
//登录成功,证明设置生效,退出:
mysql> \q

六、    安装redis
# sudo apt-get install redis-server
# sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig
//redis端口默认6379,官网设置为0,我设置为6379
# sed 's/^port .*/port 6379/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf
#echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf
# echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf
# mkdir /var/run/redis
# chown redis:redis /var/run/redis
# chmod 755 /var/run/redis
if [ -d /etc/tmpfiles.d ]; then   
echo 'd  /var/run/redis  0755  redis  redis  10d  -' | sudo tee -a /etc/tmpfiles.d/redis.conf
fi
# sudo service redis-server restart
# sudo usermod -aG redis git
 

七、    安装gitlab
1、克隆仓库:
安装gitlab到/home/git:
#cd /home/git
//克隆7.6版本的gitlab仓库到服务器上:
# sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-6-stable gitlab
#cd /home/git/gitlab
#sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
2、修改配置:
//修改gitlab配置文件gitlab.yml:
#sudo -u git -H vim config/gitlab.yml
(1) 在gitlab一栏下:
host改为gitlab服务器ip,port改为自定义端口
 
(2)找到email_from字段,改为公司邮箱
 
(3)找到default_projects_features字段,issues、merge_requests、wiki为true,snippets为false,visibility_level为private
 
(4)找到issues_tracker一栏,添加jira选项,如下图:
    jira:
       title: "Atlassian Jira"
       project_url: "http://10.0.1.2:8080/issues/?jql=project=:issues_tracker_id"
       issues_url: "http://10.0.1.2:8080/browse/:id"
       new_issue_url: "http://10.0.1.2:8080/secure/CreateIssue.jspa"
 
//gravatar一栏默认:
 
//ldap一栏默认:
 
//amniauth一栏默认:
 
(5) GitLab Satellites一栏,修改为安装目录路径:
  satellites:
    path: /home/git/gitlab-satellites/
  backup:
    path: "tmp/backups"
 
(6) GitLab Shell一栏,修改为安装目录路径:
  gitlab_shell:
    path: /home/git/gitlab-shell/
    repos_path: /home/git/repositories/
    hooks_path: /home/git/gitlab-shell/hooks/
    upload_pack: true
    receive_pack: true
 
(7)git一栏设置为git环境变量,max_size根据业务修改:
#which git
 
git:
     bin_path: /usr/bin/git
     max_size: 20971520
     timeout: 10
 
//其他默认不变:
 

3、设置gitab对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/

4、创建gitlab-satellites目录:
#sudo -u git -H mkdir /home/git/gitlab-satellites
#sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites
//修改对tmp目录、public目录权限:
#sudo chmod -R u+rwX tmp/pids/
#sudo chmod -R u+rwX tmp/sockets/
#sudo chmod -R u+rwX  public/uploads

5、修改unicorn.rb文件:
#sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
//查看核心数,根据内核数来设定worker_processes值:
#nproc
//将默认路径改为安装目录:
#sudo -u git –H vim config/unicorn.rb
worker_processes 2
working_directory "/home/git/gitlab"
listen "/home/git/gitlab/tmp/sockets/gitlab.socket", :backlog => 64
listen "127.0.0.1:8080", :tcp_nopush => true
timeout 30
pid "/home/git/gitlab/tmp/pids/unicorn.pid"
stderr_path "/home/git/gitlab/log/unicorn.stderr.log"
stdout_path "/home/git/gitlab/log/unicorn.stdout.log"
 
//其他默认不变:
 

6、设置git的全局配置:
# 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 "git"
//配置全局邮箱为gitlab.yml中设置的邮箱:
# sudo -u git -H git config --global user.email 公司邮箱名
# sudo -u git -H git config --global core.autocrlf input

7、修改redis连接设置:
# sudo -u git -H cp config/resque.yml.example config/resque.yml
# sudo -u git -H vim config/resque.yml
//此处若不使用系统默认的resque.yml,需要自己修改redis socket路径。
备注:确保gitlab.yml 和unicorn.rb的修改匹配你的设置

8、修改Gitlab DB 设置:
#sudo -u git cp config/database.yml.mysql config/database.yml
//只修改生产环境中设置,更新用户和密码,密码使用双引号:
# sudo –u git –H vim config/database.yml
production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: gitlabhq_production
  pool: 10
  username: git
  password: "git"
 
//设置git用户对database.yml有读的权限:
# sudo -u git -H chmod o-rwx config/database.yml

9、安装Gems:
//先确保bundler 版本>=1.5.2(执行bundle -v)
#sudo -u git -H bundle install --deployment --without development test postgres aws

10、安装GitLab Shell:
//GitLab Shell是一个用于ssh访问权限认证和仓库管理的软件:
#sudo -u git -H bundle exec rake gitlab:shell:install[v2.4.0] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production

//修改GitLab Shell的配置文件config.yml,修改gitlab的用户为git、web登录地址gitlab_url、仓库放置路径字段repos_path、认证密钥路径字段auth_file、redis主机和端口:
#sudo -u git -H vim /home/git/gitlab-shell/config.yml
 
11、初始化数据库:
//初始化数据库,产生一个默认用户和密码:
#sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
 
//设置gitlab的web登录新账号和密码后,一定记住!
#sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=”12345678”
 
account: root, 12345678

12、安装gitlab初始启动脚本init:
#sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
#sudo cp lib/support/init.d/gitlab.default.example /etc/default/gitlab
备注:若安装gitlab不是默认目录,必须修改/etc/default/gitlab中相关路径
//设置gitlab自启动
#sudo update-rc.d gitlab defaults 21

13、设置日志轮询:
#sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

14、检查gitlab服务和环境变量配置是否正确:
#sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
 
15、编译assets:
#sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
//启动gitlab实例
#sudo service gitlab start
 
#ps auxww
 
 

八、    安装Nginx:
1、安装nginx:
#sudo apt-get install -y nginx
2、站点配置:
//复制模版:
#sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab #sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
//修改站点监听端口、ip、日志目录、socket路径、站点public目录:
#sudo vim /etc/nginx/sites-available/gitlab
upstream gitlab {
  server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}
server {
  listen *:8088 default_server;
  server_name 10.0.1.1;
  server_tokens off;
  root /home/git/gitlab/public;
  client_max_body_size 5m;
  access_log  /var/log/nginx/gitlab_access.log;
  error_log   /var/log/nginx/gitlab_error.log;
 
//其他默认不变:
 
3、测试配置:
#sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
//重启nginx
#sudo service nginx restart
到此,gitlab搭建完成!!!
//检测所有应用是否正常:
#sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
都是绿色ok,表示正常。
 
 
打开浏览器,输入:10.0.1.1:8088,输入账号、密码登录测试