A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Tuesday, August 11, 2009

Sinatra and Passenger

It is easy to run a Sinatra application under the Phusion Passenger and Apache web server, but the configuration is not especially explicit.

I am assuming that you have Passenger already installed.

1: Set up your Sinatra application and test it on localhost - try something very simple for the purposes of getting it running under Passenger.

2: Create a file called config.ru in the same directory as the Sinatra app containing something like this, where 'yourapp' refers to your application in 'yourapp.rb'
require 'yourapp'
set :environment, :production
run Sinatra::Application
I find the choice of the file name config.ru confusing, but there you go.

3: Create a 'public' directory in the directory with your app, as well as 'tmp' and 'log' directories. They can be empty for a simple application - but you need 'public'
# mkdir public
# mkdir tmp
# mkdir log

4: Edit your apache2.conf (or httpd.conf) file

You may need to add a PassengerDefaultUser line right after your other Passenger lines.
PassengerDefaultUser root
And then you want a Virtual host block for the Sinatra service that looks something like this:
<VirtualHost *:80>
DocumentRoot /yourdir/public
ErrorLog /yourdir/log/sinatra_error_log
CustomLog /yourdir/log/sinatra_access_log common
<Directory /yourdir/public>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all

You might want to 'touch' the two log files in your app log directory so they exist before apache wants to use them. In the past, at least, not having the log files in place would cause Apache to complain.

5. Restart Apache
This is a full restart
# /etc/init.d/apache2 restart
For changes to your application just use the standard Passenger reload by touching the file restart.txt in your tmp directory
# touch /yourdir/tmp/restsrt.txt

It is the presence of the empty public directory below your Sinatra app directory that tells Passenger that you have an app there. Notice that you are not telling it explicitly that you have a Sinatra app in the Apache conf file. Passenger looks one level up from this directory, sees the config.ru file and executes that.

Rather cryptic for my taste - but admittedly simple once you know how - and now you do...


No comments:

Archive of Tips