@cdmonkey
2015-06-10T04:50:40.000000Z
字数 11801
阅读 1701
开源工具
GitLab,是一个使用“Ruby on Rails”开发的开源应用程序,与Github类似,能够浏览源代码,管理缺陷和注释,非常适合在团队内部使用。
在开始之前请先查看官方的刚需文档:
https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/requirements.md
该文档说明了系统,软件和硬件等各方面的需求。详细的了解这些,可以避免碰到很多怪异的问题。
略
[root@Gitlab ~]# cat /etc/issueCentOS release 6.5 (Final)------------------#升级操作系统(这一步并不是必须的):[root@Gitlab ~]# yum -y update
rpm -ivh http://mirrors.aliyun.com/epel/6/x86_64/epel-release-6-8.noarch.rpm
这个软件仓库里有很多非常常用的软件,而且是专门针对红帽企业版设计的,对标准的yum源是一个很好的补充,完全免费使用,由“Fedora”项目维护。
[root@Gitlab ~]# vim /etc/yum.repos.d/PUIAS_6_computational.repo#创建该文件,并添加如下的内容:[PUIAS_6_computational]name=PUIAS computational Base $releasever - $basearchmirrorlist=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearch/mirrorlist#baseurl=http://puias.math.ias.edu/data/puias/computational/$releasever/$basearchgpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puias
Download and install GPG key:
[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[root@Gitlab ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-puias
检验下是否安装成功:
[root@Gitlab ~]# rpm -qa gpg*gpg-pubkey-c105b9de-4e0fd3a3gpgme-1.1.8-3.el6.x86_64gpg-pubkey-41a40948-4ce19266
安装完上面的两个源后,可以检测下:
[root@Gitlab ~]# yum repolistLoaded plugins: fastestmirror, securityLoading mirror speeds from cached hostfileepel/metalink | 4.0 kB 00:00* PUIAS_6_computational: puias.math.ias.edu* base: mirrors.aliyun.com* epel: mirrors.neusoft.edu.cn* extras: mirrors.aliyun.com* updates: mirrors.aliyun.comPUIAS_6_computational | 3.3 kB 00:00PUIAS_6_computational/primary_db | 1.9 MB 00:08epel | 4.4 kB 00:00epel/primary_db | 6.6 MB 01:25repo id repo name statusPUIAS_6_computational PUIAS computational Base 6 - x86_64 2,868base CentOS-6 - Base - mirrors.aliyun.com 6,518epel Extra Packages for Enterprise Linux 6 - x86_64 11,592extras CentOS-6 - Extras - mirrors.aliyun.com 38updates CentOS-6 - Updates - mirrors.aliyun.com 1,155repolist: 22,171
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 gityum install -y perl-devel perl-ExtUtils-Embed
如果部分包不能安装,可能需要增加“RHEL6”的安装源。
yum-config-manager --enable rhel-6-server-optional-rpms
检查Python版本:
#应确保其版本为2.5+:[root@Gitlab ~]# python --versionPython 2.6.6
除此之外还有一项重要的工作就是升级git版本,因为gitlab要求其版本在1.7.2。如果想要升级到最新版本,那么就需要手动编译安装。由于当前的系统已经安装了git,所以直接从“github”上克隆一个最新版。
#网上说如果通过yum安装,只能装到1.7.1,但是我们当前的版本确是如下所示的版本,应该是我们增加了软件源的缘故。[root@Gitlab ~]# git --versiongit version 1.8.3.1-----------------------#升级到最新版本:[root@Gitlab ~]# cd tools/[root@Gitlab tools]# git clone https://github.com/git/git.git#升级前最好备份当前的版本:[root@Gitlab tools]# mv /usr/bin/git{,.bak}#然后开始进行编译安装:[root@Gitlab tools]# cd git/[root@Gitlab git]# make configureGIT_VERSION = 2.4.0.GITGEN configure[root@Gitlab git]# ./configure --prefix=/usr/bin[root@Gitlab git]# make && make install#创建软链接(这里是个小坑):[root@Gitlab git]# ln -s /usr/bin/bin/git /usr/bin/git#最后进行验证:[root@Gitlab bin]# git --versiongit version 2.4.0.GIT
若是条件允许,从源码编译安装有很多好处,至少可以安装最新的版本。它的每个版本都在不断尝试改进用户体验,所以能通过源代码自己编译安装最新版本就再好不过了。
设置为开机自启动并启动服务。
[root@Gitlab ~]# chkconfig redis on[root@Gitlab ~]# service redis startStarting redis-server: [ OK ]
暂略
[root@Gitlab ~]# cd tools/[root@Gitlab tools]# wget ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p645.tar.gz[root@Gitlab tools]# tar zxvf ruby-2.0.0-p645.tar.gz[root@Gitlab tools]# cd ruby-2.0.0-p645./configure --prefix=/usr/local/make && make install
安装完成后,检测安装成功与否:
[root@Gitlab ~]# ruby -vruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-linux]
注意:这里最好使用2.0.0版本,使用更高版本的话后面的操作会出现报错,很难解决。
这是一个安装ruby的包系统,用来管理gem。安装之前,首先需要添加“rubygems”的国内镜像,否则以国内的网络环境几乎无法连接到。详见:http://ruby.taobao.org
[root@Gitlab ~]# gem sources --remove https://rubygems.org/https://rubygems.org/ removed from sources[root@Gitlab ~]# gem sources -a https://ruby.taobao.org/https://ruby.taobao.org/ added to sources[root@Gitlab ~]# gem sources -l*** CURRENT SOURCES ***https://ruby.taobao.org/ #请确保这里只有一个源。
[root@Gitlab ~]# gem install bundler --no-ri --no-rdocFetching: bundler-1.10.3.gem (100%)Successfully installed bundler-1.10.31 gem installed
因为该用户不需要进行登录,所以这里不需要设置密码。另外,为了下面的安装操作方便一些,推荐为该用户进行sudo授权。
[root@Gitlab ~]# useradd -c 'Gitlab' git[root@Gitlab ~]# visudogit ALL=(ALL) NOPASSWD: ALL
这是专门为“GitLab”开发的提供ssh访问和版本管理的软件。
#首先要切换用户:[root@Gitlab ~]# su - git#克隆:[git@Gitlab ~]$ git clone https://github.com/gitlabhq/gitlab-shell.git[git@Gitlab ~]$ cd gitlab-shell/#切换成1.8.0版本,并编辑配置文件:[git@Gitlab gitlab-shell]$ git checkout v1.9.1[git@Gitlab gitlab-shell]$ cp config.yml.example config.yml#这里最重要的是将gitlab_url修改成使用的域名。如果使用的域名是测试域名,不要忘记在系统的hosts做域名映射。[git@Gitlab gitlab-shell]$ vim config.ymlgitlab_url: "http://gitlab.cdmonkey.com/"
#最后,创建一些需要的目录和文件(具体见下面的输出内容):[git@Gitlab gitlab-shell]$ ./bin/installmkdir -p /home/git/repositories: OKmkdir -p /home/git/.ssh: OKchmod 700 /home/git/.ssh: OKtouch /home/git/.ssh/authorized_keys: OKchmod 600 /home/git/.ssh/authorized_keys: OKchmod -R ug+rwX,o-rwx /home/git/repositories: OKfind /home/git/repositories -type d -exec chmod g+s {} ;: OK
注意:如果通过使用“https”访问,则需进行如下改动:
gitlab_url: "https://gitlab.cdmonkey.com/"self_signed_cert: true
[root@Gitlab ~]# yum install -y mysql-server mysql-devel[root@Gitlab ~]# chkconfig mysqld on[root@Gitlab ~]# /etc/init.d/mysqld start#设置root用户的密码:[root@Gitlab ~]# /usr/bin/mysql_secure_installation
创建新用户和数据库给gitlab使用:
[root@Gitlab ~]# mysql -uroot -pmysql> GRANT USAGE ON *.* TO 'gitlab'@'localhost' IDENTIFIED BY '123';mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';
[root@Gitlab ~]# su - git[git@Gitlab ~]$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab#切换分支(这步还不到是干什么用)。[git@Gitlab ~]$ cd gitlab[git@Gitlab gitlab]$ git checkout 6-7-stableBranch 6-7-stable set up to track remote branch 6-7-stable from origin.Switched to a new branch '6-7-stable'
[git@Gitlab ~]$ cd /home/git/[git@Gitlab ~]$ cp gitlab/config/gitlab.yml.example gitlab/config/gitlab.yml[git@Gitlab ~]$ vim gitlab/config/gitlab.yml#修改配置文件中的访问域名:host: gitlab.cdmonkey.comport: 80https: true #若需要使用https来访问,这里也要进行修改。-----------------------#设定一些目录所有者和权限:[git@Gitlab ~]$ cd /home/git/gitlabchown -R git log/chown -R git tmp/chmod -R u+rwX log/chmod -R u+rwX tmp/#创建目录:[git@Gitlab gitlab]$ mkdir /home/git/gitlab-satellites#[git@Gitlab gitlab]$ mkdir tmp/pids/[git@Gitlab gitlab]$ mkdir tmp/sockets/[git@Gitlab gitlab]$ chmod -R u+rwX tmp/pids/[git@Gitlab gitlab]$ chmod -R u+rwX tmp/sockets/#[git@Gitlab gitlab]$ mkdir public/uploads[git@Gitlab gitlab]$ chmod -R u+rwX public/uploads#创建并编辑“unicorn”配置文件,当前实验采用默认配置。[git@Gitlab gitlab]$ cp config/unicorn.rb.example config/unicorn.rb[git@Gitlab gitlab]$ vim config/unicorn.rblisten "127.0.0.1:8080", :tcp_nopush => true------------------#配置git的用户和邮件:[git@Gitlab gitlab]$ git config --global user.name "GitLab"[git@Gitlab gitlab]$ git config --global user.email "gitlab@localhost"[git@Gitlab gitlab]$ git config --global core.autocrlf input
以上的配置比较复杂和琐碎,细心些就行了。一定要确保gitlab.yml和unicorn.rb两个文件设置正确。
[git@Gitlab gitlab]$ cp config/database.yml.mysql config/database.yml#在配置文件中设置连接数据库的用户名和密码(只需修改“production”部分即可):[git@Gitlab gitlab]$ vim config/database.ymlproduction:adapter: mysql2encoding: utf8reconnect: falsedatabase: gitlabhq_productionpool: 10reaping_frequency: 10username: gitlabpassword: "123"-----------------------#确保该文件只有git账号有权限读取:[git@Gitlab gitlab]$ chmod o-rwx config/database.yml
注意这里还有一个配置文件需要编辑:
[git@Gitlab gitlab]$ cp config/resque.yml.example config/resque.yml[git@Gitlab gitlab]$ vim config/resque.yml#设置“Redis”的监听地址:production: redis://localhost:6379
[git@Gitlab ~]$ cd gitlab#更换ruby源,加快包的安装速度:[git@Gitlab gitlab]$ vim Gemfilesource 'https://ruby.taobao.org/'
#切换回根用户进行安装:[root@Gitlab ~]# gem install charlock_holmes --version '0.6.9.4'
[root@Gitlab ~]# su - git[git@Gitlab ~]$ cd gitlab[git@Gitlab gitlab]$ bundle install --deployment --without development test postgres puma awsFetching source index from https://ruby.taobao.org/...Post-install message from httparty:When you HTTParty, you must party hard!
[git@Gitlab gitlab]$ bundle exec rake gitlab:setup RAILS_ENV=production...Administrator account created:login.........admin@local.hostpassword......5iveL!fe#操作完成后,就会生成一个默认的管理员账号:admin@local.host5iveL!fe
#切换到根用户执行:[root@Gitlab ~]# wget -O /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/master/init/sysvinit/centos/gitlab-unicorn[root@Gitlab ~]# chmod +x /etc/init.d/gitlab[root@Gitlab ~]# chkconfig --add gitlab#设置为开机自启动:[root@Gitlab ~]# chkconfig gitlab on
[root@Gitlab ~]# su - git[git@Gitlab ~]$ cd gitlab/[git@Gitlab gitlab]$ bundle exec rake gitlab:env:info RAILS_ENV=productionSystem informationSystem: CentOS 6.6Current User: gitUsing RVM: noRuby Version: 2.0.0p645Gem Version: 2.0.14Bundler Version:1.10.3Rake Version: 10.1.1GitLab informationVersion: 6.7.5Revision: 00aa5c1Directory: /home/git/gitlabDB Adapter: mysql2URL: http://gitlab.cdmonkey.comHTTP Clone URL: http://gitlab.cdmonkey.com/some-project.gitSSH Clone URL: git@gitlab.cdmonkey.com:some-project.gitUsing LDAP: noUsing Omniauth: noGitLab ShellVersion: 1.9.1Repositories: /home/git/repositories/Hooks: /home/git/gitlab-shell/hooks/Git: /usr/bin/git
#切换到根用户执行:[root@Gitlab ~]# cd /home/git/gitlab[root@Gitlab gitlab]# cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
#启动服务这里又是一个坑,不执行下面的操作启动就会报错。除此之外,也可以修改服务启动文件,这里就省略了。[git@Gitlab ~]$ cd gitlab[git@Gitlab gitlab]$ cp -r script/* bin
#启动服务要以根用户的身份来执行:[git@Gitlab gitlab]$ sudo /etc/init.d/gitlab startStarting unicorn: [ OK ]Starting sidekiq: [ OK ]
查看应用更加详细的检测信息:
[git@Gitlab gitlab]$ bundle exec rake gitlab:check RAILS_ENV=production
报错信息及处理:
...from /home/git/gitlab-shell/bin/check:11:in `<main>'`gitlab-shell self-check failedTry fixing it:Make sure GitLab is running;Check the gitlab-shell configuration file:sudo -u git -H editor /home/git/gitlab-shell/config.ymlPlease 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接口。 |
最后的设定内容:
[git@Gitlab gitlab]$ vim config/gitlab.ymlhost: gitlab.cdmonkey.comport: 80https: true[git@Gitlab gitlab]$ vim config/unicorn.rblisten "127.0.0.1:8080", :tcp_nopush => true[git@Gitlab gitlab-shell]$ vim config.ymlgitlab_url: "http://gitlab.cdmonkey.com:8080"
#可以不用处理这个下面这个问题:Init script up-to-date? ... noTry fixing it:Redownload the init scriptFor more information see:doc/install/installation.md in section "Install Init Script"Please fix the error above and rerun the checks.
Install Nginx:
[root@Gitlab ~]# yum -y install nginx[root@Gitlab ~]# chkconfig nginx on[root@Gitlab ~]# mkdir /etc/nginx/sites-available[root@Gitlab ~]# mkdir /etc/nginx/sites-enabled[root@Gitlab ~]# cd /home/git/gitlab[root@Gitlab gitlab]# cp lib/support/nginx/gitlab /etc/nginx/sites-available/[root@Gitlab gitlab]# ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
修改配置文件:
[root@Gitlab ~]# vim /etc/nginx/nginx.conf#把原来加载配置文件的这行注释掉,替换为以下这行:include /etc/nginx/sites-enabled/*;
[root@Gitlab ~]# vim /etc/nginx/sites-available/gitlab#设置访问域名:server_name gitlab.cdmonkey.com;
[root@Gitlab ~]# usermod -a -G git nginx[root@Gitlab ~]# chmod g+rx /home/git/
添加ssl证书或者自己生成一个:
[root@Gitlab ~]# cd /etc/nginx/[root@Gitlab nginx]# openssl req -new -x509 -nodes -days 3560 -out gitlab.crt -keyout gitlab.key
启动服务:
[root@Gitlab ~]# /etc/init.d/nginx start
这里有一步是拉取静态资源文件,需要执行,否侧会报“502”错误:
[root@Gitlab ~]# sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
至此就算安装完成了。默认的账号密码:
admin@local.host
5iveL!fe

[root@Gitlab ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 39892/mysqldtcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 28515/redis-servertcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15651/nginxtcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 15132/unicorn_rails...