[关闭]
@cdmonkey 2015-06-10T04:50:40.000000Z 字数 11801 阅读 1453

Gitlab

开源工具


一、简介

GitLab,是一个使用“Ruby on Rails”开发的开源应用程序,与Github类似,能够浏览源代码,管理缺陷和注释,非常适合在团队内部使用。

在开始之前请先查看官方的刚需文档:
https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/requirements.md
该文档说明了系统,软件和硬件等各方面的需求。详细的了解这些,可以避免碰到很多怪异的问题。

二、安装

1. 安装操作系统

  1. [root@Gitlab ~]# cat /etc/issue
  2. CentOS release 6.5 (Final)
  3. ------------------
  4. #升级操作系统(这一步并不是必须的):
  5. [root@Gitlab ~]# yum -y update

2. 安装软件源

Install EPEL source

  1. rpm -ivh http://mirrors.aliyun.com/epel/6/x86_64/epel-release-6-8.noarch.rpm

这个软件仓库里有很多非常常用的软件,而且是专门针对红帽企业版设计的,对标准的yum源是一个很好的补充,完全免费使用,由“Fedora”项目维护。

Add PUIAS source

  1. [root@Gitlab ~]# vim /etc/yum.repos.d/PUIAS_6_computational.repo
  2. #创建该文件,并添加如下的内容:
  3. [PUIAS_6_computational]
  4. name=PUIAS computational Base $releasever - $basearch
  5. mirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist
  6. #baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch
  7. gpgcheck=1
  8. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias

Download and install GPG key:

  1. [root@Gitlab ~]# 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
  2. [root@Gitlab ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-puias

检验下是否安装成功:

  1. [root@Gitlab ~]# rpm -qa gpg*
  2. gpg-pubkey-c105b9de-4e0fd3a3
  3. gpgme-1.1.8-3.el6.x86_64
  4. gpg-pubkey-41a40948-4ce19266

安装完上面的两个源后,可以检测下:

  1. [root@Gitlab ~]# yum repolist
  2. Loaded plugins: fastestmirror, security
  3. Loading mirror speeds from cached hostfile
  4. epel/metalink | 4.0 kB 00:00
  5. * PUIAS_6_computational: puias.math.ias.edu
  6. * base: mirrors.aliyun.com
  7. * epel: mirrors.neusoft.edu.cn
  8. * extras: mirrors.aliyun.com
  9. * updates: mirrors.aliyun.com
  10. PUIAS_6_computational | 3.3 kB 00:00
  11. PUIAS_6_computational/primary_db | 1.9 MB 00:08
  12. epel | 4.4 kB 00:00
  13. epel/primary_db | 6.6 MB 01:25
  14. repo id repo name status
  15. PUIAS_6_computational PUIAS computational Base 6 - x86_64 2,868
  16. base CentOS-6 - Base - mirrors.aliyun.com 6,518
  17. epel Extra Packages for Enterprise Linux 6 - x86_64 11,592
  18. extras CentOS-6 - Extras - mirrors.aliyun.com 38
  19. updates CentOS-6 - Updates - mirrors.aliyun.com 1,155
  20. repolist: 22,171

3. 安装依赖包和工具

  1. yum -y groupinstall "Development Tools"
  2. yum -y install vim-enhanced readline readline-devel ncurses-devel gdbm-devel glibc-devel \
  3. tcl-devel openssl-devel curl-devel expat-devel db4-devel byacc sqlite-devel gcc-c++ \
  4. libyaml libyaml-devel libffi libffi-devel libxml2 libxml2-devel libxslt libxslt-devel libicu \
  5. libicu-devel system-config-firewall-tui python-devel redis sudo wget crontabs logwatch \
  6. logrotate perl-Time-HiRes git
  7. yum install -y perl-devel perl-ExtUtils-Embed

如果部分包不能安装,可能需要增加“RHEL6”的安装源。

  1. yum-config-manager --enable rhel-6-server-optional-rpms

检查Python版本:

  1. #应确保其版本为2.5+:
  2. [root@Gitlab ~]# python --version
  3. Python 2.6.6

除此之外还有一项重要的工作就是升级git版本,因为gitlab要求其版本在1.7.2。如果想要升级到最新版本,那么就需要手动编译安装。由于当前的系统已经安装了git,所以直接从“github”上克隆一个最新版。

  1. #网上说如果通过yum安装,只能装到1.7.1,但是我们当前的版本确是如下所示的版本,应该是我们增加了软件源的缘故。
  2. [root@Gitlab ~]# git --version
  3. git version 1.8.3.1
  4. -----------------------
  5. #升级到最新版本:
  6. [root@Gitlab ~]# cd tools/
  7. [root@Gitlab tools]# git clone https://github.com/git/git.git
  8. #升级前最好备份当前的版本:
  9. [root@Gitlab tools]# mv /usr/bin/git{,.bak}
  10. #然后开始进行编译安装:
  11. [root@Gitlab tools]# cd git/
  12. [root@Gitlab git]# make configure
  13. GIT_VERSION = 2.4.0.GIT
  14. GEN configure
  15. [root@Gitlab git]# ./configure --prefix=/usr/bin
  16. [root@Gitlab git]# make && make install
  17. #创建软链接(这里是个小坑):
  18. [root@Gitlab git]# ln -s /usr/bin/bin/git /usr/bin/git
  19. #最后进行验证:
  20. [root@Gitlab bin]# git --version
  21. git version 2.4.0.GIT

若是条件允许,从源码编译安装有很多好处,至少可以安装最新的版本。它的每个版本都在不断尝试改进用户体验,所以能通过源代码自己编译安装最新版本就再好不过了。

4. Configure Redis

设置为开机自启动并启动服务。

  1. [root@Gitlab ~]# chkconfig redis on
  2. [root@Gitlab ~]# service redis start
  3. Starting redis-server: [ OK ]

5. 邮件服务器

暂略

6. Install Ruby

  1. [root@Gitlab ~]# cd tools/
  2. [root@Gitlab tools]# wget ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p645.tar.gz
  3. [root@Gitlab tools]# tar zxvf ruby-2.0.0-p645.tar.gz
  4. [root@Gitlab tools]# cd ruby-2.0.0-p645
  5. ./configure --prefix=/usr/local/
  6. make && make install

安装完成后,检测安装成功与否:

  1. [root@Gitlab ~]# ruby -v
  2. ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-linux]

