Blog » Blog Entry

How To Deploy Rails Using mod_rails On Apache

May 31, 2008

I've just spent my afternoon evaluating mod_rails. I must say I am very, very impressed so far. The install is fairly simple. Here's how I did mine:

First I installed the "passenger" gem:

gem install passenger

Next, I used passenger to build a mod_rails module for Apache:

passenger-install-apache2-module

At the end of the mod_rails build process, passenger tells you to enter this into your Apache config:

LoadModule passenger_module \
    /usr/local/lib/ruby/gems/1.8/gems/passenger-1.0.5/ext/apache2/mod_passenger.so
RailsSpawnServer /usr/local/lib/ruby/gems/1.8/gems/passenger-1.0.5/bin/passenger-spawn-server
RailsRuby /usr/local/bin/ruby

I put mine in /usr/local/apache2/conf/httpd.conf, right below my mod_php stuff. I restarted my Apache.

Right below those Apache configuration instructions, it also tells you how to configure an Apache vhost for use with mod_rails:

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

I have a number of production Rails applications, so I chose a new one I'm still working on a bit: http://pejorativewritings.com. I chose this one mostly because it's not getting any traffic yet and I have yet to set a single thing to be cached. I figured it will be a good "test" site for mod_rails.

So my vhost entry ended up looking like this:

<VirtualHost 70.85.173.194:80>
    ServerAdmin webmaster@pejorativewritings.com
    ServerName pejorativewritings.com
    ServerAlias www.pejorativewritings.com
    ErrorLog logs/error_log
    CustomLog logs/pejorativewritings.com-access_log common
    DocumentRoot /rails/pejorative/public
    <Directory /rails/pejorative/public>
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

The extra directory permissions are for static assests, CSS, JavaScript, etc. The hint is in the mod_rails manual, but I included here since I seem to need it both in development and in production modes.

I opened my browser, and w00h00! It was working with no apparent issues!

So then once my excitement died down a bit, I got to wondering. I'm now running a Rails application under Apache with mod_rails, what do I need to do to restart it when I re-deploy a new version? The mod_rails docs say all you have to do is touch a restart file, like this:

touch /rails/pejorative/tmp/restart.txt

So my altered my config/deploy.rb like this:

run "touch #{release_path}/tmp/restart.txt" 
#run "sh #{release_path}/stop_mongrel.sh"
#run "sh #{release_path}/start_mongrel.sh"

I made a simple change to test it, re-deployed and sure enough, it seems to work! Now there's no more restarting any servers when deploying Rails applications.

Tags: ruby, apache, rails, mod_rails

« Benchmarking Rails: Mongrel versus mod_rails Zend Framework core developers continue to dismay »

Add a comment:

Title:

Comment:

Name:

Email: