zerosum dirt(nap)

evolution through a series of accidents

zerosum dirt(nap)

Passenger <3 Sinatra

July 03, 2008 @ 09:26 PM by nap · 5 comments

A couple people have asked me how I'm hosting the Sinatra-based pastie service we wrote in yesterday's revised tutorial. The previous version ran on a mongrel handler frontended by nginx, but for this version I decided to try something a little different.

One of the big announcements at Railsconf last month was that Passenger (aka mod_rails) would be releasing a v2.0 with support not only for Rails applications but also for Rack, meaning that any Rack-based Ruby web framework can also run on it. Yay for deployment options, right? So anyway, I figured we'd give that a shot.

For those of you who haven't yet mucked with it, Passenger is dead simple to setup. Run gem install passenger to pull down the gem, and execute the passenger-install-apache2-module command to build and install the Apache 2.2 module (you'll need the proper Apache libraries to be present of course). The command output will show you how to configure Apache to load the module.

Getting Passenger to run a Sinatra-based application also turned out to be remarkably easy. All you need to do is create a regular old Rackup script. The file will need to be named config.ru and should contain all the logic necessary to initialize our app:

require 'rubygems'
require 'sinatra'

Sinatra::Application.default_options.merge!(
  :run => false,
  :env => ENV['RACK_ENV']
)

require 'toopaste'
run Sinatra.application

Place this file in the folder on your server where toopaste.rb and the views directory reside.

Next, create a public directory. This is where any static images, JavaScripts, or stylesheets would be kept (we're not using any, in this simple example). Point your vhost's DocumentRoot here:

<VirtualHost *:80>
  ServerName paste.zerosum.org
  DocumentRoot /var/www/apps/toopaste/public
  ...
</VirtualHost>

You might as well create a tmp directory too. You can place a restart.txt file in this directory to tell Passenger it needs to reload the app without restarting Apache (you can use this in your cap restart tasks, too).

Couldn't be much easier. For more information on deploying other Rack-based frameworks (Merb, Camping, Ramaze, etc) and various other config options, check Passenger's user guide.

5 comments so far ↓

  • Simone // July 06, 2008 @ 08:40 AM

    Hi, interesting post. I'm trying to do the same but I always receive 403 from apache.

    At the first request it takes a bit more to return 403 so I imagine that passenger takes place but I couldn't debug what cause 403. Do you have any idea?

    Passenger is working infact I have some rails app running with it.

    Thanks

  • Keith // July 15, 2008 @ 02:47 AM

    It looks like the gem doesn't support this, you need to use the edge version of Sinatra. Once I added the latest Github version, it works like a charm.

    Just follow the instructions for added the latest version on the "Sinatra website":http://sinatrarb.com

  • David // November 28, 2008 @ 06:45 AM

    Very helpful, thank you. Got Sinatra integrated with my Rails app. in about 60 seconds!

  • jimeh // July 08, 2009 @ 08:37 PM

    Hey, the example config.ru file seems to be out of date here.

    I just had a got at getting the latest version of Sinatra to run under Passenger, and I had to use a config.ru file that looked like this:

    require 'rubygems' require 'sinatra'

    set :run, false set :environment, :development

    require 'myapp' run Sinatra::Application

  • nap // July 11, 2009 @ 11:40 AM

    @jimeh -- yes, the config.ru shown here is out of date now. See http://modrails.com/documentation/Users%20guide.html#_sinatra for the latest info.

Leave a Comment