將 GitLab 架設 於 Debian Wheezy

GitLab 是 Ruby on Rails 開發的應用程式, 操作使用非常類似 GitHub, 可以通過 Web 存取 公開 或 私人的 Project.

GitLab 介紹、測試

將 GitLab 架設於 Debian Wheezy

GitLab 系統需求

硬體需求(摘錄自此篇: Gitlabhq doc install requirements):

  • CPU
    • 1 core works for under 100 users but the responsiveness might suffer
    • 2 cores is the recommended number of cores and supports up to 100 users
    • 4 cores supports about 1,000 users
    • 8 cores supports up to 10,000 users
  • Memory
    • 1GB supports up to 100 users if you do not have individual repo's over 250MB
    • 2GB is the recommended memory size and supports up to 1,000 users
    • 4GB supports up to 10,000 users

系統安裝(Gitlabhq doc install installation.md)步驟如下:

系統、套件版本需求:

  • Ubuntu/Debian
  • python 2.5+ (3.x is not supported at the moment)
  • ruby 1.9.3+
  • git 1.7.10+
  • redis 2.0+
  • MySQL or PostgreSQL

基本環境安裝步驟

  • 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

  1. python --version # 如果是 python 3, 要安裝 python 2
  2. sudo apt-get install python2.7
  3. python2 --version # 要確認 python2 存在, 不存在執行下述命令
  4. sudo ln -s /usr/bin/python /usr/bin/python2
  5. sudo apt-get install -y python-docutils # For reStructuredText markup language support install required package

Git

  1. sudo apt-get install -y git-core
  2. git --version # 需要 git 1.7.10 以上, < 1.7.10 的話, 需要手動安裝新版的 Git.

Postfix

  1. sudo apt-get install -y postfix # 若之後需要寄通知信的話, 需要安裝 postfix

Ruby、Gem

  1. # Debian wheezy 的 ruby 是 1.9.1, 不夠新, 手動 Compile
  2. mkdir /tmp/ruby && cd /tmp/ruby
  3. curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz | tar xz
  4. cd ruby-2.0.0-p247
  5. ./configure --disable-install-rdoc
  6. make
  7. sudo make install
  8. sudo gem install bundler --no-ri --no-rdoc # Install the Bundler Gem

系統設定

  1. sudo adduser --disabled-login --gecos 'GitLab' git # 增加系統使用者

GitLab shell

  1. cd /home/git
  2. sudo -u git -H git clone https://github.com/gitlabhq/gitlab-shell.git
  3. cd gitlab-shell
  4. sudo -u git -H git checkout v1.7.4 # 切到正確版本
  5. sudo -u git -H cp config.yml.example config.yml
  6. sudo -u git -H editor config.yml # 主要修改 gitlab_url, 改成自己要的 http://domain.com/
  7. sudo -u git -H ./bin/install # 會依照 config.yml 設定、建目錄、key 等

Database

  1. GitLab Database 安裝說明(可參考此篇: gitlabhq doc install databases.md)
  2. sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
  3. mysql -u root -p
    1. mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'GITLAB_PASSWORD';
    2. mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
    3. mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';
  4. # 使用 gitlab 帳號, 測試 MySQL 連線狀況
  5. sudo -u git -H mysql -u gitlab -p -D gitlabhq_production # 輸入密碼看有沒有進入 mysql>

GitLab 系統安裝

  1. # 用 Git 帳號來 安裝 GitLab 系統
  2. cd /home/git
  3. sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq.git gitlab # Clone the Source, Clone GitLab repository
  4. cd /home/git/gitlab
  5. sudo -u git -H git checkout 6-2-stable # 切換到 6-2 stable 版本(git branch -a, git branch -r), 列表可見: https://github.com/gitlabhq/gitlabhq/branches

GitLab 設定

以下 *editor 的命令, 偷懶直接 sudo vim 編輯也可以.

  1. cd /home/git/gitlab
  2. sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
  3. sudo -u git -H editor config/gitlab.yml # 將 localhost 都修改成你的 Domain.
  4. sudo chown -R git log/
  5. sudo chown -R git tmp/
  6. sudo chmod -R u+rwX  log/
  7. sudo chmod -R u+rwX  tmp/
  8. sudo -u git -H mkdir /home/git/gitlab-satellites
  9. sudo -u git -H mkdir tmp/pids/
  10. sudo -u git -H mkdir tmp/sockets/
  11. sudo chmod -R u+rwX  tmp/pids/
  12. sudo chmod -R u+rwX  tmp/sockets/
  13. sudo -u git -H mkdir public/uploads
  14. sudo chmod -R u+rwX  public/uploads
  15. sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
  16. sudo -u git -H editor config/unicorn.rb # 如果有 2G Ram, 就將設定檔得 workers 改為 3,不然就不用動
  17. sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
  18. sudo -u git -H editor config/application.rb # 將 config.middleware.use Rack::Attack 打開, 前面 "#" 移除
  19. # 設定 git 帳號得 username、Email.. 等資料
  20. sudo -u git -H git config --global user.name "GitLab"
  21. sudo -u git -H git config --global user.email "gitlab@localhost"
  22. sudo -u git -H git config --global core.autocrlf input

