Redmine2.2をnginx+unicornで動かす
alminiumを利用すると、apache+passengerで動作するのですが、さらの状態だとレスポンスが少し悪いときがあります。対策としてFastCGIを組み込むなどがあるのですが、今回は別の理由もあって、nginx+unicornを利用することにしました。
設定ファイルはこちらのブログを参考にさせていただきました。
http://d.hatena.ne.jp/kaz_shu/20120520/1337527540
unicornの設定についてはこちらの方がコメント付きなので参考になるかもしれません。
http://www.cocoalife.net/2010/10/post_77.html
実行環境
設定手順
unicornのインストール
RAILS_ROOTに移動して、追加のgemを定義します。
cd /opt/alminium
Gemfile.local ファイルを作成して、次の内容を記述します。
gem 'unicorn'
記述後、インストールを行います。
bundle install --without development test postgresql sqlite
unicornの設定ファイルの作成
RAILS_ROOT/config/unicorn.rbに次の内容を記述します。これは http://www.cocoalife.net/2010/10/post_77.html を参考にさせていただきました。
# ワーカーの数
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
# 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
nginxのインストール
以前のブログで書いたので省略します。リポジトリからインストールしました。
http://wankomagic.hatenablog.com/entry/2012/12/10/024356
nginx-unicornの連携の設定
/etc/nginx/conf.d/redmine.conf を作成し、次の記述をします。
upstream redmine {
server unix:/tmp/redmine.sock;
}
server {
listen 80;
server_name localhost;
server_name $HOSTNAME;
root /opt/alminium/public;
error_log /opt/alminium/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の起動スクリプトとして、 /etc/init.d/redmine を作成します。
こちらのサイトを参考にさせていただきました。ひとまず動かすのはRedmineだけなので、複数アプリケーション対応部分は削ってます。
http://www.02.246.ne.jp/~torutk/swetools/redmine/setupCentOS6.html#SEC121
# chkconfig: 345 83 17
# description: Redmine on unicorn
set -e
sig () {
test -s "$PID" && kill -$1 `cat "$PID"`
}
RAILS_ROOT=/opt/alminium
RAILS_ENV=production
echo -n "$RAILS_ROOT: "
cd $RAILS_ROOT || exit 1
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID="$PID.oldbin"
CMD="bundle exec unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D"
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting"
$CMD
;;
stop)
sig QUIT && echo "Stopping" && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && echo "Forcing a stop" && exit 0
echo >&2 "Not running"
;;
restart)
sig QUIT && echo "Restarting"
$CMD
;;
*)
echo >&2 "Usage: $0 <start|stop|force-stop|restart>"
exit 1
;;
esac
自動起動設定をして、起動します。
chkconfig --add redmine
chkconfig nginx on
service start redmine
service start nginx
これで今までどおりのアドレスでアクセスできると思います。ホスト名の設定などでうまくいかない場合は、 /etc/nginx/conf.d/redmine.conf ファイルの server_name を追加してみてください。IP、ホスト名どちらでも指定可能です。
nginxをCentOSのリポジトリからインストールしてる場合は、/etc/nginx/conf.d/default.confを削除しておくと良いです。