RedmineでUnicornサーバー

Redmineのインストール

RedmineをGitHubからダウンロードしてきて、bundle installを行います(今回はMySQLを使うのでpostgresql sqliteは外しました)

git clone https://github.com/redmine/redmine.git

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

MySQLでDB作成

MySQLでDBとユーザーを作成します。今回は出たばかりのMySQL5.6.10を使いました。ユーザーやデータベースは後からYMLで設定しますので、下記ではzabbixユーザーとZabbixデータベースを作っていますが、名前は別でも構いません。

mysql -u root -p
mysql create database redmine;
mysql create user 'redmine'@'localhost' identified by '*******';
mysql grant all privileges on redmine.* to 'redmine'@'localhost';

databaseのYMLを編集します。

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

編集の際の注意点としては、adapter: mysql2を忘れずに入れてください。

初期DB作成

初期DBの作成と言語選択を行います。2詰めのコマンド実行時に言語を聞かれますが、日本語jaを選択します。

$RAILS_ENV=production rake db:migrate
$RAILS_ENV=production rake redmine:load_default_data

Select language: ar, bg, bs, ca, cs, da, de, el, en, en-GB, es, et, eu, fa, fi, fr, gl, he, hr, hu, id, it, ja, ko, lt, lv, mk, mn, nl, no, pl, pt, pt-BR, ro, ru, sk, sl, sq, sr, sr-YU, sv, th, tr, uk, vi, zh, zh-TW [en] ja

Unicornサーバーのインストール

UnicornはRuby on Railsに特化したアプリケーションサーバーで、高速なのと並列処理を得意とします。
まずUnicornのGemfile.localを作成します。

echo 'gem "unicorn"'  Gemfile.local
bundle install

うまくいけば以下のようなメッセージが表示されます

bundle install
Fetching gem metadata from http://rubygems.org/.........
Fetching gem metadata from http://rubygems.org/..
Installing rake (10.0.3) 
Installing i18n (0.6.1) 
Installing multi_json (1.5.0) 
Installing activesupport (3.2.9) 
Installing builder (3.0.0) 
Installing activemodel (3.2.9) 
Installing erubis (2.7.0) 
Installing journey (1.0.4) 
Installing rack (1.4.4) 
Installing rack-cache (1.2) 
Installing rack-test (0.6.2) 
Installing hike (1.2.1) 
Installing tilt (1.3.3) 
Installing sprockets (2.2.2) 
Installing actionpack (3.2.9) 
Installing mime-types (1.19) 
Installing polyglot (0.3.3) 
Installing treetop (1.4.12) 
Installing mail (2.4.4) 
Installing actionmailer (3.2.9) 
Installing arel (3.0.2) 
Installing tzinfo (0.3.35) 
Installing activerecord (3.2.9) 
Installing activeresource (3.2.9) 
Installing coderay (1.0.8) 
Installing rack-ssl (1.3.2) 
Installing json (1.7.6) with native extensions 
Installing rdoc (3.12) 
Installing thor (0.17.0) 
Installing railties (3.2.9) 
Installing jquery-rails (2.0.3) 
Installing kgio (2.8.0) with native extensions 
Installing mysql2 (0.3.11) with native extensions 
Installing net-ldap (0.3.1) 
Installing ruby-openid (2.1.8) 
Installing rack-openid (1.3.1) 
Using bundler (1.2.3) 
Installing rails (3.2.9) 

Unicornサーバーの設定

私はUnicornのconfigは以下のように設定しました。

vim config/unicorn.rb 
worker_processes 8

listen 8008, :tcp_nopush => true

timeout 30

pid File.expand_path("tmp/pids/unicorn.pid", ENV['/srv/www/redmine/redmine'])

stderr_path File.expand_path("log/unicorn.stderr.log", ENV['/srv/www/redmine/redmine'])
stdout_path File.expand_path("log/unicorn.stdout.log", ENV['/srv/www/redmine/redmine'])

preload_app true
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!
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

Unicornの起動

以下のコマンドで起動します。

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

Nginxの設定

Unicornサーバーのポートと接続してリバースプロキシするために、nginx.confに以下の設定を入れました。

upstream redmine {
        ip_hash;
        server 127.0.0.1:8008;
}

server {

            listen   80;
           server_name redmine.geek.sc;

  location / {
        auth_basic            "Saito Server Security";
        auth_basic_user_file  "/srv/www/.htpasswd";
    root /srv/www/redmine/redmine/public;
    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/;
          }
}

Nginxを再起動します。これで、redmine.geek.scのRedmineへアクセスできるようになりました。

Unicornサーバーの自動起動

uUnicornサーバーを電源投入時に自動起動させる方法はいくつかあるのですが、私は面倒だったので、起動時に自動実行される設定ファイル(openSUSEは/etc/init.d/boot.local、CentOSは/etc/rc.local)に次のように記述しました。

cd /srv/www/redmine/redmine
bundle exec unicorn_rails -c config/unicorn.rb -E production -D

これで再起動時もUnicornサーバーが自動実行されます。

これで設定完了です。Unicornで高速Redmine環境(Ruby on rails)を!