Phusion Passenger a.k.a mod_rails

Posted by marcin
on Saturday, May 24

As deployment of even very simple web Rails apps was usually a pain (well, most of the cases) I decided to give mod_rails a go. The installation was a breeze as mod_rails installer guides you through the process nicely. Installation itself is as easy as that :

gem install passenger

Once your gem is installed run the following command (as root, otherwise you will get some “permission denied” errors)

passenger-install-apache2-module

Important note: In order to make Apache use your new shiny mod_rails you need to specifically modify its configuration by adding the following lines to your httpd.conf (NOTE: please change the paths accordingly!)

LoadModule passenger_module /Path/To/Gems/1.8/gems/passenger-1.0.5/ext/apache2/mod_passenger.so
RailsSpawnServer /Path/To/Gems/1.8/gems/passenger-1.0.5/bin/passenger-spawn-server
RailsRuby /Path/To/bin/ruby

After that all you need to do to make your app working is to modify / create virtual host definition:

<VirtualHost *:80>
      ServerName www.yourhost.com
      DocumentRoot /path/to/your/rails_app/public
</VirtualHost>

Now you are good to go ahead and restart your apache.

apachectl configtest
apachectl restart

For the test purposes I created a fresh rails app and start apache to serve it. The default page popped in on the screen which I took as a good sign ;) Next, I had created a simple controller with one action in it and then tried to reload the browser. The changes I made in the application weren’t reflected in the browser (the old version of the app was still in memory). Like in the case of standard setup (Webrick/Mongrel/etc.) with mod_rails you need to restart the application server in order for changes to take effect. One interesting thing here is that there is no command to be invoked from the command line to do it. Instead one need to create restart.txt file in application’s /tmp folder and then reload web browser pointing to your application.

touch /path/to/your/rails_app/tmp/restart.txt

Passenger loads your application into memory and keeps it there until one of the following events occur:

1. restart.txt has been created in application’s /tmp folder and web browser point to your app has been refreshed

2. Application has been idle for some time (the default it 120sec.) [see snippet below…]
[27:Application.h:264] Application 0x1a2ceb0: created.
[27:StandardApplicationPool.h:249] Cleaning idle app /path_to_your_app (PID 31918)
[27:Application.h:274] Application 0x1a2ceb0: destroyed.

If you have any issues with your deployment resulting in your app not being served / throwing errors go ahead and analyze your apache error_log (usually /var/log/apache2/error_log) and your application’s production log. It will give you some clues where things went wrong.

Also, mod_rails comes with pretty good documentation which can be found here.