Here's a quick tutorial showing how to deploy a Sinatra web application on MacOS X, with Apache web server using the Passenger plug-in.

Sinatra

Sinatra is a tiny web framework that seems to be gathering steam. It's claim to fame is its compact size, and a clear "no magic" approach to web development. Sign me up! Open a terminal and paste this to get started.

sudo gem install sinatra

Make and Deploy Sinatra's "Hello World!"

To ultimately deploy your app, you're going to have to make some extra folders and configuration files. Passenger is designed to deploy Rails apps, so to get Passenger to reload your changes, you have run 'touch tmp/restart.txt' in the terminal. Open the Terminal, and change directories to a good place to put your Sinatra application. Run the following commands in the Terminal to create the files and directories needed. I put everything in /Timestretch/ruby/hello/.

mkdir hello
cd hello
touch config.ru
touch hello.rb
mkdir public
mkdir tmp
touch tmp/restart.txt

Put this in "config.ru". Note this file has an unusual '.ru' extension. This file is a 'Rack Up' configuration file. Ruby Rack is a middleware that sits between the web server and web framework.

require 'hello'
run Hello

Put this in "hello.rb"

require 'rubygems' if RUBY_VERSION < '1.9'
require 'sinatra/base';

class Hello < Sinatra::Base
    get '/' do
        "Hello World!"
    end
end

Phusion Passenger

Passenger is a plug-in available for both Apache and Nginx web servers. It allows you to simply place the web application folder in the document root and serve up your web site. I used the Apache2 module.

sudo gem install passenger
sudo passenger-install-apache2-module

Passenger will print out a snippet to paste into the Apache configuration file. This is what mine output. Don't just copy mine. Use the output printed by the script.

Put the snippet that looks something like this in /etc/apache2/httpd.conf.

LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-3.0.7/ext/apache2/mod_passenger.so
PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-3.0.7
PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

Configure Apache

I also made up a test domain name and put it in /etc/hosts file. This lets me visit http://hello.timestretch.com/ in the browser and test the website as if a client were using it.

Add this to /etc/hosts

127.0.0.1        hello.timestretch.com

Open up the /etc/apache2/extra/httpd-vhosts.conf file and add the following. This will allow you to use http://hello.timestretch.com/ in your web browser to connect and test your web application.

<VirtualHost *:80>
    ServerName hello.timestretch.com
    DocumentRoot /Timestretch/ruby/hello/public
    <Directory /Timestretch/ruby/hello>
        AllowOverride all
        Options -MultiViews
    Order deny,allow
    Deny from all
    Allow from localhost
    Allow from 127.0.0.1
    </Directory>
</VirtualHost>

Now that everything is all set, check the apache configuration file. Fix any typos or errors it reports. Then restart Apache.

apachectl configtest
sudo apachectl graceful

Now you can visit the url http://hello.timestretch.com/ in the web browser. If all went well, you will see "Hello World!"

If you didn't get the results you expected, check the error log in /var/log/apache2/error_log and reload your page.

References

Tags: sinatra, passenger, apache2, ruby, deployment, tutorial