GitLab 簡介
因為我的個人網站 restran.net 已經啟用,博客園的內容已經不再更新。這篇文章是在 Gitlab 7.4 的環境下配置的,相關內容可能已經過時。
后續做了一次遷移,將 Gitlab 升級到了 8.9,又重新整理了一篇文章 CentOS 7 Minimal 安裝 Gitlab 8.9,如果有需要可以前往查看。
其他相關文章:
提供 Git 項目倉庫托管服務的有業界聞名的 GitHub,但你要將代碼上傳到 GitHub上面,而且將項目設為私有還要收費。而 GitLab 則是開源免費的(社區版免費,企業版需要訂閱),同樣是采用了 Ruby on Rails 開發,可以讓你在自己的內網搭建一個“山寨版”的 GitHub。GitHub 的使用體驗是誘人的,因此部署自己的 GitLab 就十分吸引人。
Gitlab的Github地址
https://github.com/gitlabhq/gitlabhq
安裝部署
以下內容為自己閱讀官方文檔並試驗后的學習筆記加部分翻譯
GitLab 提供了兩種方式來安裝,一種是使用官方打包好的文件,這種方法比較簡單,也不容易出錯;另一種是手動安裝,就是把代碼下載下來,然后安裝各種環境,數據庫,編譯環境等等,一步一步安裝起來,這種就比較麻煩,但是可以安裝各種分支版本,比如中文翻譯版。
先介紹下自己的環境,是在虛擬機中安裝的:
Ununtu 14.04,GitLab 7.4.2 Community Edition
一、使用官方安裝包安裝
官方安裝文檔
https://about.gitlab.com/downloads/
1. 下載gitlab安裝包
https://downloads-packages.s3.amazonaws.com/ubuntu-14.04/gitlab_7.4.2-omnibus-1_amd64.deb
官方文檔使用wget,但是這個文件有280MB,還是用迅雷先下載好,速度比較快。
2. 安裝 openssh-server
sudo apt-get install openssh-server
3. 安裝郵件服務器
sudo apt-get install postfix
這里選擇Internet Site
然后讓設置FQDN,但是使用默認的機器名即可,這里不是很確定。
4. 安裝gitlab安裝包
sudo dpkg -i gitlab_7.4.2-omnibus-1_amd64.deb
安裝完之后,會建立一個git用戶和一個gitlab-www用戶,在GitLab上建立的代碼倉庫都是放在/home/git/下面。但是奇怪的是,該目錄竟然對當前登錄的管理員用戶不可見,需要用git用戶登錄才可以,而且git用戶還不能登錄到圖形窗口,只能登錄到命令行。由於git用戶建立時使用了--disabled-login,需要先設置密碼才能登錄,可以用passwd git,修改git用戶的密碼。
5. 配置 gitlab
這一步在官方的文檔里面沒有,但是如果沒有配置的話,直接啟動GitLab,會出現不正確的FQDN錯誤,導致無法正常啟動。因此必須做配置。
這邊的配置使用這里分享的經驗。
sudo mkdir -p /etc/gitlab
sudo touch /etc/gitlab/gitlab.rb
sudo chmod 600 /etc/gitlab/gitlab.rb
sudo gedit /etc/gitlab/gitlab.rb
![修改'\({external_url}'][5] 把'\){external_url}'改成部署機器的域名或者IP地址
這個地址很重要,上傳的圖片什么的,url會以這個為基准,如果地址寫錯,將無法訪問到圖片等這些資源。
6. 然后對GitLab進行重配置即可
這一步也是啟動 GitLab
sudo gitlab-ctl reconfigure
7. 打開瀏覽器登陸
Username: root
Password: 5iveL!fe
第一次啟動的時候,需要初始化大量的東西,經常會出現502錯誤,通常是由於內存不足的原因導致,所以需要准備好足夠的內存。官方推薦生產環境中使用2G內存,2核CPU。虛擬機中測試1GB內存基本就可以了。
二、手動安裝
官方安裝文檔
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md
安裝前最好先確認下能否訪問amazon aws上面的資源,因為有很多安裝包需要從這里獲取,如果不行就先配置下代理服務器或者VPN,解決這個問題。否則后面的安裝會很痛苦。
安裝過程包含以下幾個內容
- Packages / Dependencies
- Ruby
- System
- Users
- Database
- Redis
- GitLab
- Nginx
1. Packages / Dependencies
需要先更新下系統,不然有些依賴的包會找不到
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install sudo -y
下面這一步為可選,如果熟悉用vim就安裝vim為默認的編輯器,或者選擇其它
sudo apt-get install -y vim
sudo update-alternatives --set editor /usr/bin/vim.basic
安裝依賴,編譯 Ruby 的時候需要用到
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
安裝 Git
sudo apt-get install -y git-core
需要確保 Git 的版本在 1.7.10 以上,如 1.7.12 或 2.0.0
git --version
如果當前系統中的 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
cd /tmp
curl -L --progress https://www.kernel.org/pub/software/scm/git/git-2.1.2.tar.gz | tar xz
cd git-2.1.2/
make prefix=/usr/local all
安裝到 /usr/local/bin
sudo make prefix=/usr/local install
When editing config/gitlab.yml (Step 5), change the git -> bin_path to /usr/local/bin/git
安裝郵件服務器,用於發送 GitLab 的通知郵件
sudo apt-get install -y postfix
Then select 'Internet Site' and press enter to confirm the hostname.
安裝郵件服務器,選擇 Internet Site,並在 FQDN 處,設置為主機名(缺省就是主機名)
2. Ruby
如果已經安裝了 Ruby 1.8 版本,先刪除
sudo apt-get remove ruby1.8
下載並編譯安裝 Ruby
mkdir /tmp/ruby && cd /tmp/ruby
curl -L --progress ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz | tar xz
cd ruby-2.1.2
./configure --disable-install-rdoc
make
sudo make install
安裝 Bundler Gem:
sudo gem install bundler --no-ri --no-rdoc
如果步驟1中的依賴部分,有些沒有安裝成功的話,這里就會出現錯誤,比如
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
3. System Users
在系統中為 GitLab 創建一個 git 用戶
Create a git user for GitLab:
sudo adduser --disabled-login --gecos 'GitLab' git
這里創建的系統用戶,顯示的用戶名是GitLab,但是在home目錄下已經有/home/git了
4. Database
官方推薦使用 PostgreSQL 數據庫
安裝數據庫包
sudo apt-get install -y postgresql postgresql-client libpq-dev
登錄到 PostgreSQL
sudo -u postgres psql -d template1
為 GitLab創建一個用戶
template1=# CREATE USER git CREATEDB;
創建 GitLab 生成環境數據庫,並給數據庫授予所有權限
template1=# CREATE DATABASE gitlabhq_production OWNER git;
退出數據庫會話
template1=# \q
試一下用新建的用戶連接到新建的數據庫,然后退出會話
sudo -u git -H psql -d gitlabhq_production
template1=# \q
5. Redis
安裝 resis 服務器
sudo apt-get install redis-server
配置 redis 以使用 sockets
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig
將 redis 的監聽端口設置為 0 來關閉 TCP 監聽
sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf
Enable Redis socket for default Debian / Ubuntu path
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf
Grant permission to the socket to all members of the redis group
echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf
Create the directory which contains the socket
sudo mkdir /var/run/redis
sudo chown redis:redis /var/run/redis
sudo 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
sudo service redis-server restart
Add git to the redis group
sudo usermod -aG redis git
6. GitLab
GitLab 將被安裝到/homt/git目錄下
cd /home/git
通過克隆 GitLab 代碼倉庫的方式下載 GitLab
sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-4-stable gitlab
這里可以替換為其他版本,例如中文翻譯版
sudo -u git -H git clone https://gitlab.com/larryli/gitlab.git -b 7-4-zh gitlab
**配置 GitLab**
切換到 GitLab 安裝目錄
cd /home/git/gitlab
復制一份 GitLab 配置文件的例子
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
更新配置文件
sudo -u git -H editor config/gitlab.yml
需要配置兩項
host: example.com # Gitlab settings
email_from: example@example.com # Email settings
host 這個字段的值要改成服務器的ip或域名,不要包含 http,這個字段很重要,會影響到后面建立項目的地址,以及上傳的圖片的地址,如把 host 配置成192.168.137.135,那么項目地址就會使這樣
http://192.168.137.135/username/test_project.git
email_from 配置的項就是 Gitlab 發郵件時的發件人,這里一定要寫成一個合法的 email 地址,可以任意。
當新建用戶,或者有評論時,就會收到這里設置的郵箱發來的郵件。如果這里填的郵箱格式有錯,將導致收不到郵件。
為 GitLab 配置寫 log/ 和 tmp/ 目錄的權限
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX log/
sudo chmod -R u+rwX tmp/
Create directory for satellites
sudo -u git -H mkdir /home/git/gitlab-satellites
sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites
為 GitLab 配置寫 tmp/pids/ 和 tmp/sockets/ 目錄的權限
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/
為 GitLab 配置寫 public/uploads/ 目錄的權限
sudo chmod -R u+rwX public/uploads
復制一份 Unicorn 配置文件的例子
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
查看當前機器有多少核CPU
nproc
Enable cluster mode if you expect to have a high load instance
Ex. change amount of workers to 3 for 2GB RAM server
Set the number of workers to at least the number of cores
sudo -u git -H editor config/unicorn.rb
復制一份 Rack attack 配置文件的例子
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
Configure Git global settings for git user, useful when editing via web
根據 gitlab.yml 中配置的信息來配置 user.email
sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "example@example.com"
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 editor 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 only:
sudo -u git cp config/database.yml.postgresql config/database.yml
MySQL only:
sudo -u git cp config/database.yml.mysql config/database.yml
MySQL and remote PostgreSQL only:
Update username/password in config/database.yml.
You only need to adapt the production settings (first part).
If you followed the database guide then please do as follows:
Change 'secure password' with the value you have given to $password
You can keep the double quotes around the password
sudo -u git -H editor config/database.yml
PostgreSQL and MySQL:
Make config/database.yml readable to git only
sudo -u git -H chmod o-rwx config/database.yml
Install Gems
Note: As of bundler 1.5.2, you can invoke bundle install -jN (where N the number of your processor cores) and enjoy the parallel gems installation with measurable difference in completion time (~60% faster). Check the number of your cores with nproc. For more information check this post. First make sure you have bundler >= 1.5.2 (run bundle -v) as it addresses some issues that were fixed in 1.5.2.
For PostgreSQL (note, the option says "without ... mysql")
如果使用 PostgreSQL,請執行這條命令
sudo -u git -H bundle install --deployment --without development test mysql aws
這里如果出現安裝錯誤,請檢查前面的依賴是否安裝正確
Or if you use MySQL (note, the option says "without ... postgres")
如果使用 MySQL,請執行這條命令
sudo -u git -H bundle install --deployment --without development test postgres aws
**安裝 GitLab Shell**
GitLab Shell is an SSH access and repository management software developed specially for GitLab.
Run the installation task for gitlab-shell (replace REDIS_URL
if needed):
sudo -u git -H bundle exec rake gitlab:shell:install[v2.0.1] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production
By default, the gitlab-shell config is generated from your main GitLab config.
You can review (and modify) the gitlab-shell config as follows:
sudo -u git -H editor /home/git/gitlab-shell/config.yml
如果要使用HTTPS,則要去查看官方文檔的HTTPS的配置步驟。
**Initialize Database and Activate Advanced Features**
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
Type 'yes' to create the database tables.
When done you see 'Administrator account created:'
Note: You can set the Administrator password by supplying it in environmental variable GITLAB_ROOT_PASSWORD, eg.:
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production GITLAB_ROOT_PASSWORD=newpassword
Install Init Script
Download the init script (will be /etc/init.d/gitlab):
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
And if you are installing with a non-default folder or user copy and edit the defaults file:
sudo cp lib/support/init.d/gitlab.default.example /etc/default/gitlab
If you installed GitLab in another directory or as a user other than the default you should change these settings in /etc/default/gitlab. Do not edit/etc/init.d/gitlab as it will be changed on upgrade.
Make GitLab start on boot:
sudo update-rc.d gitlab defaults 21
Setup 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
如果內存不夠,會導致無法分配內存,出錯。給虛擬機增加內存,重啟后,再次執行出現錯誤could not locate gemfile
原因是執行當前命令所在的目錄沒有gemfile,切換到目錄Gitlab安裝目錄
cd /home/git/gitlab
再次執行就可以了
Compile Assets
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
Start Your GitLab Instance
sudo service gitlab start
或者
sudo /etc/init.d/gitlab restart
7. Nginx
Note: Nginx is the officially supported web server for GitLab. If you cannot or do not want to use Nginx as your web server, have a look at the GitLab recipes.
Installation
sudo apt-get install -y nginx
Site Configuration
Copy the example site config:
sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab
sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
Make sure to edit the config file to match your setup:
Change YOUR_SERVER_FQDN to the fully-qualified
domain name of your host serving GitLab.
配置 Nginx
sudo editor /etc/nginx/sites-available/gitlab
需要配置兩項,servre中的listen和server_name,server_name跟gitlab.yml
中的host字段配置成一樣。
關鍵一點是去掉listen *:80 后面的default_server
否則執行sudo nginx -t時會出現nginx emerg a duplicate default server錯誤
如果是用gedit編輯的話,會在/etc/nginx/sites-available/保存一個default
需要刪除這個文件,否則會導致nginx出現
nginx error “conflicting server name” ignored
可以在var/log/niginx/error 里面看到
症狀:
訪問localhost,只出現nginx的歡迎頁,沒有出現gitlab的登錄頁面,
在后面執行
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
會發現gitlab-shell出錯
Note: If you want to use HTTPS, replace the gitlab Nginx config with gitlab-ssl. See Using HTTPS for HTTPS configuration details.
Test Configuration
Validate your gitlab or gitlab-ssl Nginx config file with the following command:
sudo nginx -t
You should receive syntax is okay and test is successful messages. If you receive errors check your gitlab or gitlab-ssl Nginx config file for typos, etc. as indicated in the error message given.
Restart
sudo service nginx restart
結束,所有的都配置完了。執行下面的命令來驗證是否配置成功。
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
如果所有的項目都是綠色的,表示 GitLab 已經成功安裝了!
NOTE: Supply SANITIZE=true environment variable to gitlab:check to omit project names from the output of the check command.
打開瀏覽器登陸吧!
用戶名:root
密碼:5iveL!fe
其他問題:
如果安裝的是 Larry Li 翻譯的 7-2-zh,在新建項目的時候,如果名稱填寫的不符合要求,會提示
Namecan contain only letters, digits, '', '-' and '.' and space. It must start with letter, digit or ''.
第一個單詞Name和can之間少了一個空格,安裝官方推薦方式安裝7.4.2官方版,沒有這個問題。
找到文件
/home/git/gitlab/lib/gitlab/regex.rb
找到 project_regex_message,在前面加上空格,其他類似的也可以一並修改
def project_regex_message
#"can contain only letters, digits, '_', '-' and '.' and space. " \
" can contain only letters, digits, '_', '-' and '.' and space. " \
"It must start with letter, digit or '_'."
end