今まではApache上にRedmineを用意していたのですが、正直遅いので、nginxとUnicornと組み合わせてみました。ちょっと前まで物理マシン上(Core 3770S)で構築していたのですが、KVM上に構築したnginxとUnicorn上の方が圧倒的に速かったので、常用することにします。
2014/6/28追記 ubuntu14.04上にredmine2.5+nginx+unicorn環境をセットアップを別途書きました。そちらをご参照ください。
ストップウォッチでのアナログな計測結果です
■ 初回アクセス
- 物理マシン上(Apache + passenger) : 8秒
- KVM上(nginx+Unicorn) :1秒以下
■ ログイン後のページ遷移
- 物理マシン上(Apache + passenger) : 0.5秒前後
- KVM上(nginx+Unicorn) :0.1秒前後(物理マシンよりも1/2ぐらい早い)
今回は開発環境を作るのではなく、zabbixの入っているサーバにredmineも同居させたかっただけなので、ubuntu13.04をインストールした後に楽できるようにrubyやrailsはapt-getによるお手軽インストールです。大体の流れは下記の通り。
- redmine2.3.3のインストール
- Unicorn環境の設定
- nginxでのリバースプロキシ設定
redmine2.3.3のインストール
apt-getで必要なパッケージのインストール
apt-get install ruby1.9.3 subversion apt-get install rails3 apt-get install nginx unicorn apt-get install libmysqlclient-dev apt-get install imagemagick libmagickcore-dev libmagickwand-dev ruby-rmagick
最新化(と思ったらrails4にまで最新化されました)
rails4が入ってしまっても、redmineのパッケージのディレクトリ上で、bundle installすれば、redmine用の必要なパッケージはバージョン固定で入れてくれるので気にしないw
# 必要なパッケージを入れておく gem update gem install bundler gem install rmagick -v '2.13.2' gem install mysql2 -v '0.3.13' # redmineを落してくる cd /var/www/ svn checkout http://svn.redmine.org/redmine/branches/2.3-stable redmine cd redmine vi Gemfile # gem 'unicorn'をgem "rails", "3.2.13"の下あたりに追記 bundle install rake generate_secret_token
DBの作成
mysql -uroot -p create database redmine character set utf8; grant all on redmine.* to redmine@localhost identified by 'yourpassword'; flush privileges;
config/database.ymlの準備
cd /config mv database.yml.example database.yml
production: adapter: mysql2 database: redmine host: localhost username: redmine password: "yourpassword" encoding: utf8
config/database.ymlの準備
# テーブルを作る RAILS_ENV=production rake db:migrate # 基本データの投入。途中の質問はjaを選択。 RAILS_ENV=production rake redmine:load_default_data
パーミッションの設定をして、動くことを確認しておく。
mkdir tmp public/plugin_assets sudo chown -R www-data:www-data files log tmp public/plugin_assets sudo chmod -R 755 files log tmp public/plugin_assets # まずは、webrickで動くことを確認しておく。Ctrl-cで終了して。 ruby script/rails server webrick -e production
Unicorn環境の設定
apt-getでunicornを入れていたので、起動スクリプトと設定ファイルは既に準備されているが、内容が古いのか上手く動かなかったので、手を入れました。
cd /var/www/redmine touch log/unicorn.stdout.log touch log/unicorn.stderr.log chown www-data:www-data log/unicorn.*
/etc/default/unicorn の中身を下記のように編集します。
# Change paramentres below to appropriate values and set CONFIGURED to yes. CONFIGURED=yes # Default timeout until child process is killed during server upgrade, # it has *no* relation to option "timeout" in server's config.rb. TIMEOUT=60 # Path to your web application, sh'ld be also set in server's config.rb, # option "working_directory". Rack's config.ru is located here. #APP_ROOT=/path/to/your/web/application APP_ROOT=/var/www/redmine # Server's config.rb, it's not a rack's config.ru CONFIG_RB="$APP_ROOT/config/unicorn.rb" # Where to store PID, sh'ld be also set in server's config.rb, option "pid". PID=/run/unicorn.pid # Additional arguments passed to unicorn, see man (1) unicorn. UNICORN_OPTS="-D -E production -l 127.0.0.1:3000 -c $CONFIG_RB $APP_ROOT/config.ru"
config/unicorn.rbの中身を下記のように編集します。
worker_processes 2 listen File.expand_path("tmp/unicorn.sock", ENV['RAILS_ROOT']) pid File.expand_path("tmp/unicorn.pid", ENV['RAILS_ROOT']) preload_app true stdout_path "/var/www/redmine/log/unicorn.stdout.log" stderr_path "/var/www/redmine/log/unicorn.stderr.log" GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{server.config[:pid]}.oldbin" if old_pid != server.pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH end end sleep 1 end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
※『Rails3のアプリケーションをnginx+unicorn上で動かすメモ』を参考にさせてもらいました。
Unicornを起動させて。3000番でlistenしていることを確認する。
/etc/init.d/unicorn start netstat -an | grep 3000 ps -aux | grep unicorn
nginxでのリバースプロキシ設定
Unicornでポート80で動かす誘惑に負けずにnginxでリバースプロキシを設定します。Apacheで言うところのバーチャルホストのように、アクセス時のホスト名がredmine.senritu.netだった場合にのみ、redmineを表示できるようにします。
バーチャルホストみたいにしておけば、他にもウェブ管理ツールをインストールした時に、ディレクトリを切って~みないな事を考えなくて良いですからね。
/etc/nginx/nginx.conf
http { # いろいろ設定が書かれているが、最後に追加しておけばいい upstream redmine { server 127.0.0.1:3000; } }
/etc/nginx/sites-available/defaultに下記を追加しておく。同ファイルの一番上にでも追加しておけばOK。
server { listen 80; root /var/www/redmine/public; index index.html index.htm; server_name redmine.senritu.net; access_log /var/log/nginx/redmine.access.log; error_log /var/log/nginx/redmine.error.log; proxy_connect_timeout 60; proxy_read_timeout 60; proxy_send_timeout 60; location / { root /var/www/redmine/public; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; fastcgi_buffers 16 16K; fastcgi_buffer_size 32K; # /etc/nginx/nginx.confで定義した値 proxy_pass http://redmine/; proxy_redirect off; } }
仕上げに、OS起動時にUnicornとnginxが自動起動するようにしておく。
# 既にApacheが入っていた場合には起動を止める sysv-rc-conf --level 35 apache2 off # 自動起動の設定 sysv-rc-conf --level 35 unicorn on sysv-rc-conf --level 35 nginx on
参考にさせてもらったサイト
- Rails3のアプリケーションをnginx+unicorn上で動かすメモ
- Redmine.JP — Redmine日本語情報サイト