Deploying Sinatra on Dreamhost With Passenger

Sinatra Folder Structure on PassengerFigure #1: Example Directory StructureSinatra is a DSL for quickly creating web-applications in Ruby with minimal effort, as quoted from the Sinatra website. It is great for really simple, really fast services and in general is fun to make apps with. Since I showed how to deploy your Rails apps on Dreamhost, I thought I would also cover how to deploy your Sinatra apps as well.

The prerequisite of this article is Git’n Your Shared Host On, as you’ll need to have your code in source control in order to deploy nicely with the Capistrano file I provide below.

1. Create Your Site

The first step to deploying a Sinatra app is identical to deploying a Rails app. Go ahead and pop over to my Rails/Dreamhost/Passenger article for step 1.

2. Capify Your App

Step two is nearly the same. cd into your sinatra apps directory and run capify like so:

$ cd yourapp
$ capify .

Now open up the newly created Capfile in the root of your project and replace it with the following:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator

default_run_options[:pty] = true

# be sure to change these
set :user, 'username'
set :domain, 'yourdomain.com'
set :application, 'yourapp'

# the rest should be good
set :repository,  "#{user}@#{domain}:git/#{application}.git" 
set :deploy_to, "/home/#{user}/#{domain}"
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false

server domain, :app, :web

namespace :deploy do
  task :restart do
    run "touch #{current_path}/tmp/restart.txt" 
  end
end

The file looks eerily similar to our Rails one but instead of putting all the guts in config/deploy.rb, we are just going to put it in the Capfile. Again, note that we overwrite the deploy:restart task to touch the restart.txt file.

3. Vendor Sinatra

At the time of this post, Sinatra was not installed on Dreamhost, so we’ll have to vendor it in our app. This is really simple.

$ mkdir vendor
$ cd vendor
$ gem unpack sinatra
$ mv sinatra-* sinatra

4. Rackup, Setup and Deploy

In order for Passenger to know what to do with your Sinatra app, you have to create a rackup file named config.ru in the root of your app. The file should look something like this:

require 'rubygems'
require 'vendor/sinatra/lib/sinatra.rb'

Sinatra::Application.default_options.merge!(
  :run => false,
  :env => :production
)

require 'yourapp.rb'
run Sinatra.application

The key line is require 'yourapp.rb'. Be sure to change that to whatever your application’s name is. At this point, be sure to commit and push to your remote. You now have everything in place and it is time to setup and deploy, just like Rails.

$ cap deploy:setup
$ cap deploy

That is it. Your sinatra app should be up and running. One thing to note is be sure that you don’t have require sinatra in yourapp.rb, it will cause problems. Instead, the top of yourapp.rb should look something like this:

require 'rubygems'
require 'vendor/sinatra/lib/sinatra.rb'

One other thing I discovered while playing around with this is you can start your app up locally, with the rackup command.

rackup config.ru

This will start your app on localhost:9292. Thought that was worth noting as well. Hope this helps people venture out of Rails occasionally and into other enlightened ruby web frameworks.

5 Comments

  1. Excellent writeup. I haven’t used Capistrano to deploy Sinatra, but I will from now on. :)

    One note: I’ve had issues with Dreamhost not finding my erb templates (where I store views). I had to add this to my config.ru file:

    
    Sinatra::Application.default_options.merge!(
      :run => false,
      :env => :production,
      :views => '/home/USERNAME/myapp.domain.com/views'
    )
    

    (change USERNAME and domain name, of course)

  2. Good point Chas. Also, I should mention that Chas was the one who spent all the hard time on getting this working. He emailed me some examples and I whipped the Capistrano version together for here. I forgot to mention him in the post (sorry). Snaps for Chas! :)

  3. Today I was writing a small Sinatra app with ActiveRecord for the db stuff. Since I wanted different environments, I set my AR::Base.establish_connection() calls in a configure do… block for each one.

    When started from the command line (running mongrel) everything works as expected, but when run via Passenger (2.0.6, Leopard, Passenger Pane) ActiveRecord can’t find the connections.

    Anyone else encountered such problems?

  4. Haye, thanks for the post. I was having some difficulty while deploying Sinatra but your post helped me out.

  5. Thanks for these instructions! I don’t think I would have attempted to try Sinatra on DH without them. Unfortunately I still had an hour of frustration trying to get past the dreaded generic-500-error page.

    First, to get any info at all about the error was difficult. Nothing got written to any log file I could find. The app’s log directory is empty, and the Apache error.log has nothing in it. I suspect this might be because Sinatra wants to display the error itself, but fails.

    I added a ‘rescue’ clause to my “get / do” block and had it print the exception. The gist of it is that it’s a file-not-found error trying to look for an ERB template at a totally nonsensical path. And the reason for this path is that $0, the name of the process, is this made-up string "Rack: " followed by the path to the release. This totally confuses Sinatra when it tries to determine its root directory in the default_options method, by calling “File.expand_path(File.dirname($0))”.

    It was only after getting to that point that I read Chas’s comment with new eyes, and understood what he meant. I found that merely setting :views to the string “views” works fine, since the CWD is already set to the app directory.

    Could you please, please add this line to the post itself, to spare others the frustration I went through? Thanks!

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.