redmine を apache, mysql, unicorn, rvm を用いて動かす

こんな感じで

  • データベースは mysql
  • ruby は rvm で導入する
  • web サーバは apache + unicorn
    • http://example.jp でバーチャルホスト
  • redmine 専用ユーザで実行

以下の手順の前提条件

  • redmine の実行ユーザ名は redmine
  • MySQL について
    • root ユーザのパスワードは P@ssword
    • 実際には redmine が接続する DB ユーザ redmine を作成する。パスワードは $ecret
    • ドメイン名は http://example.jp
    • redmine へセッション等のためのシークレットフェーズを設定する必要がある
      • シークレットフェーズは VeryVeryVeryVeryVerySecretPhase に設定している

redmine ユーザの作成

redmine は、このユーザで実行させる

[root@www20349u ~]# useradd -s /sbin/nologin redmine

yum で色々入れる

インストール
[root@www20349u ~]# # とりあえず更新
[root@www20349u ~]# yum -y update
[root@www20349u ~]# # Apache
[root@www20349u ~]# yum -y install httpd
[root@www20349u ~]# # MySQL サーバ
[root@www20349u ~]# yum -y install mysql-server
[root@www20349u ~]# # 以下は Ruby, Rails, Rvm のあたりで必要になる
[root@www20349u ~]# yum -y install zlib-devel openssl-devel mysql-devel readline-devel

Apache の設定

  • http://example.jp のバーチャルホストを設定
  • ポート番号 80 番でリスンする。
    • iptables に穴をあける必要もある
  • 上記でリクエストが来たら、 http://localhost:3100 に処理を委譲する
プロキシの設定

/etc/httpd/conf.d/redmine.conf

<VirtualHost *:80>
  ServerName example.jp

  ProxyPass / http://localhost:3100/
  ProxyPassReverse / http://localhost:3100/
</VirtualHost>
起動スクリプトを有効にする、更に起動。
[root@www20349u ~]# chkconfig httpd on
[root@www20349u ~]# service httpd start

iptables の設定, 80番ポートを通過させる

[root@www20349u ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT

MySQL

ローカルのみ接続可能にする。文字コードUTF-8 にする。
  • mysqld セクションに追加
    • default-character-set=utf8
    • skip-character-set-client-handshake
    • skip-networking
  • mysql セクションを追加
  • mysql セクションに追加
    • default-character-set=utf8

/etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

default-character-set=utf8
skip-character-set-client-handshake
skip-networking

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql]
default-character-set=utf8
chkconfig 設定, mysqld 起動
[root@www20349u ~]# chkconfig mysqld on
[root@www20349u ~]# service mysqld start
mysql root ユーザのパスワード設定
mysqladmin -u root -h localhost password "P@ssword"
redmine データベース作成, redmine ユーザ作成
[root@www20349u ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database redmine;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on redmine.* to redmine@localhost identified by "$ecret";
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

git のインストール

[root@www20349u ~]# # git インストールのために、 rpmforge リポジトリの設定を行う
[root@www20349u ~]# #
[root@www20349u ~]# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
[root@www20349u ~]# rpm -ihv rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
[root@www20349u ~]# vi /etc/yum.repos.d/rpmforge.repo

/etc/yum.repos.d/rpmforge.repo

enabled = 1

enabled = 0
[root@www20349u ~]# yum -y --enablerepo=rpmforge install git
[root@www20349u ~]# git --version
git version 1.7.3.4
[root@www20349u ~]#

redmine ユーザでログインして rvm をインストール

[root@www20349u ~]# # redmine ユーザでログインする
[root@www20349u ~]# su - redmine -s /bin/bash
[redmine@www20349u ~]$ # rvm をインストールする
[redmine@www20349u ~]$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
[redmine@www20349u ~]$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
[redmine@www20349u ~]$ source ~/.bashrc
[redmine@www20349u ~]$ rvm -v

rvm 1.2.4 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]

[redmine@www20349u ~]$ # Ruby1.8.7をインストールする
[redmine@www20349u ~]$ rvm install 1.8.7
[redmine@www20349u ~]$ rvm 1.8.7
[redmine@www20349u ~]$ # gemset "redmine" を作成し、それをデフォルトにする
[redmine@www20349u ~]$ rvm gemset create redmine
[redmine@www20349u ~]$ rvm 1.8.7@redmine --default
[redmine@www20349u ~]$ # 必要な gem をインストールする
[redmine@www20349u ~]$ gem install mysql   --no-ri --no-rdoc
[redmine@www20349u ~]$ gem install rack    --no-ri --no-rdoc -v=1.0.1
[redmine@www20349u ~]$ gem install unicorn --no-ri --no-rdoc
[redmine@www20349u ~]$ gem install i18n    --no-ri --no-rdoc -v=0.4.2
[redmine@www20349u ~]$ exit

redmine ソースコードをダウンロードして配置

[root@www20349u ~]# su - redmine -s /bin/bash
[redmine@www20349u ~]$ wget http://rubyforge.org/frs/download.php/73900/redmine-1.1.0.tar.gz
[redmine@www20349u ~]$ tar zvxf redmine-1.1.0.tar.gz
[redmine@www20349u ~]$ ln -s redmine-1.1.0 redmine
[redmine@www20349u ~]$ cd redmine
[redmine@www20349u redmine]$ # データベースとメールの設定を、サンプルファイルを元に作成する
[redmine@www20349u redmine]$ cp config/database.yml.example config/database.yml
[redmine@www20349u redmine]$ #
[redmine@www20349u redmine]$ # redmine のデータベース接続設定をする
[redmine@www20349u redmine]$ vi config/database.yml

config/database.yml

