nginx + unicorn + redmine 2.0 を ubuntu で動かしてみた。

先日、redmine 2.0 にバージョンアップされました。
Rails 3.2.3 で動いている最新バージョンです。

Ruby を触ったときないときに Passenger で動かす Redmine
サイトを見ながらやりましたが何をやっていたのか全然理解していませんでした。
今は業務でも nginx + unicorn + Rails を使用しているのでやってみようと思いました。

以下の環境にインストールしてみる

  • ubuntu 11.04
  • rbenv + rubybuild で Ruby 1.9.3 をインストール済み
  • bundler インストール済み
  • mysql インストール済み

Redmine のインストール


だいたいは公式サイトと同じなので参照してください。

1. svn からチェックアウトしてソースを取得。

svn checkout http://redmine.rubyforge.org/svn/branches/2.0-stable redmine
cd redmine

2. bundle install
rmagick, postgresql, sqlite はインストールしません。

bundle install --without development test rmagick postgresql sqlite --path vendor/bundle

3. mysql でユーザだデータベースを作成

create database redmine character set utf8;
create user 'redmine'@'localhost' identified by 'my_password';
grant all privileges on redmine.* to 'redmine'@'localhost';

4. データベース設定ファイルの設定

cp config/database.yml.example config/database.yml

コピーしたファイルの編集する。コメント解除し少しだけ変える。

production:
  adapter: mysql2 # mysql2 に変更
  database: redmine # 3.で作成したデータベース名
  host: localhost
  username: redmine # 3.で作成したユーザ
  password: password # 3.で作成したユーザのパスワード
  encoding: utf8

5. rake コマンドでちょっと設定する

bundle exec rake generate_secret_token
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake redmine:load_default_data # このあと、言語を選択するので ja と入力

6. webrick で起動確認

bundle exec rails s -e production

http://localhost:3000/ にアクセスして Redmine の画面が出れば成功です!

unicron のインストール


1. GemFile.local を作成
追加で gem を作成したい場合は GemFile.local の中に記述すればOK。

gem 'unicorn'

2. bundle install

bundle

3. config/unicorn.rb を作成して設定する
こことほぼ同じです。

worker_processes 2

listen '/tmp/redmine.sock'

stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
  
preload_app true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
  
  old_pid = "#{ server.config[:pid] }.oldbin"
  unless old_pid == server.pid
    begin
      Process.kill :QUIT, File.read(old_pid).to_i
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end
  
after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

4. unicorn 起動
webrick を起動していた場合は ctrl + c で終了しておいてください。

bundle exec unicorn_rails -c config/unicorn.rb -E production -D

nginx のインストール


ソースからインストールしました。
PCRE(Perl 互換正規表現ライブラリ)がインストールされていないと make でエラーするので

sudo apt-get install libpcre3-dev

を実行しました。

1. インストール

wget http://nginx.org/download/nginx-1.2.0.tar.gz
tar zxvf nginx-1.2.0.tar.gz
cd nginx-1.2.0
./configure
make
sudo make install

2. 設定 /usr/local/nginx/conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream redmine {
        server unix:/tmp/redmine.sock;
    }
    server {
        listen       80;
        server_name  localhost;
        root /path/to/redmine;
        error_log /path/to/redmine/log/nginx_error.log;
        try_files $uri @unicorn;
        location @unicorn {
            if (-f $request_filename) { break; }
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_pass http://redmine;
        }
    }
}

だいたいデフォルトをそのまま使用し、unicorn 用にリバースプロキシの設定をします。
※ error_page 等は省略しました。
3. 起動

sudo /usr/local/nginx/sbin/nginx

これで http://localhost にアクセスすると Redmine が表示されるはずです!