Amazon Linux 2013.09 に GitLab6.1 をインストール

GitLabはGitHubのクローンです。 サーバにプライベートなGitHubを構築することができます。

Amazon Linux にインストールする方法をまとめました。

公式Debian/Ubuntu を対象としていますが、Amazon Linuxにインストールしみました。

Amazon Linux 2013.09」AMI起動直後のインスタンスにインストールしたメモを、公式の章立てに沿って残しておきます。

実施日は2013/10/19です。このような記事はバージョンが異なると動かなくなる可能性大なのでお気をつけください。

☆マークは勝手に追加した章立てです。


ミドルウェア


1. Packages / Dependencies

依存性のあるライブラリをインストールします。
公式にも多数ライブラリが記載されていますが、普段サーバに入れているライブラリ群をまずインストールしました。 無駄なライブラリも多数含まれていますのでこの辺りは公式から拾って適宜に。

$ yum -y groupinstall "Development tools"
$ yum install -y make gcc gcc-c++ patch zlib-devel bzip2-devel readline-devel libffi-devel openssl-devel git vim-enhanced libxslt-devel libxml2-devel
$ yum -y update

☆Redis

公式では1. 「1. Packages / Dependencies」にこっそり隠れているRedisをインストールします。

EPELからインストール
$ yum -y install --enablerepo=epel redis
$ chkconfig redis on
$ service redis start

動作確認
$ redis-cli ping
PONG

Python

Pythonは既にインストールされています。

Postfix

postfixをインストールしてDNSにAレコードを登録します。

インストール
$ yum -y install postfix

sendmail停止
$ service sendmail stop
$ chkconfig sendmail off

postfix起動
$ service postfix start

host名を一時的に設定
$ hostname gitlab.example.com

hostsにhost名を設定
$ vi /etc/hosts
-----------------
127.0.0.1   localhost localhost.localdomain gitlab.example.com
-----------------

再起動後のhost名を設定
$ vi /etc/sysconfig/network
-----------------
~
HOSTNAME=gitlab.example.com
~
-----------------

2. Ruby

rbenvでインストールします。

古いRubyを削除。
$ yum -y remove ruby

rbenvをインストール
$ cd /usr/local
$ git clone https://github.com/sstephenson/rbenv rbenv
$ mkdir rbenv/shims rbenv/versions

ruby-buildをインストール
$ git clone https://github.com/sstephenson/ruby-build ruby-build
$ cd ruby-build/
$ ./install.sh

rbenvの設定
ファイルを新規作成
$ vi /etc/profile.d/rbenv.sh
------
export RBENV_ROOT="/usr/local/rbenv"
    export PATH="$PATH:/usr/local/rbenv/bin:/usr/local/ruby-build/bin"
eval "$(rbenv init -)"
------

rbenvのsudo設定
$ visudo
------
~
# 末尾に追加
Defaults !secure_path
Defaults env_keep += "PATH RBENV_ROOT"
------

設定を反映
$ source /etc/profile.d/rbenv.sh

2.0.0-p247をインストール
$ rbenv-install 2.0.0-p247
$ rbenv global 2.0.0-p247
$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]

bundlerをインストール
$ gem install bundler --no-ri --no-rdoc
$ rbenv rehash

3. System Users

実行ユーザを作成します。

$ useradd git

4. GitLab shell

GitLab shellをインストール
$ cd /home/git
$ sudo -u git -H git clone https://github.com/gitlabhq/gitlab-shell
$ cd gitlab-shell

バージョンを1.7.1に固定
$ sudo -u git -H git checkout v1.7.1

設定ファイルを変更。
$ sudo -u git -H cp config.yml.example config.yml
$ vi config.yml
-----------------
~
# URLを変更
gitlab_url: "http://gitlab.examle.com/"
~
-----------------

インストールを実行
$ sudo -u git -H ruby ./bin/install

5. Database

公式では MySQL/PostgreSQL を適宜インストールすると書いていますので、MySQLをインストールします。

