Deploying Sinatra Apps on Dreamhost

09 Oct 2008
So I LOVE creating small apps as a way of trying out new things. The problem is that I rarely deploy them anywhere. Many end up sitting in my
/workspace
directory until I decide I need to reclaim the space and probably won't work on it ever again. Recently, the Dallas Relevance folks have been meeting at Panera while we wait for our office space to materialize. Most of the time everything works out nicely: free wifi, decent coffee, plenty of room to spread out. But one thing that doesn't work correctly while at Panera is tinyurl.com. For some reason, tinyurl is blocked by Panera filters. This would be fine for most, but since my twitter friends insist on using tinyurl to post links in their tweets, it is annoying not being able to see what is going on.

Microapp to the Rescue

So I figured this would be an opportunity for simple little that let me enter the tinyurl, figure out where it was going to redirect to, and go ahead and redirect me there. This way I am never accessing the evil tinyurl.com directly from the Panera network, but instead letting my little app do that for me. The app was pretty easy to write. I used Sinatra and created one "controller" and one "view". Within a few minutes, Sinatra had "taken the stage on port 4567" and my app was working. Locally.

Give the app a home

The next challenge came when I tried to deploy it to my trusty Dreamhost account. I love Dreamhost for playing around with small apps. You get unlimited domains, they have a pretty cool admin control panel, and they support deploying Rails applications with Passenger Phusion. And since Passenger Phusion 2.0 supports Rack enabled Ruby apps, I knew I should be able to deploy this new app to my Dreamhost account. A quick search turned up a useful post with information on deploying to Sinatra apps on Dreamhost. Unfortunately, the first attempt didn't work.

Always check the logs

So I went to the logs to see what went wrong... wait there weren't any logs! Fortunately I found this post for logging with Sinatra apps. Unfortunately, the logs didn't help. Apparently, my problem ran deeper. So like all good debuggers, I started commenting out code and printing out where I was in the app. The first thing I commented out was the called to render the view using ERB. Turns out you can configure where the root of the app is located. Apparently the root path for a Sinatra app running on Dreamhost is not exactly the path where you deployed it.
Sinatra::Application.default_options[:root]
looked like this:
/home/.machinename/username/app.domain.com/Rack: /home/.machinename/username

Additional configuration needed

Looking through the Sinatra source turned up the needed configuration changes need:
path = "/path/to/app"

Sinatra::Application.default_options.merge!(
  :root => path,
  :views => path + '/views',
  :public => path + '/public',
  :run => false,
  :env => :production
)
A quick deploy later and the application was up and running. Tomorrow we are meeting at Panera and I will get to see what everyone is tweeting about.

Addendum: Deployment too?

Since Sinatra apps are so small, you could just copy everything up to the server manually. But I like have a little Rake task to do that for me. It just touches the
tmp/restart.txt
that Passenger uses to know when to restart and then uses rsync to copy the files up to the server.
desc 'Deploy to the server using rsync'
task :restart do
  sh "touch tmp/restart.txt"
end

desc 'Deploy to the server using rsync'
task :deploy => :restart do
  cmd = "rsync -ruv * #{USERNAME}@#{DOMAIN}:#{DEPLOY_PATH}"
  sh cmd
end

Take a look at the code

I have posted the code on Github for everyone to take a look at. Enjoy.

blog comments powered by Disqus