RubyEE + nginx + UnicornでRedmineを動かしたときのメモ #redmine

Amazon EC2のmicroインスタンスRedmineを動かすにはメモリが十分じゃない感じがしたので、できるだけスリムに頑張ってみた時のメモです。少人数でRedmineを使いたい時には十分じゃないかなと思ってます。

環境

Amazon EC2 microインスタンス上のAmazon Linux(AMI Id: ami-300ca731)
Redmine 1.2.1

Ruby Enterprise Editionのインストール

Ruby Enterprise Edition(RubyEE)はrailsアプリケーションに最適化されたRubyらしいです。
http://www.rubyenterpriseedition.com/ から最新版をダウンロードしてインストールします。

$ # 事前に必要なモジュールをインストールしておきます。
$ sudo yum install -y gcc gcc-c++ make patch zlib-devel openssl-devel readline-devel
$ # RubyEEをダウンロードしてインストールする
$ wget http://rubyenterpriseedition.googlecode.com/files/ruby-enterprise-1.8.7-2011.03.tar.gz
$ tar xvf ruby-enterprise-1.8.7-2011.03.tar.gz
$ cd ruby-enterprise-1.8.7-2011.03
$ sudo ./installer.rb --dont-install-useful-gems --no-dev-docs

すると次のメッセージが表示されます。

This installer will help you install Ruby Enterprise Edition 1.8.7-2011.03.
Don't worry, none of your system files will be touched if you don't want them
to, so there is no risk that things will screw up.

You can expect this from the installation process:

  1. Ruby Enterprise Edition will be compiled and optimized for speed for this
     system.
  2. Ruby on Rails will be installed for Ruby Enterprise Edition.
  3. You will learn how to tell Phusion Passenger to use Ruby Enterprise
     Edition instead of regular Ruby.

Press Enter to continue, or Ctrl-C to abort.

これからこんな流れでインストールしますよーというただの説明なので、Enterを押して次に進めます。
すると必要なモジュールのチェックの後にインストール先を聞かれます。特にこだわりが無ければそのままEnterで進めてOKです。

Checking for required software...

 * C compiler... found at /usr/bin/gcc
 * C++ compiler... found at /usr/bin/g++
 * The 'make' tool... found at /usr/bin/make
 * The 'patch' tool... found at /usr/bin/patch
 * Zlib development headers... found
 * OpenSSL development headers... found
 * GNU Readline development headers... found
--------------------------------------------
Target directory

Where would you like to install Ruby Enterprise Edition to?
(All Ruby Enterprise Edition files will be put inside that directory.)

[/opt/ruby-enterprise-1.8.7-2011.03] : 

あとは待つだけ。
インストールが終わったらパスを通しておきましょう。

$ export PATH=/opt/ruby-enterprise-1.8.7-2011.03/bin/:$PATH

gemパッケージのインストール

$ # gemパッケージのインストールはrootで行います
$ sudo su
# export PATH=/opt/ruby-enterprise-1.8.7-2011.03/bin/:$PATH
# gem install rack -v=1.1.1 --no-rdoc --no-ri
# gem install rake -v=0.8.7 --no-rdoc --no-ri
# gem install i18n -v=0.4.2 --no-rdoc --no-ri
# gem install rails -v=2.3.11 --no-rdoc --no-ri
# # sqliteで運用するため、sqlite3をインストールします
# yum install -y sqlite-devel
# gem install sqlite3 --no-rdoc --no-ri
# # Unicornもついでにインストールしてしまいます
# gem install unicorn --no-rdoc --no-ri
# exit

redmineのインストール

$ wget http://rubyforge.org/frs/download.php/75097/redmine-1.2.1.tar.gz
$ tar xvf redmine-1.2.1.tar.gz
$ # 適当なディレクトリに移動します(今回は/var/www/redmine)
$ sudo mkdir /var/www
$ sudo mv redmine-1.2.1 /var/www/redmine
$ cd /var/www/redmine

今回は小人数というか、ほぼ個人用途なので、SQLite3を使います。
config/database.ymlを以下のように編集します。

$ cp config/database.yml{.example,}
$ vi config/database.yml
production:
  adapter: sqlite3
  database: db/production.sqlite3

セッションデータ暗号化用鍵と、DBのテーブルを作成したらredmineのインストールは完了です。

$ rake generate_session_store
$ rake db:migrate RAILS_ENV=production

Unicornの設定

Unicornの設定は http://www.cocoalife.net/2010/10/post_77.html からまるまる借りました。

$ vi config/unicorn.rb
# ワーカーの数
worker_processes 2

# ソケット
listen '/tmp/unicorn.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

Unicornを起動します。

$ unicorn_rails -c config/unicorn.rb -E production -D

nginxの設定

nginxをインストールして設定ファイルを記述します。

$ sudo yum install -y nginx
$ vi /etc/nginx/conf.d/redmine.conf 

設定は http://www.cocoalife.net/2010/10/post_77.html からのほぼコピペです。

upstream unicorn {
  server unix:/tmp/unicorn.sock;
}

server {
  listen 80;
  server_name example.com;
  root /var/www/redmine/public;
  error_log /var/www/redmine/log/error.log;
  location / {
    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://unicorn;
  }
}

設定が完了したらnginxを起動します。

$ sudo /etc/init.d/nginx start

まとめ

長々と書きましたが、結構簡単に設定できました。
メモリ使用量についてはまだ見てませんが、Redmine自体は軽快に動いてます。