Ubuntu 12.04 に GitLab 5.2 と Redmine 2.3 を構築

今まで CentOS でやってきたけど、Ubuntu で構築したらとても楽チンだったのでメモ

事前準備

まずは GitLab を下記のQiitaの記事とほぼ同じように構築
Ubuntu 12.04.2 LTSにGitLab5.2をインストール - Qiita
ついでにサービスに必要なものも全部いれてしまう。

# apt-get update
# apt-get upgrade
# apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev \
libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl git-core \
openssh-server redis-server checkinstall libxml2-dev libxslt-dev \
libcurl4-openssl-dev libicu-dev postfix python nginx \
imagemagick  libmagickcore-dev libmagickwand-dev \
mysql-server mysql-client libmysqlclient-dev

デフォルトのrubyは削除しておく。共用のサーバーとか個人のマシンとかならrbenvとかrvmとかで共存させてもいいとおもう。

# apt-get remove ruby1.8
ruby のインストール

GitLab が ruby2.0 よりも1.9.3のがいいらしいので、そっちを入れる。

# mkdir /tmp/ruby && cd /tmp/ruby
# curl --progress http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p429.tar.gz | tar xz
# cd ruby-1.9.3-p429
# ./configure
# make
# make install
# gem install bundler --no-ri --no-rdoc

GitLab のインストール

Gitユーザーを追加
# adduser --disabled-login --gecos 'GitLab' git

git ユーザーで作業をする

# sudo su git
# cd /home/git
gitlab-shell をインストール
# git clone https://github.com/gitlabhq/gitlab-shell.git
# cd gitlab-shell/
# git checkout -b v1.5.0 v1.5.0
# cp config.yml{.example,}
# vim config.yml # ドメインやSSLを使うならそのへんの設定を変更する
# ./bin/install
# cd ../
gitlab をデプロイ
# cd /home/git
# git clone https://github.com/gitlabhq/gitlabhq.git gitlab
# cd /home/git/gitlab
# git checkout -b v5.2.1 v5.2.1
# cp config/gitlab.yml{.example,}
# vim config/gitlab.yml # いろいろ設定
# chown -R git log/ # ログや一時ディレクトリを作成して書き込み可能にしておく
# chown -R git tmp/
# mkdir tmp/pids/
# mkdir tmp/sockets/
# mkdir public/uploads
# chmod -R u+rwX  log/
# chmod -R u+rwX  tmp/
# chmod -R u+rwX  public/uploads
# cp config/puma.rb{.example,}
# git config --global user.name "GitLab" # Gitの設定
# git config --global user.email "gitlab@example.com"
MySQLの設定
# cp config/database.yml{.mysql,}
# mysql -u root -p

ここで $password は任意のものを入力し、上記のdatabase.ymlにそのパスワードを記述すること。

mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '$password';
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';
mysql> exit;

接続チェック

# mysql -u gitlab -p -D gitlabhq_production

gem から必要なものをインストール

# gem install charlock_holmes --version '0.6.9.4' --no-ri --no-rdoc
# bundle install --deployment --without development test postgres
# bundle exec rake gitlab:setup RAILS_ENV=production

ここのあたりで失敗する場合は、git ユーザーで gitlab-shell の ./bin/install を実行するのを忘れてたりするはず。
あとは起動チェック。

# bundle exec rake gitlab:env:info RAILS_ENV=production
# bundle exec rake sidekiq:start RAILS_ENV=production
起動スクリプトの設置
# su
# cp lib/support/init.d/gitlab /etc/init.d/gitlab
# chmod +x /etc/init.d/gitlab
# update-rc.d gitlab defaults 21

こんなかんじでGitlabの設置は完了。

Redmine

redmine は /home/ 直下じゃなくて /home/project/redmine に作ります。なんとなくユーザー領域にサービスがボコボコできるのいやなんです。

ソースのダウンロード
# mkdir -m 755 /home/project
# cd /home/project
# git clone https://github.com/redmine/redmine.git
# cd redmine
MySQL 設定
# cp config/database.yml{.example,}
# mysql -u root -p

ここで $password は任意のものを入力し、上記のdatabase.ymlにそのパスワードを記述すること。

mysql> CREATE USER 'redmine'@'localhost' IDENTIFIED BY '$password';
mysql> CREATE DATABASE IF NOT EXISTS `redmine` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `redmine`.* TO 'redmine'@'localhost';
mysql> exit;
各種設定

今回はApache2じゃなくてnginxで動かす。せっかくなのでgitlabで作ったpumaファイルをコピーして流用する。

# cp /home/git/gitlab/config/puma.rb config/puma.rb
# vim config/puma.rb # gitlab 用の設定を redmine 用に変更
# vim Gemfile # puma をインストールするために " gem "puma", '~> 2.0.1' " を追加
# cp -pR config/configuration.yml{.example,}
# vim config/configuration.yml # 各種設定
# bundle install --without development test postgresql sqlite
# rake generate_secret_token
redmine インストール
# RAILS_ENV=production rake db:migrate
# RAILS_ENV=production rake redmine:load_default_data
# mkdir tmp public/plugin_assets
# chown -R www-data:www-data /home/project
# chmod -R 755 files log tmp public/plugin_assets
起動スクリプト作成

これもせっかくなのでgitlabのを流用。ちょっと変更した点が多かったのでソースそのまま乗っけてみる。

#! /bin/bash

# Redmine
# Maintainer: @randx
# App Version: 2.3

### BEGIN INIT INFO
# Provides:          redmine
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Redmine ticket management service
# Description:       Redmine ticket management service
### END INIT INFO


