Using Capistrano with Passenger (mod_rails)

May 10th, 2008

I love Phusion Passenger. It takes away most of the pain from deploying. Instead of having to mess around with lengthy apache config files, you can just upload. POW!

Phusion Passenger — a.k.a. mod_rails — makes deployment of applications built on the revolutionary Ruby on Rails web framework a breeze. It follows the usual Ruby on Rails conventions, such as “Don’t-Repeat-Yourself”.

Installing Passenger

Another great thing about Passenger is that it’s stupidly easy to install.

gem install passenger
passenger-install-apache2-module

Then just follow the instructions that are displayed inside the terminal. It shouldn’t take more than 5-10 minutes max to get this shit on the road.

Passenger Meet Capistrano

You are using capistrano, aren’t you? Capistrano is a great tool, by Jamis Buck, for deploying your applications. It takes all the monotonous stuff and does it for you, which is nice.

If you’re not using capistrano, you can install it with the following:

gem install capistrano

Next, go to the root directory of your application and type:

capify .

This will set up your application for use with capistrano by creating a deploy.rb file and a Capfile. The deploy file is a recipe that will be used every time you deploy your application. Now lets look at making capistrano play with passenger.

The Deploy Recipe

I must confess something at this point: I’ve not actually tested this recipe yet as I’ve not had time. As far as I’m concerned it should work. If you find any problems let me know and I’ll fix them.

#############################################################
#	Application
#############################################################

set :application, "example"
set :deploy_to, "/var/www/#{application}"

#############################################################
#	Settings
#############################################################

default_run_options[:pty] = true
set :use_sudo, true

#############################################################
#	Servers
#############################################################

set :user, "jim"
set :domain, "example.com"
server domain, :app, :web
role :db, domain, :primary => true

#############################################################
#	Subversion
#############################################################

set :repository,  "http://www.example.com/svn/example"
set :svn_username, "jim"
set :svn_password, "password"
set :checkout, "export"

#############################################################
#	Passenger
#############################################################

namespace :passenger do
  desc "Restart Application"
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"
  end
end

after :deploy, "passenger:restart"

From what I’ve read, the last few lines are all you should need to restart your application. This will be called after all deploy calls so you shouldn’t have to worry about anything.

Long live passenger

Some Stuff to Read

Here’s a list of a few things that are worth reading regarding capistrano and/or passenger.

Capistrano
Passenger (mod_rails)

(Possibly) Related Posts

  • No related posts

You can follow any responses to this entry through the RSS 2.0 feed. Trackback from your own site.

8 Responses to “Using Capistrano with Passenger (mod_rails)”

  1. May 12th, 2008 at 8:53 pm - This Week in Ruby (May 12, 2008) | Zen and the Art of Programming Says:

    [...] Ron Valente has a guide on setting up a Rails server through Ubuntu 8.04 using Passenger. While Jim Neath, published Using Capistrano with Passenger (mod_rails). [...]


  2. May 14th, 2008 at 6:49 pm - Tom Copeland Says:

    Good one Jim, thanks! The only thing I do differently is put that Capistrano restart task under the deploy namespace:
    http://tomcopeland.blogs.com/juniordeveloper/2008/05/mod_rails-and-c.html


  3. May 23rd, 2008 at 9:37 pm - 28 mod_rails / Passenger Resources To Help You Deploy Rails Applications Faster Says:

    [...] - Tom Copeland presents a simple Capistrano recipe for restarting mod_rails applications. Using Capistrano with Passenger - A slightly deeper guide to using Capistrano to deploy mod_rails applications. Installing Typo [...]


  4. June 7th, 2008 at 1:01 pm - Jim Neath :: Using Capistrano with Passenger [ RubyCorner ] Says:

    [...] 12th 2008 9:24am [-] From: jimneath.org [...]


  5. June 12th, 2008 at 12:35 am - redconfetti Says:

    I tried this and Capistrano attempted to use the user account is logged into SSH to connect to the subversion repository. I had to add this to deploy.rb:

    set :scm_username, “username”
    set :scm_password, “password”

    Once this was done, it gave me an error about executing script/process/reaper, but I see that the release was still created on the server, and the ‘current’ symlink updated. Just thought I’d point this out for those who have a problem.


  6. July 29th, 2008 at 2:49 am - Brendon Says:

    In response to redconfetti and also to the author:

    It seems (perhaps if you have an older setup of rails that has been upgraded a few times) that you’ll have a reaper and spawner and other scripts in the script/process/ directory. Capistrano will try to run reaper on a deploy if it exists. I suppose there are two ways around this, first just delete reaper (I didn’t test this), or taking a leaf from the mongrel_cluster gem, override what deploy.restart does. To do this just replace the entire Passenger section in the above code with:

    #############################################################
    # Passenger
    #############################################################

    namespace :passenger do
    desc “Restart Application”
    task :restart, :roles => :app do
    run “touch #{current_path}/tmp/restart.txt”
    end
    end

    namespace :deploy do
    desc “Restart the Passenger system.”
    task :restart, :roles => :app do
    passenger.restart
    end
    end

    ———————————–

    This way you don’t even have to call a passenger restart, it’ll just happen as part of a normal deploy.


  7. July 29th, 2008 at 2:58 am - Brendon Says:

    Also found a more elegant way of doing this which also works for a cold start:

    namespace :deploy do
    %w(start restart).each { |name| task name, :roles => :app do passenger.restart end }
    end

    See:

    http://snippets.dzone.com/posts/show/5466


  8. August 14th, 2008 at 11:55 am - Jerry Cheung Says:

    I’m also really enjoying the ease of use of Passenger. I wish I had come across your site earlier for the Capistrano advice. I had a pretty nasty time figuring out to set default_run_options[:pty] to true. Here’s the story:

    http://www.whatcodecraves.com/articles/2008/08/12/picking_at_capistrano

    Another little useful tool for OS X is the Passenger prefpane. It’s not hard to do the Apache configs yourself, but I liked having this for development.


Leave a Reply

Jim Neath is a 23 year old Ruby on Rails developer from Manchester, UK. Contact Jim Neath

Recommend Me

Categories

Stalk Me