コードを舐める日々

わからないことがわからないことをしる日々

さくらVPSで nginx + MySQL + Unicorn + Redmine の運用

さくらVPSにRedmineを入れることになったたので、その手順をメモします。

構成

バージョン管理: Redmine

Railsサーバ: unicorn

プロキシサーバ: nginx

データベース: MySQL

nginxに redmine.example.com としてアクセスされたら、Redmineを起動している 127.0.0.1:5001 へ繋ぐという形にしました。

下記をほぼ参考にしています。

http://letsspeak.hatenablog.com/entry/2012/10/31/025819

redmine のインストール

githubにあるredmineを clone してきます。

# git clone https://github.com/redmine/redmine.git
# cd redmine
# bundle install --path vendor/bundle --without development test rmagick postgresql sqlite

redmine 用のDB、接続ユーザーを作成

DB名は redmineredmineというユーザー名で作成します。

パスワードは password を変更して下さい。

# mysql -u root -p
# mysql> create database redmine character set utf8;
# mysql> create user 'redmine'@'localhost' identified by 'password';
# mysql> grant all privileges on redmine.* to 'redmine'@'localhost';
# mysql> exit

redmine の DB接続情報を修正

config/database.yaml.example を config/database.yaml にコピーします。

# cp config/database.yaml.example confing/database.yaml

config/database.yaml を下記のように編集します。

(password は「redmine 用のDB、接続ユーザーを作成」で指定したものを入れる)

# Default setup is given for MySQL with ruby1.8. If you're running Redmine
# with MySQL and ruby1.9, replace the adapter name with mysql2.
# Examples for PostgreSQL and SQLite3 can be found at the end.

production:
  adapter: mysql2
  database: redmine
  host: localhost
  username: redmine
  password: password
  encoding: utf8

development:
  adapter: mysql2
  database: redmine_development
  host: localhost
  username: redmine
  password: password
  encoding: utf8

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: redmine_test
  host: localhost
  username: root
  password:
  encoding: utf8

test_pgsql:
  adapter: postgresql
  database: redmine_test
  host: localhost
  username: postgres
  password: "postgres"

test_sqlite3:
  adapter: sqlite3
  database: db/test.sqlite3

redmine の DB を構築します

2つ目のコマンドを実行すると言語選択が表示されます。ここでは「ja」と入力してリターンします。

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

webrick で起動テスト

redmineMySQLのインストールが問題なくできているか確認するために webrick という Ruby 簡易 HTTP サーバを利用して Redmine を起動テストします。

下記のコマンドではポート3000として起動します。アクセスし、 Redmine が開けば成功です。

# ruby script/rails server webrick -e production

(2012/12/29 06:56 追記)

500 error がでて、log/production.log 内を確認したら、「ArgumentError (A secret is required to generate an integrity hash for cookie session data. Use config.secret_token = "some secret phrase of at least 30 characters"in config/initializers/secret_token.rb):」を発見。

対処方法は、 「rake generate_secret_token」で解決。

unicorn のインストール

本番環境では unicorn を利用して起動します。

unicornRubyRails サーバで Ruby 版のHTTPサーバ + daemontools のようなもので、落ちても自動再起動してくれるものです。

gem コマンドで unicorn を入れます。

# gem install unicorn

Redmineunicorn で起動する

config/unicorn.rbを作成します。

#vi config/unicorn.rb

unicorn.rbは下記ように作成します。

worker_processes 2
#working_directory /home/www/rails/charag

listen File.expand_path("tmp/unicorn.sock", ENV['RAILS_ROOT'])
pid File.expand_path("tmp/unicorn.pid", ENV['RAILS_ROOT'])

timeout 60

preload_app true # ダウンタイムをなくす

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

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

そして、 unicorn.rb を指定して Redmine を起動します。

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

5001ポートにアクセスして Redmine が開けば成功です。

ps コマンド、netstat コマンドでポート開放、 unicorn がどんな風に起動しているか確認してみるといいでしょう。

今後のトラブル対策になります。

unicorn を止めるには psコマンドで unicorn の master プロセスを確認し、 kill 9 プロセスNo で止められます。

nginx のプロキシ、バーチャルホスト設定、 nginx の再起動

nginx.conf を編集します。

# vi /etc/nginx/nginx.conf

「 http { 」の下に下記のコードを入れます。

upstream redmine{
  server 127.0.0.1:5001;
}

virtual.conf を編集します。

# vi /etc/nginx/conf.d/virtual.conf

virtual.vconf に下記のコードを入れます。

server {
  listen 80;
  server_name redmine.example.com;

  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;

  auth_basic "Secret Area";
  auth_basic_user_file "/home/www/rails/redmine/.htpasswd";

  location / {

    root /home/www/rails/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;
    proxy_redirect off;
  }
}

nginx を再起動します。

# service nginx restart

virtual.conf で指定した server_name にアクセスして Redmine が開けば成功です。

最後に

git clone で redmine を入れた時、root 権限で行ったため、 nginx の実行ユーザーとは別になっていたのでうまく起動できませんでした。

500 エラーが発生した時は、フォルダ及びアクセス権限のパーミッションをチェックしてみるといいでしょう。

Redmine の初期設定等は下記のURLが詳しいです。

http://redmine.jp/tech_note/first-step/admin/