GitLab 安裝

GitLab,是一個使用 Ruby on Rails 開發的開源應用程序,與Github類似,能夠瀏覽源代碼,管理缺陷和註釋,非常適合在團隊內部使用。

gitlab是基於Ruby on Rails的,安裝和配置非常麻煩,不過有傻瓜安裝包,https://about.gitlab.com/downloads/,或者,https://bitnami.com/stack/gitlab/installer。但是這兩個包都是英文界面的,gitlab把界面提示寫在每一個rb文件裏了,所以,要用中文版的不能像redmine那樣設定一下就行了。
需要通過源碼安裝中文版giglab,代碼地址:https://gitlab.com/larryli/gitlab,當前最新版爲8.5.5。

由於公司的操作系統普遍採用centos,爲統一管理,我們仍在centos上進行安裝,官方沒有8.5.5在centos上的安裝文檔,參考Ubuntu安裝手冊https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md,以及7.0版在centos上的安裝手冊https://github.com/gitlabhq/gitlab-recipes/tree/master/install/centos

開始之前

在開始之前請先查看官方的剛需文檔: https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/requirements.md ,該文檔說明了系統,軟件和硬件等各方面的需求。詳細的瞭解這些,可以避免碰到很多怪異的問題。

安裝步驟總覽

  • 更新倉庫源
  • 安裝必須的軟件包
  • 安裝Ruby
  • 安裝Go語言
  • 創建系統用戶
  • 安裝數據庫Mysql
  • 安裝Redis
  • 安裝GitLab
  • 安裝Nginx 

0、更新倉庫源

這個比較簡單,安裝完成之後記的配置下網絡,使其可以在啓動時自動連接。而後需要升級系統和安裝一些相應的軟件和依賴包,以下逐一說明。 
Tips:如果不能連接國外的網絡,經常出現網絡錯誤或者couldn’t not resolve host這樣的錯誤,建議修改dns服務器爲8.8.8.8和8.8.4.4。

a、升級操作系統和安裝wget

$ sudo yum -y update
	$ sudo yum -y install wget

升級完成後,系統版本是6.7。

b、增加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 http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
	$ 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/epel-release-latest-6.noarch.rpm

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

c、增加PUIAS安裝源

PUIAS Linux是面向桌面和服務器的完整的操作系統,它靠編譯Red Hat Enterprise Linux的源代碼包來創建。除了這些上游的軟件包外,該項目還提供一些其他的軟件倉庫:“Addons”包含了通常的Red Hat發行中未收入的額外軟件包,“Computational”提供專門針對科學計算的軟件,“Unsupported”則收入各種各樣的測試性軟件 包。該發行由美國普林斯頓 大學的高等研究所維護。

獲取/etc/yum.repos.d/PUIAS_6_computational.repo

$ sudo wget -O /etc/yum.repos.d/PUIAS_6_computational.repo https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/install/centos/PUIAS_6_computational.repo

下載並安裝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

c、增加REMI安裝源

$ sudo rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
	$ sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

   
默認地,REMI是禁用的。要檢查REMI是否已經成功安裝,使用這個命令。你會看到幾個REMI倉庫,比如remi、remi-php55和remi-php56。

$ yum repolist disabled | grep remi

從REMI倉庫中安裝一個包 
如上所述,最好保持禁用REMI倉庫,只有在需要的時候再啓用。 
要搜索或安裝REMI倉庫中的包,使用這些命令: 
代碼如下:

$ sudo yum --enablerepo=remi search
	$ sudo yum --enablerepo=remi install

Note: 安裝過程中需要手工編輯某些文件。通過下面命令可以將VIM設置爲默認編輯器

安裝vim並設置爲默認編輯器(不知都什麼原因,沒生效)

sudo yum -y install vim-enhanced
sudo update-alternatives --set editor /usr/bin/vim.basic

# For reStructuredText markup language support, install required package:
yum -y install python-docutils

1、安裝GitLab的所需依賴包和工具

a、安裝依賴包

$ su -
# yum -y groupinstall 'Development Tools'
# yum -y install readline readline-devel ncurses-devel gdbm-devel glibc-devel tcl-devel openssl-devel curl-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 redis sudo wget crontabs logwatch logrotate perl-Time-HiRes git cmake libcom_err-devel.i686 libcom_err-devel.x86_64 nodejs