MySQL5.5をインストール
$ yum -y install mysql55 mysql55-server mysql55-devel
$ chkconfig mysqld on
$ service mysqld start

DB設定
$ mysql -uroot

ユーザ追加
mysql> GRANT ALL PRIVILEGES ON *.* TO gitlab@localhost IDENTIFIED BY 'gitlab';

データベース追加
mysql> create database gitlabhq_production default character set utf8;
mysql> quit

6. GitLab

Clone the Source

GitLabをダウンロード
$ cd /home/git
$ sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq gitlab

バージョンを6.1に固定
$ cd /home/git/gitlab
$ sudo -u git -H git checkout 6-1-stable

Configure it

GitLabの各種設定。

$ cd /home/git/gitlab
$ sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
$ vi config/gitlab.yml
-----------------
~
  ## GitLab settings
  gitlab:
    # ホスト名を変更
    host: gitlab.example.com
    port: 80
    https: false
~
-----------------

logとtmpディレクトリを作成
$ sudo chown -R git log/
$ sudo chown -R git tmp/
$ sudo chmod -R u+rwX  log/
$ sudo chmod -R u+rwX  tmp/

satellites用のディレクトリを作成
sudo -u git -H mkdir /home/git/gitlab-satellites


socketとpidのディレクトリを作成
sudo -u git -H mkdir tmp/pids/
sudo -u git -H mkdir tmp/sockets/
sudo chmod -R u+rwX  tmp/pids/
sudo chmod -R u+rwX  tmp/sockets/


アップロードディレクトリを作成
sudo -u git -H mkdir public/uploads
sudo chmod -R u+rwX  public/uploads

unicornの設定
デフォルト使用するため変更なし
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb

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

application.rbはデフォルトを使用するため変更なし

gitの設定
sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "gitlab@localhost"
sudo -u git -H git config --global core.autocrlf input

Configure GitLab DB settings

MySQL設定
$ sudo -u git cp config/database.yml.mysql config/database.yml
$ vi config/database.yml
-----------------
~
production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: gitlabhq_production
  pool: 10
  username: gitlab
  password: "gitlab"
  # host: localhost
  # socket: /tmp/mysql.sock
~
-----------------

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

Install Gems

$ cd /home/git/gitlab
$ gem install charlock_holmes --version '0.6.9.4'

MySQL依存のgem群をインストール
$ sudo -u git -H bundle install --deployment --without development test postgres aws

Initialize Database and Activate Advanced Features

初期化
$ sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production

Install Init Script

起動スクリプトを配置
$ cp lib/support/init.d/gitlab /etc/init.d/gitlab
$ chmod +x /etc/init.d/gitlab
$ chkconfig gitlab on

Set up logrotate

なぜかlogrotate用のファイルが見当たらなかったため最新版をダウンロードして配置します。
ファイル: https://github.com/gitlabhq/gitlabhq/blob/master/lib/support/logrotate/gitlab

$ cp gitlab /etc/logrotate.d/.

Check Application Status

チェック項目にrvmがあるので、rbenvよりrvmを使ったほうが良いのかもしれません。

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

System information
System:         Amazon Linux AMI release 2013.09
Current User:   git
Using RVM:      no
Ruby Version:   2.0.0p247
Gem Version:    2.0.3
Bundler Version:1.3.5
Rake Version:   10.1.0

GitLab information
Version:        6.1.0
Revision:       xxxxxxx
Directory:      /home/git/gitlab
DB Adapter:     mysql2
URL:            http://gitlab.example.com
HTTP Clone URL: http://gitlab.example.com/some-project.git
SSH Clone URL:  git@gitlab.example.com:some-project.git
Using LDAP:     no
Using Omniauth: no

GitLab Shell
Version:        1.7.1
Repositories:   /home/git/repositories/
Hooks:          /home/git/gitlab-shell/hooks/
Git:            /usr/bin/git

Start Your GitLab Instance

GitLabを起動します。

