Deploy Sinatra app with Phusion Passenger+Apache+Rack

March 13th, 2009

Recently I’ve taken an interest in Sinatra. This lightweight framework/DSL tool allows you to build powerful ruby based web apps with very little code and very very minimal overhead. The only headache I ran into was deployment of my application. Here are the steps I took.

First and foremost I would suggest using http://www.modrails.com/ in combination with apache. These tools allow you to deploy multiple rack based applications on a single server. Phusion passenger does a really nice job of managing resources so that you can run multiple apps and they play together nicely. You can also run legacy things like php apps on the same server with tremendous ease.

Next create you sinatra application for a quick example :

test.rb:
require 'rubygems'
require 'sinatra'

get '/' do
'Hello World'
end

EOF

To run this type “ruby test.rb” point your browser to http://host:4567 and you should see “Hello world”.

Create the following folders in the same directory as your sinatra app.
“lib”,”public”,”tmp” and “views”. Next create a file called “config.ru” this is the configuration file that Rack uses to know how to run your application. Add the following.

configu.ru:
require 'rubygems'
require 'sinatra'
Sinatra::Application.default_options.merge!(
:run => false,
:enf => :production
)
require 'test.rb'
run Sinatra::Application
EOF

This file is a very basic example and probably not suitable for all applications. A nice feature I discovered is you can check the rack configuration by issuing the command “rackup config.ru”. This will attempt to launch your application on port 9292. Simply point your browser to http://host.com:9292 and you should see Hello World. If there are any configuration errors you can fix them before deployment.

Once you are satisfied with your config.ru the next step is to get it deployed with apache + passenger phusion.

Add a VirtualHost entry to your apache configuration. The key step in this is to point the DocumentRoot to the “public” directory you created earlier i.e. /path/to/app/public. *Be sure in apache to add a directory entry to that public directory or you will get issues!*
a sample entry would look like:

ServerAdmin webmaster@mysite.com
DocumentRoot "/path/to/myapp/public"
ServerName myserver.com
ErrorLog "/var/log/error_log"
CustomLog "/var/log/access_log" common

Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all


A sample application directory might look similar to the following:

./
../
./.git
./app.rb
./config.ru
./lib
./publc
./tmp
./views

After adding the VirturalHost entry restart apache and you should be able to point your browswer to the URL and see “Hello World”.

A further cool benefit of using this configuration is you can restart this particular application by creating an empty file in ./tmp called “restart.txt” and this lets you restart the application with out affecting other applications you might have running. Honestly it is either to build and test the application when deployed with apache in this manner than using ruby app.rb because you can simply edit the file and issue “touch tmp/restart.txt” and you’re done. This is just a personal opinion.

Good luck

Entry Filed under: General

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Issues

Click here to lend your support to: Dog & Dentist and make a donation at www.pledgie.com !

Related Posts

Calendar

March 2009
M T W T F S S
« Aug    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Most Recent Posts

Other


Computers Blog Top Sites
Google PageRank Checker Tool
  • Kansas City, Missouri
    • overcast
    • Temp: 43°F
    • Wind Chill: 38°F
    • Humidity: 76%
    • Wind: WSW at 9 mph
    • Dew Point: 36°F
    • Barometer: 29.60" Hg (1002 hPa)
    • Clouds: overcast
    • Visibility: 10 miles