注意:这里最好使用2.0.0版本,使用更高版本的话后面的操作会出现报错,很难解决。

Install bundle

这是一个安装ruby的包系统,用来管理gem。安装之前,首先需要添加“rubygems”的国内镜像,否则以国内的网络环境几乎无法连接到。详见:http://ruby.taobao.org

  1. [root@Gitlab ~]# gem sources --remove https://rubygems.org/
  2. https://rubygems.org/ removed from sources
  3. [root@Gitlab ~]# gem sources -a https://ruby.taobao.org/
  4. https://ruby.taobao.org/ added to sources
  5. [root@Gitlab ~]# gem sources -l
  6. *** CURRENT SOURCES ***
  7. https://ruby.taobao.org/ #请确保这里只有一个源。
  1. [root@Gitlab ~]# gem install bundler --no-ri --no-rdoc
  2. Fetching: bundler-1.10.3.gem (100%)
  3. Successfully installed bundler-1.10.3
  4. 1 gem installed

7. 创建用户

因为该用户不需要进行登录,所以这里不需要设置密码。另外,为了下面的安装操作方便一些,推荐为该用户进行sudo授权。

  1. [root@Gitlab ~]# useradd -c 'Gitlab' git
  2. [root@Gitlab ~]# visudo
  3. git ALL=(ALL) NOPASSWD: ALL