$ service gitlab start
Starting the GitLab Unicorn web server...
Starting the GitLab Sidekiq event dispatcher...
The GitLab Unicorn webserver with pid 21912 is running.
The GitLab Sidekiq job dispatcher is not running.

Sidekiqが起動していない旨のエラーが発生しました。 ぐぐったら数日前に改善されているバグもありましたが、このエラーはSidekiqを起動後のsleep時間以内に対象のプロセスが立ち上がっていないことから「not running」と判断していました。

start関数のsleep 2 の部分を 5 に変更することで解決しました。

$ vi /etc/init.d/gitlab
-----------------
~
    120 # Starts Unicorn and Sidekiq.
    121 start() {
    122   check_stale_pids
    123
    124   # Then check if the service is running. If it is: don't start again.
    125   if [ "$web_status" = "0" ]; then
    126     echo "The Unicorn web server already running with pid $wpid, not restarting."
    127   else
    128     echo "Starting the GitLab Unicorn web server..."
    129     # Remove old socket if it exists
    130     rm -f "$socket_path"/gitlab.socket 2>/dev/null
    131     # Start the webserver
    132     bundle exec unicorn_rails -D -c "$unicorn_conf" -E "$RAILS_ENV"
    133   fi
    134
    135   # If sidekiq is already running, don't start it again.
    136   if [ "$sidekiq_status" = "0" ]; then
    137     echo "The Sidekiq job dispatcher is already running with pid $spid, not restarting"
    138   else
    139     echo "Starting the GitLab Sidekiq event dispatcher..."
    140     RAILS_ENV=$RAILS_ENV bundle exec rake sidekiq:start
    141     # We are sleeping a bit here because sidekiq is slow at writing it's pid
            # 5秒に変える
    142     sleep 5
    143   fi
    144
    145   # Finally check the status to tell wether or not GitLab is running
    146   status
    147 }
~
-----------------

再起動
$ service gitlab restart
Starting the GitLab Unicorn web server...
Starting the GitLab Sidekiq event dispatcher...
The GitLab Unicorn webserver with pid 21912 is running.
The GitLab Sidekiq job dispatcher with pid 25391 is running.
GitLab and all it's components are up and running.

Double-check Application Status

多分起動スクリプトを修正したからだと思いますが、「Init script up-to-date?」で怒られています。でも動作に問題はないためスルーします。

$ sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
Checking Environment ...

Git configured for git user? ... yes
Has python2? ... yes
python2 is supported version? ... yes

Checking Environment ... Finished

Checking GitLab Shell ...

GitLab Shell version >= 1.7.1 ? ... OK (1.7.1)
Repo base directory exists? ... yes
Repo base directory is a symlink? ... no
Repo base owned by git:git? ... yes
Repo base access is drwxrws---? ... yes
update hook up-to-date? ... yes
update hooks in repos are links: ...
sample / examples ... ok

Checking GitLab Shell ... Finished

Checking Sidekiq ...

Running? ... yes

Checking Sidekiq ... Finished

Checking GitLab ...

Database config exists? ... yes
Database is SQLite ... no
All migrations up? ... no
yes
GitLab config exists? ... yes
GitLab config outdated? ... no
Log directory writable? ... yes
Tmp directory writable? ... yes
Init script exists? ... yes
Init script up-to-date? ... no
  Try fixing it:
  Redownload the init script
  For more information see:
  doc/install/installation.md in section "Install Init Script"
  Please fix the error above and rerun the checks.
projects have namespace: ...
sample / examples ... yes
Projects have satellites? ...
sample / examples ... yes
Redis version >= 2.0.0? ... yes
Your git bin path is "/usr/bin/git"
Git version >= 1.7.10 ? ... yes (1.8.3)

Checking GitLab ... Finished

7. Nginx

Installation

インストール
$ yum -y install nginx
$ chkconfig nginx on

Site Configuration

GitLab用の設定ファイルをコピー
$ cp lib/support/nginx/gitlab /etc/nginx/conf.d/gitlab.conf

