Running Capistrano with Passenger (mod_rails)

The following recipe enables you to deploy on a Passenger (mod_Rails) server with Capistrano. It's a ready to use boilerplate, you just need to store it somewhere in your rails project (for example in the config/deploy folder) and require it from your deploy.rb file.

#
# = Capistrano Passenger deploy tasks
#
# Provides tasks for deploying a Rails application with Passenger (aka mod_rails).
#
# Category::    Capistrano
# Package::     Passenger
# Author::      Simone Carletti
# Copyright::   2007-2008 The Authors
# License::     MIT License
# Link::        http://simonecarletti.com/
# Source::      https://gist.github.com/2769
#
#

unless Capistrano::Configuration.respond_to?(:instance)
  abort "This extension requires Capistrano 2"
end

Capistrano::Configuration.instance.load do

  namespace :passenger do

    desc <<-DESC
      Restarts your application. \
      This works by creating an empty `restart.txt` file in the `tmp` folder
      as requested by Passenger server.
    DESC
    task :restart, :roles => :app, :except => { :no_release => true } do
      run "touch #{current_path}/tmp/restart.txt"
    end

    desc <<-DESC
      Starts the application servers. \
      Please note that this task is not supported by Passenger server.
    DESC
    task :start, :roles => :app do
      logger.info ":start task not supported by Passenger server"
    end

    desc <<-DESC
      Stops the application servers. \
      Please note that this task is not supported by Passenger server.
    DESC
    task :stop, :roles => :app do
      logger.info ":stop task not supported by Passenger server"
    end

  end

  namespace :deploy do

    desc <<-DESC
      Restarts your application. \
      Overwrites default :restart task for Passenger server.
    DESC
    task :restart, :roles => :app, :except => { :no_release => true } do
      passenger.restart
    end

    desc <<-DESC
      Starts the application servers. \
      Overwrites default :start task for Passenger server.
    DESC
    task :start, :roles => :app do
      passenger.start
    end

    desc <<-DESC
      Stops the application servers. \
      Overwrites default :start task for Passenger server.
    DESC
    task :stop, :roles => :app do
      passenger.stop
    end

  end

end

This extension creates the following Capistrano tasks under the passenger namespace:

  • start
  • stop
  • restart

Additionally, it overwrites the default Capistrano deploy:start/stop/restart tasks to fully integrate Passenger into your current deployment strategy.

Please note that, due to Passenger architecture, only restart task is supported. start and stop task are available only for compatibility purpose. Invoking deploy:start or deploy:stop will simply return a warning.

The most recent version of this recipe is available as a Gist (#3102).