Apache上でRuby on Railsアプリケーションを動かす/Passenger(mod_rails for Apache)の利用 (original) (raw)

この文書は内容が古くなっています。 インストール ページで新しい文書を探してください。

要約

Redmine等のRuby on Railsアプリケーションを"Phusion Passenger(mod_rails for Apache)":http://www.modrails.com/ を利用してApache上で稼働させる手順を説明します。
運用の簡素化、複数インスタンスを並列稼働させることによるレスポンスの改善が期待できます。

インストールの流れは次の通り。

  1. gem install passenger
  2. passenger-install-apache2-module
  3. httpd.confの変更

Phusion Passengerとは

Railsアプリケーションを実行するためのApacheモジュールです。

特徴

Mongrelと比較したメリット

Passengerのインストール

gemでインストールできます。

1. gem install passenger

passenger-install-apache2-moduleを実行し、Apacheモジュールのビルドとインストールを行います。

1. passenger-install-apache2-module

このスクリプトは対話形式で処理を行います。画面の指示に従って進めれば容易に処理を完了できます。
全体の流れは次の通りです。

  1. 必要なソフトウェアの存在チェック
  2. モジュールのビルドおよびインストール
  3. httpd.confの設定方法の表示

例として、Debian 4.0 + Apache 2.2(ソースからビルド)の環境でpassenger-install-apache2-moduleを実行したときの画面を示します。

注意:
Apacheをソースからビルドしてインストールした環境では、APXS2およびPATH環境変数を設定する必要があります。設定を行わないとスクリプトがApache本体、Apacheヘッダファイル、APRを見つけることができず、インストールができません。

インストーラ実行。

1. export APXS2=/usr/local/apache2/bin/apxs

2. export PATH=/usr/local/apache2/bin:$PATH

3. passenger-install-apache2-module

手順が表示される。

Welcome to the Passenger Apache 2 module installer.

This installer will guide you through the entire installation process. It

shouldn’t take more than 3 minutes in total.

Here’s what you can expect from the installation process:

  1. The Apache 2 module will be installed for you.
  2. You’ll learn how to configure Apache.
  3. You’ll learn how to deploy a Ruby on Rails application.

Don’t worry if anything goes wrong. This installer will advise you on how to

solve any problems.

Press Enter to continue, or Ctrl-C to abort.

Enterキーを押して次へ進む。環境の確認とPassengerのビルドが行われる。

Checking for required software…

* GNU C++ compiler… found at /usr/bin/g++

* Ruby development headers… found

* OpenSSL support for Ruby… found

* RubyGems… found

* Rake… found at /usr/bin/rake

* Apache 2… found at /usr/local/apache2/bin/httpd

* Apache 2 development headers… found at /usr/local/apache2/bin/apxs

* Apache Portable Runtime (APR) development headers… found

* fastthread… found  

-————————————————————-


Compiling and installing Apache 2 module…

cd /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1

rake clean apache2

(in /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1)

(中略)


The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.so RailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-server RailsRuby /usr/bin/ruby1.8

After you restart Apache, you are ready to deploy any number of Ruby on Rails

applications on Apache, without any further Ruby on Rails-specific

configuration!

Press ENTER to continue.

ビルドに成功すると、Passengerを組み込むためのhttpd.confの設定が表示されるのでコピーしておく。

Enterキーを押して次へ進む。virtualhostの設定のサンプルが表示される。


Deploying a Ruby on Rails application: an example

Suppose you have a Ruby on Rails application in /somewhere. Add a virtual host to your Apache configuration file, and set its DocumentRoot to /somewhere/public, like this:

<VirtualHost *:80> ServerName www.yourhost.com DocumentRoot /somewhere/public

And that's it! You may also want to check the Users Guide for security and optimization tips and other useful information:

/usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/doc/Users guide.html

Enjoy Passenger, a product of Phusion (www.phusion.nl) :-) http://www.modrails.com/

Apacheの設定

passenger-install-apache2-moduleの最終段階の画面で"Please edit your Apache configuration file, and add these lines:"という指示とともにhttpd.confに追加すべき設定内容が表示されますので、httpd.confを修正します。

httpd.conf:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.so

RailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-server

RailsRuby /usr/bin/ruby1.8

Passengerを組み込んだApacheではDocumentRootを$RAILS_ROOT/publicに設定すれば、自動的にRailsアプリケーションが認識されます。

例えば、ネームベースバーチャルホスト"redmine.example.jp"で、/var/lib/rails/redmineにインストールされたRedmineを動かすには次のようにします。

httpd.conf:

<VirtualHost *:80>

ServerName redmine.example.jp

DocumentRoot /var/lib/rails/redmine/public

以上で設定は完了です。Apacheを再起動してください。

運用

Railsアプリケーションの起動方法

Apacheを起動すれば、Railsアプリケーションも利用可能な状態になります。

Railsアプリケーションの再起動方法

二つの方法があります。

1. Apacheを再起動

Apacheを再起動することにより、全てのRailsアプリケーションが再起動されます。

2. $RAILS_ROOT/tmp/restart.txtというファイルを作成

例えばRailsアプリケーションが/var/lib/rails/redmineにインストールされている場合、/var/lib/rails/redmine/tmp/restart.txtというファイルを作成してください。中身は空でかまいません。restart.txtを作成した後の最初のリクエストの際に、PassengerがRailsアプリケーションを再起動します。再起動が完了するとrestart.txtは削除されます。

この方法は次のメリットがあります。

Passengerのチューニング

サーバ環境や利用頻度にあわせて以下のパラメータの調整を行ってください。
いずれもhttpd.conf中で設定します。なお、これらはApache全体に対する設定であり、バーチャルホストごとの指定はできません。

RailsMaxPoolSize

Apacheサーバ上で生成するRailsのインスタンス数を設定します。

デフォルト値は20ですが、メモリが少ない環境では減らすことを検討してください。アクセスが集中した際にメモリが不足して極端にレスポンスが悪化する恐れがあります。"Passenger Users Guide":http://www.modrails.com/documentation/Users%20guide.html では、メモリ256MBのVPS環境では2、2GBのメモリを搭載したサーバでは30が推奨されています。

RailsPoolIdleTime

Railsインスタンスへのリクエストがこの設定値以上の時間無かった場合に、インスタンスを終了させます。

デフォルト値は120(秒)です。この値が小さすぎると、インスタンスの生成・終了が頻繁に行われることになりレスポンスが悪化します。逆に大きすぎると必要以上に長時間メモリを占有されてしまいます。Passenger Users Guideでは、ユーザーのwebサイト上の平均滞在時間の2倍を推奨しています。例えば1回のRedmine上での平均的な作業時間が5分の場合、推奨設定値は600(秒)です。

トラブルシューティング

パーミッションの問題

Railsアプリケーションのディレクトリ($RAILS_ROOT)および$RAILS_ROOT以下のファイル・ディレクトリのオーナーは、root以外としてください。

Passengerでは、root権限でRailsアプリケーションを動かすことはできません。Railsアプリケーションが動作するユーザーは$RAILS_ROOT/config/environment.rbのオーナーとなりますが、environment.rbがrootの場合はnobodyユーザーで動作します。このとき、オーナーがrootである$RAILS_ROOT/logディレクトリや$RAILS_ROOT/tmpディレクトリに書き込みができず、アプリケーションが動作しないことがあります。