b、安裝git

確定Git版本在2.7.3及以上

git --version

系統默認版本爲1.8.3,版本太老,刪除並通過源代碼安裝

刪除安裝的git

yum -y remove git

安裝git依賴包

$ sudo yum install curl-devel expat-devel gettext-devel openssl-devel perl-devel zlib-devel

下載源碼並編譯安裝 
mkdir /tmp/git && cd /tmp/git 
curl --progress https://www.kernel.org/pub/software/scm/git/git-2.8.2.tar.gz | tar xz 
cd git-2.8.2/ 
./configure 
make 
make prefix=/usr/local install

爲確保$PATH環境變量生效,需要重新連接後執行git --version

當編輯文件 config/gitlab.yml 時(第5步), 修改 git -> bin_path 爲 /usr/local/bin/git

c、安裝郵件服務器

接下來我們還要安裝一個郵件服務器,官方推薦使用postfix

yum install -y postfix

接下來選擇 'Internet Site' 並回車確定主機名

2、安裝Ruby

注意:當前支持的Ruby版本爲2.1.x. Ruby 2.2 和2.3尚不支持。

下載並編譯:

su -
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress https://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.8.tar.gz | tar xz
cd ruby-2.1.8
./configure --disable-install-rdoc
make
make prefix=/usr/local install

安裝完成後,重新登錄終端確保$PATH生效,檢測ruby的安裝成功與否:

$ which ruby
	/usr/local/bin/ruby
	$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]

Gitlab的包使用bundler進行依賴關係管理,所以還得安裝。如果在國內的用戶請先修改Ruby的源服務器

gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
gem sources -l

安裝bundle:

$ sudo gem install bundler --no-ri --no-rdoc

如果提示sudo: gem: command not found,使用root賬號登錄執行該命令即可。

3.安裝Go語言支持

在GitLab8.0以後HTTP請求開始依賴Go編譯,所以我們要進行安裝,這裏要注意Go會區分操作系統位數

mkdir /tmp/golang && cd /tmp/golang

curl -O --progress https://storage.googleapis.com/golang/go1.5.3.linux-amd64.tar.gz

tar -C /usr/local -xzf go1.5.3.linux-amd64.tar.gz

sudo ln -sf /usr/local/go/bin/{go,godoc,gofmt} /usr/local/bin/

4、創建系統用戶

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

修改git用戶的PATH路徑

visudo
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin

修改爲

Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

5.安裝數據庫

注意:因爲gitlab需要最低9.1版本,centos默認的8.x版postgreSQL無法支持, 需要添加 PGDG yum庫,如果系統已安裝postgresql需要先進行卸載:

yum remove postgresql

安裝 pgdg 資源庫:

rpm -Uvh http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-1.noarch.rpm
wget https://www.postgresql.org/ftp/source/v9.3.8/postgresql-9.3.8.tar.gz 

安裝 postgresql93-server 以及 postgreqsql93-devel 包:

yum install postgresql93-server postgresql93-devel

可執行文件被安裝在 /usr/pgsql-9.3/bin/。 爲保證正常執行,需要將該路徑添加到 $PATH 環境變量或創建鏈接。這裏我們爲gitlab創建鏈接:

ln -s /usr/local/postgresql-9.3.8/bin/pg_dump /usr/bin/pg_dump
ln -s /usr/local/postgresql-9.3.8/bin/pg_restore /usr/bin/pg_restore
ln -s /usr/local/postgresql-9.3.8/bin/psql /usr/bin/psql

重命名服務腳本爲postgresql:

mv /etc/init.d/{postgresql-9.3,postgresql}

ln -s /usr/local/postgresql-9.3.8/bin/psql /usr/bin/psql
 

初始化數據庫

service postgresql initdb

啓動postgresql服務,並設置開機自動啓動:

service postgresql start
chkconfig postgresql on

配置數據庫用戶及密碼

su - postgres
export PATH=$PATH:/usr/pgsql-9.3/bin/
psql -d template1

psql (9.4.3)
Type "help" for help.
template1=# CREATE USER git CREATEDB;
CREATE ROLE
template1=# CREATE DATABASE gitlabhq_production OWNER git;
CREATE DATABASE
template1=# \q
exit # exit uid=postgres, return to root