8. GitLab shell

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

  1. #首先要切换用户:
  2. [root@Gitlab ~]# su - git
  3. #克隆:
  4. [git@Gitlab ~]$ git clone https://github.com/gitlabhq/gitlab-shell.git
  5. [git@Gitlab ~]$ cd gitlab-shell/
  6. #切换成1.8.0版本,并编辑配置文件:
  7. [git@Gitlab gitlab-shell]$ git checkout v1.9.1
  8. [git@Gitlab gitlab-shell]$ cp config.yml.example config.yml
  9. #这里最重要的是将gitlab_url修改成使用的域名。如果使用的域名是测试域名,不要忘记在系统的hosts做域名映射。
  10. [git@Gitlab gitlab-shell]$ vim config.yml
  11. gitlab_url: "http://gitlab.cdmonkey.com/"
  1. #最后,创建一些需要的目录和文件(具体见下面的输出内容):
  2. [git@Gitlab gitlab-shell]$ ./bin/install
  3. mkdir -p /home/git/repositories: OK
  4. mkdir -p /home/git/.ssh: OK
  5. chmod 700 /home/git/.ssh: OK
  6. touch /home/git/.ssh/authorized_keys: OK
  7. chmod 600 /home/git/.ssh/authorized_keys: OK
  8. chmod -R ug+rwX,o-rwx /home/git/repositories: OK
  9. find /home/git/repositories -type d -exec chmod g+s {} ;: OK

注意:如果通过使用“https”访问,则需进行如下改动:

  1. gitlab_url: "https://gitlab.cdmonkey.com/"
  2. self_signed_cert: true

9. 安装数据库

  1. [root@Gitlab ~]# yum install -y mysql-server mysql-devel
  2. [root@Gitlab ~]# chkconfig mysqld on
  3. [root@Gitlab ~]# /etc/init.d/mysqld start
  4. #设置root用户的密码:
  5. [root@Gitlab ~]# /usr/bin/mysql_secure_installation

创建新用户和数据库给gitlab使用:

  1. [root@Gitlab ~]# mysql -uroot -p
  2. mysql> GRANT USAGE ON *.* TO 'gitlab'@'localhost' IDENTIFIED BY '123';
  3. mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
  4. mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

10. Install Gitlab

  1. [root@Gitlab ~]# su - git
  2. [git@Gitlab ~]$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab
  3. #切换分支(这步还不到是干什么用)。
  4. [git@Gitlab ~]$ cd gitlab
  5. [git@Gitlab gitlab]$ git checkout 6-7-stable
  6. Branch 6-7-stable set up to track remote branch 6-7-stable from origin.
  7. Switched to a new branch '6-7-stable'

配置项目

  1. [git@Gitlab ~]$ cd /home/git/
  2. [git@Gitlab ~]$ cp gitlab/config/gitlab.yml.example gitlab/config/gitlab.yml
  3. [git@Gitlab ~]$ vim gitlab/config/gitlab.yml
  4. #修改配置文件中的访问域名:
  5. host: gitlab.cdmonkey.com
  6. port: 80
  7. https: true #若需要使用https来访问,这里也要进行修改。
  8. -----------------------
  9. #设定一些目录所有者和权限:
  10. [git@Gitlab ~]$ cd /home/git/gitlab
  11. chown -R git log/
  12. chown -R git tmp/
  13. chmod -R u+rwX log/
  14. chmod -R u+rwX tmp/
  15. #创建目录:
  16. [git@Gitlab gitlab]$ mkdir /home/git/gitlab-satellites
  17. #
  18. [git@Gitlab gitlab]$ mkdir tmp/pids/
  19. [git@Gitlab gitlab]$ mkdir tmp/sockets/
  20. [git@Gitlab gitlab]$ chmod -R u+rwX tmp/pids/
  21. [git@Gitlab gitlab]$ chmod -R u+rwX tmp/sockets/
  22. #
  23. [git@Gitlab gitlab]$ mkdir public/uploads
  24. [git@Gitlab gitlab]$ chmod -R u+rwX public/uploads
  25. #创建并编辑“unicorn”配置文件,当前实验采用默认配置。
  26. [git@Gitlab gitlab]$ cp config/unicorn.rb.example config/unicorn.rb
  27. [git@Gitlab gitlab]$ vim config/unicorn.rb
  28. listen "127.0.0.1:8080", :tcp_nopush => true
  29. ------------------
  30. #配置git的用户和邮件:
  31. [git@Gitlab gitlab]$ git config --global user.name "GitLab"
  32. [git@Gitlab gitlab]$ git config --global user.email "gitlab@localhost"
  33. [git@Gitlab gitlab]$ git config --global core.autocrlf input

