Passenger(mod_rails)をインストールしてみる

Rails運用環境として何かと話題のmod_railsを導入します。
http://www.nearch.jp/mongrelからmod_railsに切り替えました。体感的には速くなった気がする。

利点

実際の導入作業の前にmod_rails構成の利点をメモ。

  • mongrel_cluster等に比べて構成がとてもシンプルになる

運用の際にApacheだけ気にしてればいいので楽。

  • Railsアプリの初期設定が簡単

VirtualHost設定すれば動く。

  • mongrel_cluster構成に比べてオーバーヘッドが減る

コンテキストスイッチが減る。
上記くらいの利点があると思う。
あとベンチマークは取ってないけど、mongrel_cluster + Apache構成よりは速い。
また開発が活発なので、これから実行速度、安定性が増していきそう。将来性に期待。

インストール

今回の例はCentOS5環境です。

# gem install passenger

gemから導入。

# passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.0.3.

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.

apacheモジュールをコンパイルして導入します。
ここでエンターを押すと環境チェックが動きます。何か不足している物があれば教えてくれるので、Ctrl-Cして入れてやり直します。
忘れ物がなければ自動でコンパイルが行われます。

--------------------------------------------
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-2.0.3/ext/apache2/mod_passenger.so
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3
   PassengerRuby /usr/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.

一通り作業が終わるとApacheの設定が書き出されるのでメモ。

--------------------------------------------
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:

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

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

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

次にVirtualHostの設定を教えてくれるのでメモ。

Apacheの設定

/etc/httpd/conf.d/passenger.conf を作成して、先ほどのメモをペースト。

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.3
PassengerRuby /usr/bin/ruby

Railsアプリを配置

適当なアプリ(hogehoge)を設置してみます。
設置場所はrailsappのユーザディレクトリ配下にします。

# useradd railsapp
# chmod 705 /home/railsapp/
# su - railsapp
% cd /home/railsapp
% rails hogehoge

hogehogeの内容は適当にどうぞ。
パーミッションは、/home/railsappをapacheから読めるように設定しておけばOKです。アプリはconfig/environment.rbの持ち主の権限で動作します。

VirtualHostの設定

/etc/httpd/conf.d/virtualhost.conf を作ります。

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName hogehoge.example.jp
  DocumentRoot /home/railsapp/hogehoge/public
</VirtualHost>

Apache再起動確認

# /etc/init.d/httpd restart

これで http://hogehoge.example.jp で動作してるはず。
Capistranoでのデプロイ方法は後ほど別エントリで書きます。