GitLabをCentOS6.3上に構築してみよう
コードレビューするのに何かよさげな仕組みは無いかなと調べるほど「Githubいいわーまじいいわー」的な記事が目につきました。そんなによいのなら!ということでGitLabを建てて試してみようかな、となったわけです。(Githubを素直に使えなかった理由はお察しください)
このメモはGitLab構築時のメモです。参考情報はここhttps://github.com/gitlabhq/gitlabhq/blob/5-0-stable/doc/install/installation.mdとここhttps://github.com/gitlabhq/gitlab-recipes/blob/master/install/CentOS_6.md
なお、参考にしたインストールガイドが「素のCentOS6.3上にちまちま構築」を前提としているため、細かい手順まで書いてます。ご了承をば。
前準備として、基本的なソフトやらサービスを追加しよう
まずはEPELリポジトリを追加します。要root。
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
続いてGitとGitolite(Gitで共有リポジトリのユーザー管理やアクセス管理を行うためのツール)に必要なツール類をインストールします。要root。
# yum -y groupinstall 'Development Tools' ### 'Additional Development' # yum -y install vim-enhanced httpd readline readline-devel ncurses-devel gdbm-devel glibc-devel \ tcl-devel openssl-devel curl-devel expat-devel db4-devel byacc \ sqlite-devel gcc-c++ libyaml libyaml-devel libffi libffi-devel \ libxml2 libxml2-devel libxslt libxslt-devel libicu libicu-devel \ system-config-firewall-tui python-devel redis sudo mysql-server wget \ mysql-devel crontabs logwatch logrotate sendmail-cf qtwebkit qtwebkit-devel \ perl-Time-HiRes
インストールガイドによると、RHEL6.3において上記 yum -y install が実はインストールできてなかった!ということがあったそうです。なので、念のため2回実行して
"No package XXX available"
を確認しておくとよさげです。
CentOS自体もアップデートしておきましょう。要root。
# yum -y update
Redisの設定
Redisとはインメモリデータベースの1つで、データ構造サーバーとも呼ばれるようです。OS起動時に実行されるように設定しましょう。要root。
# chkconfig redis on # service redis start
MySQLの設定
同じようにMySQLについても、OS起動時に実行されるように設定しましょう。要root。
# chkconfig mysqld on # service mysqld start
以下のコマンドもご一緒に。全て"Yes"で、よりSecureにMySQLを使えます。
$ /usr/bin/mysql_secure_installation
httpdの設定
GitLabはApacheを使用しているそうです。こいつも起動設定。
$ chkconfig httpd on
ポートの設定は /etc/httpd/conf.d/gitlab.conf を作成しましょう。以下のオリジナル(3000)をいい感じに設定しましょう。
<VirtualHost *:80> ServerName git.example.org ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPreserveHost On ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost>
他のWebサイトを同じシステム内で動作させている場合は、/etc/httpd/conf/httpd.conf にて以下を記述しましょう。
NameVirtualHost *:80
Firewallの設定
iptablesを設定して、http、https、sshを通します。ここでは /etc/sysconfig/iptables に以下の様に記述します。
# Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
emailの設定
以下ファイルを編集します。
$ cd /etc/mail $ vim /etc/mail/sendmail.mc
define(`SMART_HOST', `smtp.example.com')dnl
以下行をコメントアウトします。(行頭にdnlを追記)
EXPOSED_USER(`root')dnl
これらの設定を有効化します。
$ make $ chkconfig sendmail on
再起動
ここまでで基本的な設定は完了したので、一度再起動しましょう。
Ruby
次にRubyを入れます。要root。
# mkdir /tmp/ruby && cd /tmp/ruby # wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p327.tar.gz # tar xfvz ruby-1.9.3-p327.tar.gz # cd ruby-1.9.3-p327 # ./configure # make # make install
続いてBundler Gem(rubygemsのラッパーで、Rails3のgem管理に採用されていたりするようです)を入れます。要root。
# gem install bundler
システムユーザの設定
GitとGitolite用のユーザを作ります。要root。
# adduser \ --system \ --shell /bin/bash \ --comment 'Git Version Control' \ --create-home \ --home-dir /home/git \ git # adduser \ --shell /bin/bash \ --comment 'GitLab user' \ --create-home \ --home-dir /home/gitlab \ gitlab # usermod -a -G git gitlab
gitlabユーザには後でパスワードが必要になるので、先に設定してしまいます。
# passwd gitlab # please choose a good password :)
SSH鍵を作ります。要root。
# sudo -u gitlab -H ssh-keygen -q -N '' -t rsa -f /home/gitlab/.ssh/id_rsa
全メールを転送する
システム全体のロギングをメール転送するように設定します。要root。
# echo adminlogs@example.com > /root/.forward # chown root /root/.forward # chmod 600 /root/.forward # restorecon /root/.forward # echo adminlogs@example.com > /home/gitlab/.forward # chown gitlab /home/gitlab/.forward # chmod 600 /home/gitlab/.forward # restorecon /home/gitlab/.forward
Gitoliteの設定
GitoliteソースコードのGitlabフォークをクローンします。要root。git設定はできてる体で以下コマンド。
(インストールドキュメントでは git clone -b gl-v320 https://github.com/gitlabhq/gitolite.git〜 という書き方だったんだけども、自分の環境ではうまく行かなかったのでちょっと修正)
# cd /home/git # git clone -b gl-v320 git@github.com:gitlabhq/gitolite.git /home/git/gitolite
GitoliteをGitLabのadminとして設定
GitoliteをGitLabのadminとして設定します。要root。
# # Add Gitolite scripts to $PATH # sudo -u git -H mkdir /home/git/bin # sudo -u git -H sh -c 'printf "%b\n%b\n" "PATH=\$PATH:/home/git/bin" "export PATH" >> /home/git/.profile' # sudo -u git -H sh -c 'gitolite/install -ln /home/git/bin' # # Copy the gitlab user's (public) SSH key ... # cp /home/gitlab/.ssh/id_rsa.pub /home/git/gitlab.pub # chmod 0444 /home/git/gitlab.pub # # ... and use it as the admin key for the Gitolite setup # sudo -u git -H sh -c "PATH=/home/git/bin:$PATH; gitolite setup -pk /home/git/gitlab.pub"
各ディレクトリのパーミッションを、設定ディレクトリ用として設定します。
# # Make sure the Gitolite config dir is owned by git # chmod 750 /home/git/.gitolite/ # chown -R git:git /home/git/.gitolite/
続いて各ディレクトリのパーミッションを、リポジトリ用として設定します。
# # Make sure the repositories dir is owned by git and it stays that way # chmod -R ug+rwXs,o-rwx /home/git/repositories/ # chown -R git:git /home/git/repositories/ # # Make sure the gitlab user can access the required directories # chmod g+x /home/git
GitアカウントをGitLabユーザとして教えてあげます。要root。
# su - gitlab (以下は logged in as **gitlab**) $ ssh git@localhost ("Are you sure you want to continue connecting?(yes/no)"にyesと答えると以下のような出力が得られます) PTY allocation request failed on channel 0 hello gitlab, this is git@localhost running gitolite3 v3.2-gitlab-patched-0-g2d29cf7-dt on git 1.7.1
ここまでの設定が成功しているかどうか、以下で確認します。logged in as **gitlab**
# Clone the admin repo so SSH adds localhost to known_hosts ... # ... and to be sure your users have access to Gitolite $ git clone git@localhost:gitolite-admin.git /tmp/gitolite-admin # If it succeeded without errors you can remove the cloned repo $ rm -rf /tmp/gitolite-admin
このCloneが成功しない場合、以降のインストール作業を継続してはだめです。トラブルシューティングhttps://github.com/gitlabhq/gitlab-public-wiki/wiki/Trouble-Shooting-Guideとにらめっこして、解決してから進みましょう。
Gitlabのインストール
いよいよGitlabをインストールします。gitlabユーザのホームディレクトリにインストールしましょう。logged in as **gitlab**
$ cd /home/gitlab (ソースをクローンします。インストールガイドと比べて一部変更。) $ git clone git@github.com:gitlabhq/gitlabhq.git gitlab (チェックアウトしましょう) $ cd /home/gitlab/gitlab $ git checkout 4-0-stable
Gitalabの設定
Gitlabの設定ファイルサンプルをコピーします。
$ cp /home/gitlab/gitlab/config/gitlab.yml{.example,}
サンプルファイル内で"localhost" となっている箇所をGitlabを動かすドメイン名に変更する必要があります。また他の設定内容とも一致しているかどうかも確認しましょう。
$ vim /home/gitlab/gitlab/config/gitlab.yml
次にunicorn.rb(=RubyのためのHTTPサーバ)の設定ファイルサンプルをコピーします。
$ cp /home/gitlab/gitlab/config/unicorn.rb{.example,}
サンプルファイル内でループバックのlistenポート設定がありますが、ここまでの設定で3000に変更してるはずなので、変更しましょう。
$ vim /home/gitlab/gitlab/config/unicorn.rb (以下を修正) listen "127.0.0.1:3000" # listen to port 3000 on the loopback interface
Gitlab DBの設定
Gitlabで使用するDB(ここではMySQL)を設定します。
$ cp /home/gitlab/gitlab/config/database.yml{.mysql,}
次にDBの設定ファイルを編集し、username/passwordを正しいものにします。
$ vim /home/gitlab/gitlab/config/database.yml (以下の"supersecret "を修正) production: adapter: mysql2 encoding: utf8 reconnect: false database: gitlabhq_production pool: 5 username: gitlab password: supersecret # host: localhost # socket: /tmp/mysql.sock
Gemのインストール
Gemをインストールします。要root。
# cd /home/gitlab/gitlab # gem install charlock_holmes --version '0.6.9'
gitlabに戻ります。
$ cd /home/gitlab/gitlab (For mysql db) $ bundle install --deployment --without development test postgres
Gitの設定
GitLabはGitoliteへの変更をコミットしてプッシュすることができる必要があります。そのためにusernameとemailが必要です。オススメはconfig/gitlab.yml内で設定したものと同じにしておくようなので、それに合わせます。
gitlabにて。
$ git config --global user.name "GitLab" $ git config --global user.email "gitlab@localhost" (以下、要root。) # cd /home/gitlab/gitlab # cp ./lib/hooks/post-receive /home/git/.gitolite/hooks/common/post-receive # chown git:git /home/git/.gitolite/hooks/common/post-receive
DB初期化とAdvanced機能の起動
gitlabに戻ります。
$ cd /home/gitlab/gitlab $ bundle exec rake gitlab:app:setup RAILS_ENV=production
mysqlのrootパスワードが求められるので入力すると、DBとuserが定義されます。
Init Scriptのインストール
init scriptを落としてきます。要root。
# curl https://raw.github.com/gitlabhq/gitlab-recipes/master/init.d/gitlab-centos > /etc/init.d/gitlab # chmod +x /etc/init.d/gitlab # chkconfig --add gitlab (起動時実行) # chkconfig gitlab on (起動してみましょう) # /etc/init.d/gitlab start
失敗しました。以下コマンドでステータスがチェックできます。
$ bundle exec rake gitlab:env:info RAILS_ENV=production $ bundle exec rake gitlab:check RAILS_ENV=production
修正したのは以下の通り。
- 取得した/etc/init.d/gitlabの冒頭に、このファイルは不正なうんぬんなエラー部分があったので削除。
- "NAME=git"を"NAME=gitlab"に
- 起動のなかで、"Starting sidekiq: rake aborted! Don't know how to build task 'sidekiq:start'"と言われたのでsidekiqをインストール。
- sidekiqをインストールするのにRedisが必要なのでインストール。
Done!
起動が成功したら、ブラウザからアクセスしてみましょう。adminアカウントが作成されているので、以下でログインしてみてください。
admin@local.host 5iveL!fe
長かったー!Enjoy!