Apache2+PassengerでRailsを動かす

手順

前提
  • Apache2はインストール済み
  • WEBrickRailsアプリの動作確認済み
    • $ rvm use 1.8.7-head@rails3.0 --default のRVM環境でやります
passengerインストール
$ gem install passenger
passenger-install-apache2-module実行
$ passenger-install-apache2-module

Enterキーで進んでいくと、

Installation instructions for required software

 * To install Curl development headers with SSL support:
   Please run apt-get install libcurl4-openssl-dev or libcurl4-gnutls-dev, whichever you prefer.

 * To install Apache 2 development headers:
   Please run apt-get install apache2-prefork-dev as root.

 * To install Apache Portable Runtime (APR) development headers:
   Please run apt-get install libapr1-dev as root.

 * To install Apache Portable Runtime Utility (APU) development headers:
   Please run apt-get install libaprutil1-dev as root.

If the aforementioned instructions didn't solve your problem, then please take
a look at the Users Guide:

  /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7/doc/Users guide Apache.html

とあるのでインストールする。

$ sudo aptitude install libcurl4-openssl-dev
$ sudo aptitude install apache2-prefork-dev
$ sudo aptitude install libapr1-dev
$ sudo aptitude install libaprutil1-dev

再度passenger-install-apache2-moduleを実行する。

$ passenger-install-apache2-module

Enterキーで進んでいくと、今度は成功。

--------------------------------------------
The Apache 2 module was successfully installed.

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

   LoadModule passenger_module /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7/ext/apache2/mod_passenger.so
   PassengerRoot /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7
   PassengerRuby /home/atsm/.rvm/wrappers/ruby-1.8.7-head@rails3.0/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 Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   <VirtualHost *:80>
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
      <Directory /somewhere/public>
         AllowOverride all              # <-- relax Apache security settings
         Options -MultiViews            # <-- MultiViews must be turned off
      </Directory>
   </VirtualHost>

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

  /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7/doc/Users guide Apache.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.

とあるのでメモしておく。

/etc/apache2/apache2.conf編集

まず

LoadModule passenger_module /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7/ext/apache2/mod_passenger.so
PassengerRoot /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7
PassengerRuby /home/atsm/.rvm/wrappers/ruby-1.8.7-head@rails3.0/ruby

Apache設定ファイルに追記する。

$ cd /etc/apache2
$ sudo cp -p apache2.conf apache2.conf.bak
$ sudo vim apache2.conf

ファイルの最後に追記する。

LoadModule passenger_module /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7/ext/apache2/mod_passenger.so
PassengerRoot /home/atsm/.rvm/gems/ruby-1.8.7-head@rails3.0/gems/passenger-3.0.7
PassengerRuby /home/atsm/.rvm/wrappers/ruby-1.8.7-head@rails3.0/ruby
確認用Railsアプリを作成
$ cd /var
$ sudo chmod 777 www
$ cd www
$ rails new test
Apacheバーチャルホストの設定修正
$ cd /etc/apache2/sites-available/
$ sudo cp -p default default.bak
$ vim default
  1 <VirtualHost *:80>
  2         ServerAdmin webmaster@localhost
  3
  4         DocumentRoot /var/www/practice/public
  5         RailsEnv development
  6         <Directory />
  7                 Options FollowSymLinks
  8                 AllowOverride None
  9         </Directory>
 10         <Directory /var/www/practice/public>
 11 #               Options Indexes FollowSymLinks MultiViews
 12 #               AllowOverride None
 13 #               Order allow,deny
 14 #               allow from all
 15          AllowOverride all
 16          Options -MultiViews
 17         </Directory>
 18
 19         ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
 20         <Directory "/usr/lib/cgi-bin">
 21                 AllowOverride None
 22                 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
 23                 Order allow,deny
 24                 Allow from all
 25         </Directory>
 26
 27         ErrorLog ${APACHE_LOG_DIR}/error.log
 28
 29         # Possible values include: debug, info, notice, warn, error, crit,
 30         # alert, emerg.
 31         LogLevel warn
 32
 33         CustomLog ${APACHE_LOG_DIR}/access.log combined
 34
 35     Alias /doc/ "/usr/share/doc/"
 36     <Directory "/usr/share/doc/">
 37         Options Indexes MultiViews FollowSymLinks
 38         AllowOverride None
 39         Order deny,allow
 40         Deny from all
 41         Allow from 127.0.0.0/255.0.0.0 ::1/128
 42     </Directory>
 43
 44 </VirtualHost>

のようなかんじ。

※追記(2011/05/20)
passengerで起動すると何も指定がないと「production」(本番)環境で動くので、5行目のRailsEnv developmentを追記。
Apache再起動
$ sudo /etc/init.d/apache2 restart
確認
ゲストOS
http://localhost/
ホストOS
http://ゲストOSのIP/

で確認。
今度はホストOSからアクセスしても問題なく早い。ということはWEBrickの問題だったのかなあ。

参考URL