使用 gitlab 用戶 (uid=git) 測試連接,檢查當前用戶:

whoami

用 git 用戶連接Postgres:

sudo -u git psql -d gitlabhq_production

顯示如下提示:

gitlabhq_production=>

說明已經連接成功,可以通過 \q 命令退出

在 /var/lib/pgsql/9.3/data/pg_hba.conf 中配置認證方式及認證信息:

host    all             all             127.0.0.1/32            trust

6.安裝Redis

Gitlab要求Redis版本不低於2.8,系統默認安裝版本爲2.4.1,卸載當前版本並採用remi源安裝最新版本:

卸載當前redis:

yum -y remove redis

採用remi源安裝最新版本:

yum --enablerepo=remi install redis

設置開機自動啓動:

chkconfig redis on
Configure redis to use sockets:
cp /etc/redis.conf /etc/redis.conf.orig

Disable Redis listening on TCP by setting 'port' to 0:

sed 's/^port .*/port 0/' /etc/redis.conf.orig | sudo tee /etc/redis.conf

Enable Redis socket for default CentOS path:

echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis.conf
echo -e 'unixsocketperm 0770' | sudo tee -a /etc/redis.conf

Create the directory which contains the socket

mkdir /var/run/redis
chown redis:redis /var/run/redis
chmod 755 /var/run/redis

Persist the directory which contains the socket, if applicable

if [ -d /etc/tmpfiles.d ]; then
    echo 'd  /var/run/redis  0755  redis  redis  10d  -' | sudo tee -a /etc/tmpfiles.d/redis.conf
fi

Activate the changes to redis.conf:

service redis restart

Add git to the redis group:

usermod -aG redis git

7. GitLab

我們將 GitLab 安裝到 "git" 用戶的home中:

cd /home/git

獲取源代碼

選擇中文版代碼地址進行克隆,默認採用master版本,對應gitlab8.5.5

sudo -u git -H git clone https://gitlab.com/larryli/gitlab.git gitlab

配置gitlab

進入gitlab安裝目錄

cd /home/git/gitlab

複製gitlab配置文件

sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml

Update GitLab config file, follow the directions at top of file

sudo -u git -H vim config/gitlab.yml

修改 gitlab -> host 爲主機域名 
更新 gitlab -> email_from 爲[email protected] 
修改 git -> bin_path 爲 /usr/local/bin/git

複製安全文件

sudo -u git -H cp config/secrets.yml.example config/secrets.yml
sudo -u git -H chmod 0600 config/secrets.yml

保證 GitLab 對 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/

保證 GitLab 對 tmp/pids/ 和 tmp/sockets/ 目錄具備寫權限

sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/

創建 public/uploads/ 目錄

sudo -u git -H mkdir public/uploads/

保證只有 GitLab 用戶能夠訪問 public/uploads/ 目錄 
因此 public/uploads 中的文件通過 gitlab-workhorse 提供服務

sudo chmod 0700 public/uploads

修改 CI 構建跟蹤存儲目錄權限

sudo chmod -R u+rwX builds/

修改 CI artifacts 存儲目錄權限

sudo chmod -R u+rwX shared/artifacts/

複製並創建 unicorn 配置文件

sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

查詢內核數了

nproc

如果支持高負載可以啓用集羣設置 
將 workers 數據設置爲不小於CPU內核數量 
Ex. change amount of workers to 3 for 2GB RAM server

sudo -u git -H vim config/unicorn.rb

通過複製 example 創建 Rack attack 配置文件

sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb

爲 git 用戶配置全局 Git 配置,當使用 web 編輯器進行編輯時使用

sudo -u git -H git config --global core.autocrlf input

配置 Redis 連接

sudo -u git -H cp config/resque.yml.example config/resque.yml

Change the Redis socket path if you are not using the default Debian / Ubuntu configuration

sudo -u git -H vim config/resque.yml

Important Note: Make sure to edit both gitlab.yml and unicorn.rb to match your setup.
Note: If you want to use HTTPS, see Using HTTPS for the additional steps.

配置 GitLab 數據庫連接配置

只採用 PostgreSQL 配置信息:

