バージョン管理共有サービスGitHubのクローズド版、GitLabを導入してみる。
前提
・環境
CentOS6.4+unicorn+Apache+rvm
・公式ガイド
https://github.com/gitlabhq/gitlabhq/blob/5-0-stable/doc/install/installation.md
・目標
GitLabをインストールし、サブドメイン運用、動作確認まで。
内容
1,yumで各種パッケージをインストール
epelリポジトリを追加する。wgetでリポジトリをとってきてrpmでインストール。
$ wget http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm $ sudo rpm -ivh epel-release-6-8.noarch.rpm
epelリポジトリを有効にし、yumで各種パッケージをインストール。
$ sudo vi /etc/yum.repos.d/epel.repo 【#enabled=0に変更】 $ sudo yum update $ sudo yum -y groupinstall 'Development Tools' 'Additional Development' --enablerepo=epel $ sudo yum install libxslt-devel libyaml-devel libxml2-devel gdbm-devel libffi-devel zlib zlib-devel openssl-devel libyaml-devel readline readline-devel curl-devel openssl-devel pcre-devel git memcached-devel valgrind-devel mysql-devel ImageMagick-devel ImageMagick libicu libicu-devel libffi-devel make bzip2 autoconf automake libtool python-pip bison iconv-devel redis --enablerepo=epel
pythonもインストール。
$ sudo yum install python
pythonのバージョン確認
$ python --version Python 2.6.6
2.5+なのでOK。
2,Gitlab用のgitユーザーを作成する。
sudo権限もつけておく。
$ sudo useradd -c "GitLab" git $ sudo passwd git $sudo usermod -G wheel git
3,Rubyをインストール。
$ ruby -v ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
既に入っているのでOK。
4,GitLab Shellをインストール
GitLab用に作られたリポジトリ管理ソフトウェアだそうな。
$ sudo su git $ cd /home/git $ git clone https://github.com/gitlabhq/gitlab-shell.git $ cd gitlab-shell
v5.0用のブランチを作成し移動。
$ git checkout -b v1.1.0
Gitlab Shellの設定ファイルを編集。
$ cp config.yml.example config.yml $ vi config.yml
下記のみ編集。
gitlab_url: "http://gitlab.example.net/"
5,データベースの設定
私の環境はMySQLを既にインストール済みです。
$ mysql -u root -p mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'パスワード'; mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`; mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost'; mysql> \q
データベースに接続できるか確認
$ sudo -u git -H mysql -u gitlab -p -D gitlabhq_production
さきほど作ったパスワードを入力してログインチェック。
6,GitLabのインストール
GitLabをユーザーgitのホームディレクトリにインストールする。
$ cd /home/git
GitLabリポジトリをクローンし、5.0バージョンへ。
$ git clone https://github.com/gitlabhq/gitlabhq.git gitlab $ cd /home/git/gitlab $ sudo -u git -H git checkout 5-0-stable Branch 5-0-stable set up to track remote branch 5-0-stable from origin. Switched to a new branch '5-0-stable'
設定
$ cp config/gitlab.yml.example config/gitlab.yml $ vi config/gitlab.yml
## GitLab settings gitlab: ## Web server settings host: gitlab.example.net port: 80 https: false # Uncomment and customize to run in non-root path # Note that ENV['RAILS_RELATIVE_URL_ROOT'] in config/unicorn.rb may need to be changed #relative_url_root: /gitlab # Uncomment and customize if you can't use the default user to run GitLab (default: 'git') # user: git ## Email settings # Email address used in the "From" field in mails sent by GitLab email_from: [email protected] # Email address of your support contact (default: same as email_from) support_email: [email protected] … ## GitLab Shell settings gitlab_shell: # REPOS_PATH MUST NOT BE A SYMLINK!!! repos_path: /home/git/repositories/ hooks_path: /home/git/gitlab-shell/hooks/ # Git over HTTP upload_pack: true receive_pack: true # If you use non-standard ssh port you need to specify it ssh_port: 10022(sshのポート。もしデフォルトから変えているなら記入)
オーナーとパーミッションの変更
$ sudo chown -R git log/ $ sudo chown -R git tmp/ $ sudo chmod -R u+rwX log/ $ sudo chmod -R u+rwX tmp/
satellitesディレクトリを作成
$ mkdir /home/git/gitlab-satellites
pids用のディレクトリを作り、GitLabが書き込めるように
$ mkdir tmp/pids/ $ sudo chmod -R u+rwX tmp/pids/
GitLab用データベースの設定
$ cp config/database.yml.mysql config/database.yml $ vi config/database.yml
production: adapter: mysql2 encoding: utf8 reconnect: false database: gitlabhq_production pool: 5 username: gitlab password: "データベースのパスワード" host: localhost # socket: /tmp/mysql.sock
7,unicornの設定
$ cp ~/gitlab/config/unicorn.rb.example ~/gitlab/config/unicorn.rb $ vi config/unicorn.rb
unicornはポート8080をlistenする。
… listen "127.0.0.1:8080", :tcp_nopush => true …
8,Gem設定
$ cd /home/git/gitlab $ gem install charlock_holmes --version '0.6.9'
今回データベースはMySQLなので下記を実行
$ bundle install --deployment --without development test postgres
8,データベースを初期化
$ bundle exec rake gitlab:setup RAILS_ENV=production … [email protected] password......5iveL!fe
上記を実行すると、LoginIDとpasswordを取得できるのでメモしておく。
8,起動スクリプトのインストール
5.0に対応した起動スクリプトを取得する。
$ sudo curl --output /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/5-0-stable/init.d/gitlab
パーミッションを変更し、自動起動設定。
$ sudo chmod +x /etc/init.d/gitlab $ sudo chkconfig --add gitlab $ chkconfig --list gitlab gitlab 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ランレベル 2 ~ 5 が on になっているのでOK。
起動スクリプトにrvmの環境変数を加える。
$ sudo vi /etc/init.d/gitlab 下記を追記 # # set rvm environment valiables. # export PATH=/home/git/.rvm/gems/ruby-1.9.3-p392/bin:/home/git/.rvm/gems/[email protected]/bin:/home/git/.rvm/rubies/ruby-1.9.3-p392/bin:/home/git/.rvm/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/git/.rvm/bin export GEM_HOME=/home/git/.rvm/gems/ruby-1.9.3-p392 export GEM_PATH=/home/git/.rvm/gems/ruby-1.9.3-p392:/home/git/.rvm/gems/[email protected] export MY_RUBY_HOME=/home/git/.rvm/rubies/ruby-1.9.3-p392 export IRBRC=/home/git/.rvm/rubies/ruby-1.9.3-p392/.irbrc
9,アプリケーションの状態の確認
GitLabのインストール環境は下記のコマンドで確認する。
$ cd ~/gitlab $ bundle exec rake gitlab:env:info RAILS_ENV=production System information System: CentOS release 6.4 (Final) Current User: git Using RVM: yes RVM Version: 1.19.1 Ruby Version: 1.9.3p392 Gem Version: 1.8.25 Bundler Version:1.3.5 Rake Version: 10.0.3 GitLab information Version: 5.0.1 Revision: 907a8f4 Directory: /home/git/gitlab DB Adapter: mysql2 URL: http://gitlab.example.net HTTP Clone URL: http://gitlab.example.net/some-project.git SSH Clone URL: ssh://[email protected]:10022/some-project.git Using LDAP: no Using Omniauth: no GitLab Shell Version: 1.1.0 Repositories: /home/git/repositories/ Hooks: /home/git/gitlab-shell/hooks/ Git: /usr/bin/git
その他、GitLabの設定でミスがないか下記のコマンドで確認
$ bundle exec rake gitlab:check RAILS_ENV=production
全ての項目が緑にならばOK。エラーがでた場合は指示通りに対応する。
ただし、下記のようなエラーは、バージョンが古いため発生するものなので、無視する。(今回インストールしているバージョンは 5.0。最新は5.3)
GitLab Shell version? ... FAIL. Please update gitlab-shell to v1.1.0 … Init script up-to-date? ... no
10,GitLab起動
GitLabを起動する。
$ sudo service gitlab start
11,Apacheの設定
まずは Apache実行ユーザ(ここでは apache とする)が /home/git にアクセスできるよう、 git グループに追加します。
$ sudo usermod -G git apache
次に /home/git にグループからのアクセスを許可するようパーミッションを設定します。
$ chmod g+rX /home/git
次に、GitLab 用のバーチャルホスト定義ファイルを設定。
今回はサブドメインで運用。
ここでは、バーチャルホストの定義ファイルをvhost.confとする。
$ sudo vi /etc/httpd/conf.d/vhost.conf
以下を記述
<VirtualHost *:80> ServerName gitlab.example.net DocumentRoot /home/git/gitlab/public ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ </VirtualHost>
apache再起動
$ sudo service httpd graceful
12,SSH接続の際のrubyの問題を解決
今回rvmで運用しているので、sshで直接コマンドを実行する場合。どのrcスクリプトも読まれない。つまり、.bashrcも.zshrcも.profileもなにも読まれない。もちろん、/etc/profile.dの中にあるスクリプトも読み込まれない。 というわけで下記を設定。
$ sudo vi /etc/ssh/sshd_config $ env | grep -E "^(GEM_HOME|PATH|RUBY_VERSION|MY_RUBY_HOME|GEM_PATH)=" > ~/.ssh/environment $ sudo /etc/init.d/sshd restart
13,GitLabアクセス
設定したURLにアクセスし、ログイン画面が表示されたら成功。
8で取得したLoginIDとpasswordでログインする。
14,問題対応
私の環境だけかもしれないが、公開鍵を設定したり、ブラウザ上から新しいリポジトリを作成した際、その内容が反映されない。sidekiqの動作不良だと思われる。
そういう場合は下記のURLの対応法でOK。
https://github.com/hiroponz/gitlab-recipes/commit/7f8dd74e119edf5ffa112bef86c729eb1226b3d2
以上。