APP_ROOT="/home/project/redmine"
APP_USER="www-data"
DAEMON_OPTS="-C $APP_ROOT/config/puma.rb"
PID_PATH="$APP_ROOT/tmp/pids"
SOCKET_PATH="$APP_ROOT/tmp/sockets"
WEB_SERVER_PID="$PID_PATH/puma.pid"
NAME="redmine"
DESC="Redmine service"

check_pid(){
  if [ -f $WEB_SERVER_PID ]; then
    PID=`cat $WEB_SERVER_PID`
    STATUS=`ps aux | grep $PID | grep -v grep | wc -l`
  else
    STATUS=0
    PID=0
  fi
}

execute() {
  sudo -u $APP_USER -H bash -l -c "$1"
}

start() {
  cd $APP_ROOT
  check_pid
  if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
    # Program is running, exit with error code 1.
    echo "Error! $DESC $NAME is currently running!"
    exit 1
  else
    if [ `whoami` = root ]; then
      execute "rm $SOCKET_PATH/redmine.socket"
      execute "RAILS_ENV=production bundle exec puma $DAEMON_OPTS"
      execute "mkdir -p $PID_PATH > /dev/null  2>&1 &"
      echo "$DESC started"
    fi
  fi
}

stop() {
  cd $APP_ROOT
  check_pid
  if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
    ## Program is running, stop it.
    kill -QUIT `cat $WEB_SERVER_PID`
    execute "mkdir -p $PID_PATH > /dev/null  2>&1 &"
    rm "$WEB_SERVER_PID" >> /dev/null
    echo "$DESC stopped"
  else
    ## Program is not running, exit with error.
    echo "Error! $DESC not started!"
    exit 1
  fi
}

restart() {
  cd $APP_ROOT
  check_pid
  if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
    echo "Restarting $DESC..."
    kill -USR2 `cat $WEB_SERVER_PID`
    execute "mkdir -p $PID_PATH > /dev/null  2>&1 &"
    if [ `whoami` = root ]; then
      execute "mkdir -p $PID_PATH > /dev/null  2>&1 &"
    fi
    echo "$DESC restarted."
  else
    echo "Error, $NAME not running!"
    exit 1
  fi
}

status() {
  cd $APP_ROOT
  check_pid
  if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
    echo "$DESC / Puma with PID $PID is running."
  else
    echo "$DESC is not running."
    exit 1
  fi
}

## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root"
    exit 1
fi

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload|force-reload)
        echo -n "Reloading $NAME configuration: "
        kill -HUP `cat $PID`
        echo "done."
        ;;
  status)
        status
        ;;
  *)
        echo "Usage: sudo service redmine {start|stop|restart|reload}" >&2
        exit 1
        ;;
esac

exit 0

これを /etc/init.d/redmine においておく。そして設定。

# chmod +x /etc/init.d/redmine
# update-rc.d redmine defaults 21

とりあえずこんな感じでredmineも動くはず

Nginx の設定

あとは適当にNginxの設定。gitlab と redmine をバーチャルホストで動かす。SSL利用するのでkeyとかcrtとか設定したけど、必要なければそのへんはコメントアウトでいいかも。
# /etc/nginx/sites-available/gitlab

# GITLAB
# Maintainer: @randx
# App Version: 5.0

upstream gitlab {
  server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}

server {
  listen *:443 default_server ssl;         # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
  ssl on;
  ssl_certificate /etc/nginx/ssl/server.crt;
  ssl_certificate_key /etc/nginx/ssl/server.key;
  server_name gitlab.hogehoge.com;     # e.g., server_name source.example.com;
  root /home/git/gitlab/public;

  # individual nginx logs for this gitlab vhost
  access_log  /var/log/nginx/gitlab_access.log;
  error_log   /var/log/nginx/gitlab_error.log;

  location / {
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (gitlab unicorn)
  location @gitlab {
    proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://gitlab;
  }
}

これと redmine
/etc/nginx/sites-available/redmine

# Redmine
# Maintainer: @randx
# App Version: 2.3

upstream redmine {
  server unix:/home/project/redmine/tmp/sockets/redmine.socket;
}

server {
  listen *:443 ssl;         # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
  ssl on;
  ssl_certificate /etc/nginx/ssl/server.crt;
  ssl_certificate_key /etc/nginx/ssl/server.key;
  server_name redmine.hogehoge.com;     # e.g., server_name source.example.com;
  root /home/project/redmine/public;

  # individual nginx logs for this gitlab vhost
  access_log  /var/log/nginx/redmine_access.log;
  error_log   /var/log/nginx/redmine_error.log;

  location / {
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @redmine;
  }

  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (gitlab unicorn)
  location @redmine {
    proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://redmine;
  }
}

んで、起動するときは

# service gitlab restart
# service redmine restart
# service nginx restart

とかやる。

メールの設定

せっかくメールと連携できるサービスなので、postfix(送信のみ)を設定しておく。
まずデフォルトでsendmailがはいっていたりするので、それを削除。

# service sendmail stop
# apt-get remove sendmail

んで、postfix の設定ファイルを少しいじる

# vim /etc/postfix/main.cf

このへんはTSL使うとか、ドメイン変えるとかを変更。
そして起動。

# service postfix start

送信をテストする

# mail unko@hoge.com

メール作成を完了するには、最後の行に「.」だけをうってエンターキーを押す。うまく送信できない場合は /var/log/mail.log あたりを参考に修正していく。

こんなかんじでひととおりできるはず。