# MySQL (default setup).

production:
  adapter: mysql
  database: redmine
  host: localhost
  username: redmine
  password: $ecret
  encoding: utf8
(以下略)
[redmine@www20349u redmine]$ #
[redmine@www20349u redmine]$ # セッション文字列を設定する必要がある
[redmine@www20349u redmine]$ vi config/environment.rb

config/environment.rb

Rails::Initializer.run do |config|
  # Settings in config/environments/* take precedence those specified here
  ... (途中省略

  config.action_controller.session = { :key => "_myapp_session", :secret => "VeryVeryVeryVeryVerySecretPhase" }
end
[redmine@www20349u redmine]$
[redmine@www20349u redmine]$ # データベースマイグレート実行
[redmine@www20349u redmine]$ rake db:migrate RAILS_ENV=production
 (省略)
==  ChangeProjectsNameLimit: migrated (0.0066s) ===============================

==  ChangeProjectsIdentifierLimit: migrating ==================================
-- change_column(:projects, :identifier, :string, {:limit=>nil})
   -> 0.0070s
==  ChangeProjectsIdentifierLimit: migrated (0.0071s) =========================

[redmine@www20349u ~]$ exit
[root@www20349u ~]#

成功した、とりまログアウト。

unicornredmine を起動するため頑張る

unicorn のコンフィグを書く
  • ワーカプロセスは2つ
  • fork 後に実行ユーザを redmine へ変更する
[root@www20349u ~]# vi /home/redmine/redmine/config/unicorn.conf

/home/redmine/redmine/config/unicorn.conf

$unicorn_user = "redmine"
$unicorn_group = "redmine"

worker_processes 2
working_directory '/home/redmine/redmine'

listen      "127.0.0.1:3100", :tcp_nopush => true
timeout     30

pid         "tmp/pids/unicorn.pid"
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"

preload_app  true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection

  begin
    uid, gid = Process.euid, Process.egid
    user, group = $unicorn_user, $unicorn_group
    target_uid = Etc.getpwnam(user).uid
    target_gid = Etc.getgrnam(group).gid
    worker.tmp.chown(target_uid, target_gid)
    if uid != target_uid or gid != target_gid
      Process.initgroups(user, target_gid)
      Process::GID.change_privilege(target_gid)
      Process::UID.change_privilege(target_uid)
    end
  rescue
    if RAILS_ENV = "development"
      STDERR.puts "could not change user, oh well"
    else
      raise e
    end
  end
end

起動スクリプト

起動スクリプトを書く
[root@www20349u ~]# vi /etc/init.d/unicorn_rails_redmine

/etc/init.d/unicorn_rails_redmine

#!/bin/bash
#
# unicorn_rails_redmine Startup script for unicorn.
#
# chkconfig: - 85 15
# description: redmine on unicorn start/stop script.
#

#
# set rvm environment valiables.
#
export PATH=/home/redmine/.rvm/gems/ruby-1.8.7-p330@redmine/bin:/home/redmine/.rvm/gems/ruby-1.8.7-p330@global/bin:/home/redmine/.rvm/rubies/ruby-1.8.7-p330/bin:/home/redmine/.rvm/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/redmine/bin
export GEM_HOME=/home/redmine/.rvm/gems/ruby-1.8.7-p330@redmine
export GEM_PATH=/home/redmine/.rvm/gems/ruby-1.8.7-p330@redmine:/home/redmine/.rvm/gems/ruby-1.8.7-p330@global
export BUNDLE_PATH=
export MY_RUBY_HOME=/home/redmine/.rvm/rubies/ruby-1.8.7-p330
export IRBRC=/home/redmine/.rvm/rubies/ruby-1.8.7-p330/.irbrc

set -u
set -e

APP_NAME=redmine
APP_ROOT="/home/redmine/$APP_NAME"
CNF="$APP_ROOT/config/unicorn.conf"
PID="$APP_ROOT/tmp/pids/unicorn.pid"
ENV=production

UNICORN_OPTS="-D -E $ENV -c $CNF"

old_pid="$PID.oldbin"

cd $APP_ROOT || exit 1

sig () {
        test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
        test -s $old_pid && kill -$1 `cat $old_pid`
}

case ${1-help} in
start)
        sig 0 && echo >&2 "Already running" && exit 0
        cd $APP_ROOT ; unicorn_rails $UNICORN_OPTS
        ;;
stop)
        sig QUIT && exit 0
        echo >&2 "Not running"
        ;;
force-stop)
        sig TERM && exit 0
        echo >&2 "Not running"
        ;;
restart|reload)
        sig HUP && echo reloaded OK && exit 0
        echo >&2 "Couldn't reload, starting instead"
        unicorn_rails $UNICORN_OPTS
        ;;
upgrade)
        sig USR2 && exit 0
        echo >&2 "Couldn't upgrade, starting instead"
        unicorn_rails $UNICORN_OPTS
        ;;
rotate)
        sig USR1 && echo rotated logs OK && exit 0
        echo >&2 "Couldn't rotate logs" && exit 1
        ;;
*)
        echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
        exit 1
        ;;
esac
起動スクリプトを有効にする、更に起動。
[root@www20349u ~]# chmod +x /etc/init.d/unicorn_rails_redmine
[root@www20349u ~]# chkconfig unicorn_rails_redmine on
[root@www20349u ~]# service unicorn_rails_redmine start

確認してみる

[root@www20349u ~]# netstat -ant | grep 3100
tcp        0      0 127.0.0.1:3100              0.0.0.0:*                   LISTEN

3100 がリスンしている。

以上で設定完了!

これで、 http://example.jpredmine の画面が確認できるはず