以上的配置比较复杂和琐碎,细心些就行了。一定要确保gitlab.ymlunicorn.rb两个文件设置正确。

配置数据库访问文件

  1. [git@Gitlab gitlab]$ cp config/database.yml.mysql config/database.yml
  2. #在配置文件中设置连接数据库的用户名和密码(只需修改“production”部分即可):
  3. [git@Gitlab gitlab]$ vim config/database.yml
  4. production:
  5. adapter: mysql2
  6. encoding: utf8
  7. reconnect: false
  8. database: gitlabhq_production
  9. pool: 10
  10. reaping_frequency: 10
  11. username: gitlab
  12. password: "123"
  13. -----------------------
  14. #确保该文件只有git账号有权限读取:
  15. [git@Gitlab gitlab]$ chmod o-rwx config/database.yml

注意这里还有一个配置文件需要编辑:

  1. [git@Gitlab gitlab]$ cp config/resque.yml.example config/resque.yml
  2. [git@Gitlab gitlab]$ vim config/resque.yml
  3. #设置“Redis”的监听地址:
  4. production: redis://localhost:6379

Install Gems

  1. [git@Gitlab ~]$ cd gitlab
  2. #更换ruby源,加快包的安装速度:
  3. [git@Gitlab gitlab]$ vim Gemfile
  4. source 'https://ruby.taobao.org/'
  1. #切换回根用户进行安装:
  2. [root@Gitlab ~]# gem install charlock_holmes --version '0.6.9.4'

Install MySQL Package

  1. [root@Gitlab ~]# su - git
  2. [git@Gitlab ~]$ cd gitlab
  3. [git@Gitlab gitlab]$ bundle install --deployment --without development test postgres puma aws
  4. Fetching source index from https://ruby.taobao.org/
  5. ...
  6. Post-install message from httparty:
  7. When you HTTParty, you must party hard!

初始化数据库

  1. [git@Gitlab gitlab]$ bundle exec rake gitlab:setup RAILS_ENV=production
  2. ...
  3. Administrator account created:
  4. login.........admin@local.host
  5. password......5iveL!fe
  6. #操作完成后,就会生成一个默认的管理员账号:
  7. admin@local.host
  8. 5iveL!fe

安装启动脚本

  1. #切换到根用户执行:
  2. [root@Gitlab ~]# wget -O /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/master/init/sysvinit/centos/gitlab-unicorn
  3. [root@Gitlab ~]# chmod +x /etc/init.d/gitlab
  4. [root@Gitlab ~]# chkconfig --add gitlab
  5. #设置为开机自启动:
  6. [root@Gitlab ~]# chkconfig gitlab on

检测应用环境

  1. [root@Gitlab ~]# su - git
  2. [git@Gitlab ~]$ cd gitlab/
  3. [git@Gitlab gitlab]$ bundle exec rake gitlab:env:info RAILS_ENV=production
  4. System information
  5. System: CentOS 6.6
  6. Current User: git
  7. Using RVM: no
  8. Ruby Version: 2.0.0p645
  9. Gem Version: 2.0.14
  10. Bundler Version:1.10.3
  11. Rake Version: 10.1.1
  12. GitLab information
  13. Version: 6.7.5
  14. Revision: 00aa5c1
  15. Directory: /home/git/gitlab
  16. DB Adapter: mysql2
  17. URL: http://gitlab.cdmonkey.com
  18. HTTP Clone URL: http://gitlab.cdmonkey.com/some-project.git
  19. SSH Clone URL: git@gitlab.cdmonkey.com:some-project.git
  20. Using LDAP: no
  21. Using Omniauth: no
  22. GitLab Shell
  23. Version: 1.9.1
  24. Repositories: /home/git/repositories/
  25. Hooks: /home/git/gitlab-shell/hooks/
  26. Git: /usr/bin/git

Set Logarate

  1. #切换到根用户执行:
  2. [root@Gitlab ~]# cd /home/git/gitlab
  3. [root@Gitlab gitlab]# cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