設定ファイルを編集
$ vi /etc/nginx/conf.d/gitlab.conf
-----------------
server {
~
  # サーバ名を変更
  server_name gitlab.example.com;     # e.g., server_name source.example.com;
~
-----------------

Restart

$ service nginx start

ブラウザからhttp://gitlab.example.comにアクセスすると502エラーが発生しました。原因はUnixソケットの接続先に権限がないためパーミッションエラーとなっていることでした。homeのパーミッションを変更するのは気持ち悪いですが、とりあえず変更して解決しました。

エラー内容

2013/10/19 00:22:21 [crit] 24783#0: *12 connect() to unix:/home/git/gitlab/tmp/sockets/gitlab.socket failed (13: Permission denied) while connecting to upstrream, client: 192.168.100.20, server: gitlab.example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/git/gitlab/tmp/sockets/gitlab.socket:/", host: "gitlab.example.com"

パーミッション変更

$ chmod o+x /home/git

Done!

ひとまずこれでインストール完了です。

http://gitlab.example.com/にアクセスすればログイン画面が表示されます。

☆番外編

その他にSSLとBackup/Restoreも試してみました。

SSL

NginxでSSLオフロードします。

証明書の作成

$ cd /etc/nginx

鍵作成
$ openssl genrsa -out rsa-secret.key 2048
自己署名
$ openssl req -new -x509 -days 36500 -key rsa-secret.key -out request.csr
証明書発行
$ openssl x509 -in request.csr -out certificate.crt

Nginxの設定

設定
$ vi /etc/nginx/conf.d/gitlab.conf
-----------------
~
server {
  # 443に変更
  listen *:443 default_server;         # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
  server_name gitlab.example.com;     # e.g., server_name source.example.com;
  server_tokens off;     # don't show the version number, a security best practice
  root /home/git/gitlab/public;

  # ssl周りの設定を追加
  ssl                  on;
  ssl_certificate      /etc/nginx/certificate.crt;
  ssl_certificate_key  /etc/nginx/rsa-secret.key;
  ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers          HIGH:!ADH:!MD5;
~
-----------------

$ service nginx restart

GitLabの設定

$ vi /home/git/gitlab/config/gitlab.yml
-----------------
~
  gitlab:
    host: gitlab.example.com
    # SSL用に変更
    port: 443
    https: true
~
-----------------

GitLab shellの設定

$ vi /home/git/gitlab-shell/
-----------------
~
# URLをSSLに変更
gitlab_url: "https://gitlab.example.com/"

http_settings:
  # 自己署名証明書を許可
  self_signed_cert: true
~
-----------------

GitLab再起動

$ service gitlab restart

Done!

https://gitlab.example.com/

☆Backup/Restore

参考サイト のまま

Backup
$ bundle exec rake gitlab:backup:create RAILS_ENV=production

Restore
$ bundle exec rake RAILS_ENV=production gitlab:backup:restore BACKUP=tmp/backups/xxx.tar

ERROR

Git CloneでSSLエラー

git clone コマンドで Ruby の HTTP通信エラーが発生しました。調べてみるとGitLab shellが自己署名証明書に対応していないことが原因でした。既に問題は解決して自己署名証明書用のオプションが追加されていました。
※上記SSLのGitLab shellの設定参照

$ git clone git@gitlab.example.com:sample/examples.git
Initialized empty Git repository in /home/xxxxxx/xxxxxx/.git/
/usr/local/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
        from /usr/local/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
        from /usr/local/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
        from /usr/local/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `connect'
        from /usr/local/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
        from /usr/local/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/http.rb:851:in `start'
        from /home/git/gitlab-shell/lib/gitlab_net.rb:62:in `get'
        from /home/git/gitlab-shell/lib/gitlab_net.rb:17:in `allowed?'
        from /home/git/gitlab-shell/lib/gitlab_shell.rb:60:in `validate_access'
        from /home/git/gitlab-shell/lib/gitlab_shell.rb:23:in `exec'
        from /home/git/gitlab-shell/bin/gitlab-shell:16:in `<main>'