sudo -u git cp config/database.yml.postgresql config/database.yml

只爲 git 用戶分配 config/database.yml 的讀權限

sudo -u git -H chmod o-rwx config/database.yml

安裝 Gems

修改 git 用戶的Ruby的源服務器

sudo -u git -H gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
sudo -u git -H gem sources -l
sudo -u git -H bundle config mirror.https://rubygems.org https://ruby.taobao.org

PostgreSQL 數據庫(不使用MySQL

sudo -u git -H bundle config build.pg --with-pg-config=/usr/pgsql-9.3/bin/pg_config
sudo -u git -H bundle install --deployment --without development test mysql aws kerberos

安裝 GitLab Shell

GitLab Shell 是一個 SSH 訪問以及倉庫管理軟件,專門用於 GitLab。

啓動 gitlab-shell 安裝任務 (請根據需要替換 REDIS_URL ):

sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production

默認情況下, gitlab-shell 是根據 GitLab 配置信息生成的。 
可以通過以下命令檢查及修改 gitlab-shell :

sudo -u git -H vim /home/git/gitlab-shell/config.yml

Note: Make sure your hostname can be resolved on the machine itself by either a proper DNS record or an additional line in /etc/hosts ("127.0.0.1 hostname"). This might be necessary for example if you set up GitLab behind a reverse proxy. If the hostname cannot be resolved, the final installation check will fail with "Check GitLab API access: FAILED. code: 401" and pushing commits will be rejected with "[remote rejected] master -> master (hook declined)".

安裝 gitlab-workhorse

cd /home/git
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-workhorse.git
cd gitlab-workhorse
sudo -u git -H git checkout 0.6.5
sudo -u git -H make

初始化數據庫及高級特性

進入 Gitlab 安裝目錄

cd /home/git/gitlab

sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production

輸入 'yes' 創建數據庫表結構

完成時顯示輸出 'Administrator account created:'

默認密碼

login.........root
password......5iveL!fe

本系統登錄後修改爲gitlab@2016

Note: You can set the Administrator/root password and e-mail by supplying them in environmental variables, GITLAB_ROOT_PASSWORD and GITLAB_ROOT_EMAIL respectively, as seen below. If you don't set the password (and it is set to the default one) please wait with exposing GitLab to the public internet until the installation is done and you've logged into the server the first time. During the first login you'll be forced to change the default password.
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=yourpassword GITLAB_ROOT_EMAIL=youremail
Secure secrets.yml

The secrets.yml file stores encryption keys for sessions and secure variables. Backup secrets.yml someplace safe, but don't store it in the same place as your database backups. Otherwise your secrets are exposed if one of your backups is compromised.

安裝啓動腳本

下載啓動腳本 (位於 /etc/init.d/gitlab):

sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
chkconfig --add gitlab

配置 GitLab 自動運行:

chkconfig gitlab on

安裝 Logrotate

sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab

檢查應用狀態

檢查 Gitlab 程序及環境是否正確配置:

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

編譯Assets

sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production

啓動 GitLab 服務

sudo service gitlab start

或者

sudo /etc/init.d/gitlab restart

8. 配置WebServer

這裏官方建議使用nginx,當然如果你對apache足夠熟悉也可以改用apache。 
首先配置nginx 源:

vim /etc/yum.repos.d/nginx.repo 
  1. [nginx]
  2. name=nginx repo
  3. baseurl=http://nginx.org/packages/centos/6/$basearch/
  4. gpgcheck=0
  5. enabled=1
yum -y  --enablerepo=nginx install nginx

chkconfig nginx on

如果你的機器上沒能IPV6地址,一定要註釋掉IPV6協議部分

cp lib/support/nginx/gitlab /etc/nginx/conf.d/gitlab.conf

添加用戶nginx到git組

usermod -a -G git nginx

chmod g+rx /home/git/

檢查配置

sudo nginx -t

重啓nginx:

sudo service nginx restart

安裝完成!

再次驗證應用狀態,保證安裝過程中沒有錯過任何步驟,可以通過以下命令驗證:

sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production

如果所有檢查項均顯示爲綠色,那麼恭喜你完成了 Gitlab 中文版安裝!

發佈了113 篇原創文章 · 獲贊 69 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章