Start Gitlab

  1. #启动服务这里又是一个坑,不执行下面的操作启动就会报错。除此之外,也可以修改服务启动文件,这里就省略了。
  2. [git@Gitlab ~]$ cd gitlab
  3. [git@Gitlab gitlab]$ cp -r script/* bin
  1. #启动服务要以根用户的身份来执行:
  2. [git@Gitlab gitlab]$ sudo /etc/init.d/gitlab start
  3. Starting unicorn: [ OK ]
  4. Starting sidekiq: [ OK ]

查看应用更加详细的检测信息:

  1. [git@Gitlab gitlab]$ bundle exec rake gitlab:check RAILS_ENV=production

报错信息及处理:

  1. ...
  2. from /home/git/gitlab-shell/bin/check:11:in `<main>'`
  3. gitlab-shell self-check failed
  4. Try fixing it:
  5. Make sure GitLab is running;
  6. Check the gitlab-shell configuration file:
  7. sudo -u git -H editor /home/git/gitlab-shell/config.yml
  8. Please fix the error above and rerun the checks.

The Gitlab configuration depends on three files:

文件 说明
/home/git/gitlab/config/unicorn.rb 为ruby提供的服务端口和地址。
/home/git/gitlab/config/gitlab.yml 为gitlab服务的端口和地址。
/home/git/gitlab-shell/config.yml gitlab-shell要调用的API接口。

最后的设定内容:

  1. [git@Gitlab gitlab]$ vim config/gitlab.yml
  2. host: gitlab.cdmonkey.com
  3. port: 80
  4. https: true
  5. [git@Gitlab gitlab]$ vim config/unicorn.rb
  6. listen "127.0.0.1:8080", :tcp_nopush => true
  7. [git@Gitlab gitlab-shell]$ vim config.yml
  8. gitlab_url: "http://gitlab.cdmonkey.com:8080"
  1. #可以不用处理这个下面这个问题:
  2. Init script up-to-date? ... no
  3. Try fixing it:
  4. Redownload the init script
  5. For more information see:
  6. doc/install/installation.md in section "Install Init Script"
  7. Please fix the error above and rerun the checks.

11. Install Web Server

Install Nginx:

  1. [root@Gitlab ~]# yum -y install nginx
  2. [root@Gitlab ~]# chkconfig nginx on
  3. [root@Gitlab ~]# mkdir /etc/nginx/sites-available
  4. [root@Gitlab ~]# mkdir /etc/nginx/sites-enabled
  5. [root@Gitlab ~]# cd /home/git/gitlab
  6. [root@Gitlab gitlab]# cp lib/support/nginx/gitlab /etc/nginx/sites-available/
  7. [root@Gitlab gitlab]# ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab

修改配置文件:

  1. [root@Gitlab ~]# vim /etc/nginx/nginx.conf
  2. #把原来加载配置文件的这行注释掉,替换为以下这行:
  3. include /etc/nginx/sites-enabled/*;
  1. [root@Gitlab ~]# vim /etc/nginx/sites-available/gitlab
  2. #设置访问域名:
  3. server_name gitlab.cdmonkey.com;
  1. [root@Gitlab ~]# usermod -a -G git nginx
  2. [root@Gitlab ~]# chmod g+rx /home/git/

添加ssl证书或者自己生成一个:

  1. [root@Gitlab ~]# cd /etc/nginx/
  2. [root@Gitlab nginx]# openssl req -new -x509 -nodes -days 3560 -out gitlab.crt -keyout gitlab.key

启动服务:

  1. [root@Gitlab ~]# /etc/init.d/nginx start

这里有一步是拉取静态资源文件,需要执行,否侧会报“502”错误:

  1. [root@Gitlab ~]# sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production

至此就算安装完成了。默认的账号密码:
admin@local.host
5iveL!fe

此处输入图片的描述

12. 检查端口

  1. [root@Gitlab ~]# netstat -lntup
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 39892/mysqld
  5. tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 28515/redis-server
  6. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15651/nginx
  7. tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 15132/unicorn_rails
  8. ...
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注