Sinatra Framework and nginx on Debian 6 (Squeeze)

Updated by Linode Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

This guide has been deprecated and is no longer being maintained.

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

Issue the following commands to update your system’s package database and all installed packages:

apt-get update
apt-get upgrade

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:

# [...]
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:

 1 2 3 4 5 6 7 8 910111213141516
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.

 1 2 3 4 5 6 7 8 91011121314
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 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.

See Also

comments powered by Disqus

This guide is published under a CC BY-ND 4.0 license.