ホスティングサービスでもRailsが利用できるようになるかも、な「Passenger」
MOONGIFTでも紹介したmod_railsと呼ばれるPassenger。Creative Commonsによるオープンソース・ソフトウェアです。
今回はその導入方法を書きたいと思います。インストールは簡単で、gemを使って行えます。
$ sudo gem install passenger
Password:
Bulk updating Gem source index for: http://gems.rubyforge.org
Building native extensions. This could take a while...
Successfully installed passenger-1.0.1
Installing ri documentation for passenger-1.0.1...
Installing RDoc documentation for passenger-1.0.1...
次に、Apacheに組み込むためのコマンドを実行します。詳細は以下にて。
インストールを開始する前に、別途必要なモジュールを入れておきます。筆者の場合、fastthreadがなかったのでインストールします。これもGemからインストールできます。
$ sudo gem install fastthread
Select which gem to install for your platform (i686-darwin9.1.2)
1. fastthread 1.0.1 (ruby)
2. fastthread 1.0.1 (mswin32)
3. fastthread 1.0 (ruby)
4. fastthread 1.0 (mswin32)
5. Skip this gem
6. Cancel installation
> 1
Building native extensions. This could take a while...
Successfully installed fastthread-1.0.1
Installing ri documentation for fastthread-1.0.1...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
Installing RDoc documentation for fastthread-1.0.1...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
ではインストールを開始します。
$ sudo passenger-install-apache2-module
/opt/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-install-apache2-module:25: warning: Insecure world writable dir /opt/local/var/db/postgresql82 in PATH, mode 040777
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.
このように表示されるので、1を入力してエンターを押します。
Checking for required software...
(中略) --------------------------------------------
The Apache 2 module was successfully installed.
Please edit your Apache configuration file, and add these lines:
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.so
RailsSpawnServer /opt/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-server
RailsRuby /opt/local/bin/ruby
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.
--------------------------------------------
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
</VirtualHost>
And that's it! You may also want to check the Users Guide for security and
optimization tips and other useful information:
/opt/local/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/
これでインストール完了です。次にhttpd.confを修正します。上のコマンド結果に出ているものをそのまま使えば良いだけです。
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.so
RailsSpawnServer /opt/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-server
RailsRuby /opt/local/bin/ruby
RailsEnv development
RailsEnvはデフォルトがproductionになっていますので、必要に応じて変更してください。後はVirtualHostを使う場合は、コマンド結果と同じように設定します。
<VirtualHost *:80>
ServerName local.example.com
DocumentRoot /path_to_rails_root/public/
ErrorLog logs/j-error_log # 必要に応じて
CustomLog logs/j-access_log combined # 必要に応じて
</VirtualHost>
これで設定は完了ですが、Apache2.2系の場合以下の設定を修正する必要があるようです。またはディレクトリ設定の追加を行う必要があります。
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all ← これをコメントアウト
</Directory>
他の環境(Apache 2.0系など)は確認していませんので各自お願いします。ではApacheを再起動して確認してみます。
見事、Railsのページが表示されれば完了です。キャッシュも含めて特に動作は問題ないように思えます。レスポンスも悪くありません。Ajaxの部分も動作します。なお、設定によるのかも知れませんが、静的なファイルについてはApacheから送信されているようなので、これはレスポンス向上にも役立ちますね。
次回はレスポンスの実際の計測を行ってみたいと思います。とりあえず公式サイトに掲載されているレスポンス比較は以下の通りです。
via Documentation — Phusion Passenger (a.k.a. mod_rails)
ということで、Typoであればトップ、PetstoreではThinに劣るくらい、EldoradoではMongrel/Thinと似たり寄ったりといった所のようです。レスポンス性能も高いようですし、何より設置の手軽さが良いですね。
Overview — Phusion Passenger (a.k.a. mod_rails)
http://www.modrails.com/
MOONGIFT: » チェック必須!Rails新時代の到来か?「Passenger(mod_rails for Apache)」:オープンソースを毎日紹介
http://www.moongift.jp/2008/04/passenger/

