Linode Library Home
Linode Library RSS Feed
Home :: Frameworks :: Sinatra
Print View View Source

Sinatra Framework and nginx on Debian 5 (Lenny)

Published: by


This document is an older, unmaintained guide. There may be a new guide available for this software.

Sinatra is a simple lightweight framework for web application development in the Ruby programming language. Rather than providing a complete development system, Sinatra provides a basic URL-mapping system that developers can use to create powerful custom applications.


Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install Software

To install a more current version of Ruby Gems, which is required for running Sinatra Applications on Debian 5 (Lenny), you must install several packages from the Backports project. Insert the following line in your /etc/apt/sources.list file:

File excerpt:/etc/apt/sources.list

deb lenny-backports main

Issue the following commands to update your system's package database and all installed packages, and install the backports repository's key:

apt-get update
apt-get upgrade
apt-get install debian-backports-keyring

Add the following snippet to the /etc/apt/preferences file (you may need to create it):

File excerpt:/etc/apt/preferences

Package: rubygems
Pin: release a=lenny-backports
Pin-Priority: 999

Package: rubygems1.8
Pin: release a=lenny-backports
Pin-Priority: 999

Issue the following command to install Ruby dependencies for Sinatra.

apt-get install wget build-essential ruby1.8 ruby1.8-dev irb1.8 rdoc1.8 zlib1g-dev libopenssl-ruby1.8 rubygems1.8 libopenssl-ruby libzlib-ruby libssl-dev libpcre3-dev  libcurl4-openssl-dev

Create symbolic links to the installed version of Ruby:

ln -s /usr/bin/ruby1.8 /usr/bin/ruby
ln -s /usr/bin/irb1.8 /usr/bin/irb

Install the rack, rake and sinatra gems:

gem install rack rake sinatra

Your application may require additional dependencies. If this is the case, install these gems at this point using the "gem install" tool.

Install Nginx

Proceed to the Phusion Passenger site and locate the link for the current source code tarball. Download it as follows (substitute the link for the current version):

cd /opt
tar xzvf passenger-3.0.1.tar.gz

Run the Phusion Passenger installer for Nginx:

cd /opt/passenger-3.0.1/bin

Press "Enter" to continue with the installation. When prompted for the Nginx installation method, we recommend you choose "1" to allow the installer to automatically download, compile, and install Nginx for you. Unless you have specific needs that would necessitate passing custom options to Nginx at compile time, this is the safest way to proceed.

Do not remove the Passenger files from opt after the install. They need to stay in place or your install will not function correctly.

Configure Web Server

Nginx is now installed in /opt/nginx, but there are no "init" scripts to control this process. Issue the following sequence of commands to download a script, move it to the proper directory, set the proper permissions and set system startup links:

cd /opt
wget -O
mv /opt/ /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults

You can now start, stop, and restart Nginx using the following commands:

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart

To start the web server for the first time, issue the following command:

/etc/init.d/nginx start

Create the following directories beneath the "/srv/www" hierarchy for your application. Modify to match the domain of the site you are deploying:

mkdir -p /srv/www/
mkdir -p /srv/www/
mkdir -p /srv/www/
mkdir -p /srv/www/
mkdir -p /srv/www/
mkdir -p /srv/www/

Insert the following line into the "/opt/nginx/conf/nginx.conf" file, modifying the path for /srv/www/ to match the directory created above:

File excerpt:/opt/nginx/conf/nginx.conf

# [...]
http {
    include /srv/www/;
    passenger_root /opt/passenger-3.0.1;
    passenger_ruby /usr/bin/ruby1.8;
# [...]

This inserts the contents of "/srv/www/" into your nginx configuration, and allows you to specify the configuration of the virtual host for the site. Consider the following example configuration, and modify this file to meet the needs of your deployment:


server {
        listen 80;

    access_log /srv/www/;
        error_log /srv/www/;

        root /srv/www/;
        passenger_enabled on;

    location /static {
            root   /srv/www/;
            index  index.html index.htm index.php;


Your Sinatra application will handle all requests for the and domains, except those that begin with /static which are handled directly by nginx. When this configuration has been created and properly modified, issue the following command to restart the web server:

/etc/init.d/nginx restart

Create a Basic Sinatra Application

The following is a very basic Sinatra application. Place the following code in the /srv/www/ file.


require 'rubygems'
require 'sinatra'

get '/' do
  "Hello and Goodbye"

get '/hi' do
  "Hello World! :)"

get '/bye' do
  "Goodbye World! :("

Deploy Sinatra Applications with Rack

Create a Rack configuration file located at "/srv/www/" to allow Passenger to run your application properly. Deploy the following file:


require 'rubygems'
require 'sinatra'

require 'app'
run Sinatra::Application

The "require 'app'" statement references the app.rb file. Modify this line to require your application. Any time you make changes to your Rack file or your application, issue the following command so that Passenger will restart your application:

touch /srv/www/

You can now access your Sinatra application by visiting "``in your web browser. If you used the example application above, visit "``", "", and "" to view different messages.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Creative Commons License

This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.

Last edited by Matthew Cone on Monday, October 8th, 2012 (r3087).