Git
是一種分散式的程式碼版本管理系統,git在工作時可以不用時刻依賴後臺伺服器,在本地電腦上就可以管理版本控制,但是在需要協同開發時就必須要使用後臺伺服器了,目前網際網路上有github,碼雲這樣的遠端程式碼庫可以使用,但是對於企業來說,自己的核心程式碼還是放在企業內部比較安全。

Gitlab
是一款開源的git程式碼版本管理系統的後臺伺服器,在一個生產程式碼的企業裡,這樣的伺服器是必不可少的,下面就來探討一下如何搭建企業gitlab伺服器。

1.      根據官方安裝文件安裝相應的包

sudo yum install -y curl policycoreutils-python openssh-server cronie
sudo lokkit -s http -s ssh

第一條命令:安裝依賴包

第二條命令:允許通過http和ssh方式訪問gitlab

2.        手動下載gitlab-ce安裝包

下載地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-11.9.8-ce.0.el6.x86_64.rpm

3.        安裝gitlab-ce

rpm -ivh gitlab-ce-11.9.8-ce.0.el6.x86_64.rpm

出現上面的介面表明gitlab-ce安裝完成

4.        漢化

漢化包下載地址:https://gitlab.com/xhang/gitlab/tree/11-9-stable-zh

本次安裝的gitlab版本是V11.9.8,那就下和這個版本一樣的漢化包

wget https://gitlab.com/xhang/gitlab/-/archive/11-9-stable-zh/gitlab-11-9-stable-zh.tar.gz

解壓漢化包

tar -xzvf gitlab-11-9-stable-zh.tar.gz

備份英文版本

cp -r/opt/gitlab/embedded/service/gitlab-rails{,.bak}

覆蓋英文版本

cd gitlab-11-9-stable-zh
cp -rf */opt/gitlab/embedded/service/gitlab-rails/

5.        配置gitlab.rb

Gitlab的配置檔案:/etc/gitlab/gitlab.rb

修改這個配置檔案,修改如下內容行:

egrep -v '#' /etc/gitlab/gitlab.rb |egrep -v '^$'

external_url 'http://xytgit'
gitlab_rails['backup_keep_time'] = 31536000
user['username'] = "git"
user['group'] = "git"
unicorn['listen'] = '192.168.10.21'
unicorn['port'] = 8088
nginx['listen_port'] = 80

說明:

第一行:配置訪問URL

第二行:備份儲存時間

第三行:gitlab執行使用者

第四行:gitlab執行使用者組

第五行:unicorn監聽地址 unicorn是ruby寫的httpserver

每六行:unicorn監聽埠

第7行:gitlab自帶nginx監聽埠

6.        執行gitlab-ctl reconfigure

報瞭如下的錯,意思是說建立連結檔案tmp時,發現已存在一個叫tmp的目錄,因此無法建立一個叫tmp的連結,仔細檢查漢化目錄發現的確存在一個tmp的目錄,解決方法是刪除tmp目錄。

除了tmp,還有一個log目錄也同樣需要刪除,否則無法建立連結檔案

7.        啟動gitlab服務

Gitlab-ctl start

8.        檢視gitlab服務狀態

Gitlab-ctl status

9.        登入gitlab主頁

10.   502錯誤解決方法

發生502錯誤,基本上都是因為埠被佔用,修改合適埠即可。

Gitlab-ctl reconfigure
Gitlab-ctl restart

11.   Gitlab資料備份

gitlab-rake gitlab:backup:create

資料備份的目錄在/etc/gitlab/gitlab.rb配置檔案中有定義

gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"

12.   Gitlab資料恢復

先停止相關服務

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

gitlab-rake
gitlab:backup:restore BACKUP=
備份檔案編號

例如我們的備份檔案的編號是1502357536_2017_08_10_9.4.3,因此執行下面的命令即可恢復gitlab

gitlab-rake gitlab:backup:restore BACKUP=1502357536_2017_08_10_9.4.3

敲完命令後,出現第一個互動頁面,

# gitlab-rake gitlab:backup:restore BACKUP=1502357536_2017_08_10_9.4.3 Unpacking backup ... done Before restoring the database we recommend removing all existing tables to avoid future upgrade problems. Be aware that if you have custom tables in the GitLab database these tables and all data will be removed. Do you want to continue (yes/no)?

輸入yes即可恢復。