Redmine を nginx + Unicorn で動かしてみる

今後のために、一度 Webサーバである nginx と Railsサーバ?の Unicorn を使って Redmine を動作させてみることにしました。
すでに Redmine は Apache + Passenger で動いています。

環境

  • Ubuntu Server x86_64 10.04

Rack のアンインストール

このエントリを書いている時点では Redmine は Rails 2.3.5 で作成されていて、 Rails 2.3.5 は Rack 1.0.1 を使用します。
もし Rack の1.2.1とかがインストールされている場合にはアンインストールしましょう。
でないと、こんなエラーが出て起動しません。
これにだいぶんはまりました。

I, [2010-10-05T14:04:25.155622 #26791]  INFO -- : unlinking existing socket=/tmp/unicorn.sock
I, [2010-10-05T14:04:25.155914 #26791]  INFO -- : listening on addr=/tmp/unicorn.sock fd=3
I, [2010-10-05T14:04:25.156344 #26791]  INFO -- : Refreshing Gem list
Missing the Rails 2.3.5 gem. Please `gem install -v=2.3.5 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.

Unicorn 0.96 doesn’t play nice with Rails 2.3.5

インストール

$ sudo apt-get install nginx
$ sudo gem install unicorn

Unicorn の設定

設定のほとんどは nginx + Unicorn を試してみた – milk1000cc からのコピペです。

ここで設定するソケットを後で使用します。

$ vi $RAILS_ROOT/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
      # SIGTTOU だと worker_processes が多いときおかしい気がする
      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 の設定

http にきたものはすべて https に転送するようにしているので、設定は以下のようになります。

upstream unicorn の部分で設定するものは、先ほどの unicorn.rb で設定したソケットです。
最下行から2行目の proxy_pass で設定する部分は、最上位の upstream hoge の hoge になります。

$ vi /etc/nginx/sites-available/redmine
upstream unicorn {
         server unix:/tmp/unicorn.sock;
}

server {
        listen          80;
        server_name     example.com; 
        rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
        listen  443;    
        server_name     example.com;

        root            /var/www/redmine/public;
        error_log       /var/www/redmine/log/error.log;

        ssl  on;
        ssl_certificate  /etc/nginx/ssl/apache.pem;
        ssl_certificate_key  /etc/nginx/ssl/apache.pem;
        
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;

        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;
        }
}

サーバのスタート

$ sudo ln -s /etc/nginx/sites-available/redmine /etc/nginx/sites-enabled/redmine
$ sudo /etc/init.d/nginx start

参考文献