Getting Sinatra, Apache 2, and Phusion Passenger working together on Ubuntu 10.04

Post to Twitter

Today I’m going to go over the steps you need to take to get a Sinatra web app working on Apache 2 with Phusion Passenger (mod_rails). If you haven’t done so already make sure to read my previous article on how to get everything up and running on Ubuntu first.


You can install Sinatra very easily with the following command:

$ gem install sinatra

Now before we do anything lets ensure everything so far is working (assuming you have used my previous article as a starting point). In my home folder I created the following folder structure:

Create a similar structure, name it as you wish but you will need to modify accordingly of course. You can also read about the project structure here. Under the sinatra-project folder I created a file called config.ru. The contents of config.ru are as follows:

app = proc do |env|
    return [200, { "Content-Type" => "text/html" }, "Hello World"]
end
run app

Next you need to open your Apache 2′s httpd.conf file (mine was in /etc/apache2) and modify the contents like so:

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /home/chadlung/repository/sinatra-project/public
</VirtualHost>

Note: There are several ways you can set this up, I simply did an easy and fast way. Obviously you will need to configure your Apache system as to what suits your needs best.

We now need to restart Apache to get this change recognized:

$ sudo /etc/init.d/apache2 restart

For non-Apache changes simply restart Passenger (mod_rails) you can just do this: restart Passenger. So in my case it would be:

$ touch sinatra-project/tmp/restart.txt

Start your web browser and point to localhost.

From inside the sinatra-project folder run this command:

$ touch hello.rb

Now enter this code into the new file:

require 'rubygems'
require 'sinatra'

get '/' do
  "Hello Sinatra"
end

Go into the config.ru and change it to this:

require 'hello'

root_dir = File.dirname(__FILE__)

set :environment, ENV['RACK_ENV'].to_sym
set :root,        root_dir
set :app_file,    File.join(root_dir, 'hello.rb')
disable :run

run Sinatra::Application

Restart Passenger or restart Apache and then refresh your browser.

Thats all you need for a minimal Sinatra app running under Apache 2 and Passenger.

Post to Twitter

This entry was posted in Open Source, Ruby, Sinatra, Ubuntu. Bookmark the permalink.

Comments are closed.