GitLab DB 設定

  1. sudo -u git cp config/database.yml.mysql config/database.yml
  2. sudo -u git -H editor config/database.yml # 修改 MySQL 設定的帳號、密碼, 設定 production 就可以了.

安裝 Gems

  1. cd /home/git/gitlab
  2. sudo gem install charlock_holmes --version '0.6.9.4' # Character encoding detecting library for Ruby using ICU
  3. sudo -u git -H bundle install --deployment --without development test postgres aws # 安裝相關 Ruby Gems Package MySQL 執行此行, PostgreSQL 是把 "without postgres" 改成 "without mysql"
  4. 若遇到下述錯誤訊息 (使用 Percona MySQL)
    An error occurred while installing mysql2 (0.3.11), and Bundler cannot continue.
    Make sure that `gem install mysql2 -v '0.3.11'` succeeds before bundling.
  5. 解法: sudo apt-get install libmysqlclient18.1-dev # Percona 需安裝此 Library.
  6. 註: Gemfile.lock 可看到目前取用 mysql2 得版本是 0.3.11, 新版可見 https://rubygems.org/gems/mysql2
  7. 再來重新執行上述命令即可安裝完成: sudo -u git -H bundle install --deployment --without development test postgres aws
  8. sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production # 初始化 DB, 並將 Rails 設定到 Production mode, 輸入 'yes' 建立 database
  9. 產生 GitLab 預設帳號、密碼 如下:
    1. [email protected]
    2. password......5iveL!fe

設定開機自動啟動

  1. sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
  2. sudo chmod +x /etc/init.d/gitlab
  3. sudo update-rc.d gitlab defaults 21

設定 Logrotate

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

檢查 GitLab 環境設定狀態

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

啟動 GitLab

  1. sudo service gitlab start # 或 sudo /etc/init.d/gitlab restart

檢查 GitLab 各個系統狀態

  1. sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
  2. 訊息檢查說少了這些, 就補上去.
    1. sudo -u git -H git config --global user.name  "GitLab"
    2. sudo -u git -H git config --global user.email "gitlab@localhost"

設定 Apache2 環境

  1. sudo apt-get install apache2
  2. 可見: gitlab-recipes/web-server/apache at master · gitlabhq/gitlab-recipes
  3. 取得 github.conf: https://github.com/gitlabhq/gitlab-recipes/blob/master/web-server/apache/gitlab.conf
  4. cd /tmp/
  5. wget https://raw.github.com/gitlabhq/gitlab-recipes/master/web-server/apache/gitlab.conf
  6. sudo vim gitlab.conf # 修改 ServerName, ProxyPassReverse, Log 路徑(/var/log/httpd/logs/ => /var/log/apache2/)等等.
  7. sudo mv gitlab.conf /etc/apache2/sites-enabled/
  8. sudo a2enmod rewrite proxy proxy_http
  9. sudo service apache2 restart # 或 sudo /etc/init.d/apache2 restart

重新啟動 GitLab 方式

  • sudo /etc/init.d/gitlab restart

到此即安裝完成, 可用下述帳號、密碼登入:

  • http://gitlab.domain-name # 連線進入, 預設帳號、密碼如下:
  • login: [email protected]
  • password: 5iveL!fe

備份、還原

  • /home/git/ - 備份全部
  • 下述感謝 fetag 提供得資訊:
    • Gitlab 的備份與恢復可以參考:
    • https://github.com/gitlabhq/gitlabhq/blob/master/doc/raketasks/backup_restore.md
    • 是直接 bundle 執行 script 打包必要文件為 .tar 檔案,恢復時亦同。

問題除錯檢查

問題: 若是用 hosts 設定 假Domain 來作安裝的話, 如果指令(git push -u origin master)遇到下述錯誤:

Access denied.
fatal: The remote end hung up unexpectedly

解法:

  1. vim /home/git/gitlab-shell/config.yml
    1. Debug Log: /home/git/gitlab-shell/gitlab-shell.log , tail -f /var/log/auth.log
    2. Debug SSH: ssh -T -v [email protected]
  2. 翻 /home/git/gitlab-shell/gitlab-shell.log 可以看到錯誤訊息, 若錯誤訊息是 404, 請加上下述:

    $ vim /etc/hosts
    127.0.0.1  gitlab.domain-name

相關網頁

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈將 GitLab 架設 於 Debian Wheezy〉中有 2 則留言

fetag 發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料