Swing Sinatra onto a VPS: A Definitive Tutorial

I’ve recently became a fanatic for the sinatra framework and trying to deploy my first app was a nightmare. I have gathered bits of crappy tutorials into a real…full…complete walkthrough on getting a fresh VPS to host your sinatra app. We’ll be looking at lighttpd as a reverse proxy with thin and also discuss the potentail use of nginx.

Bold text needs to customized by you. Let’s go!

Step One: Initial Server Setup with Ubuntu

Step Two: Install RVM

$ command curl -sSL https://rvm.io/mpapis.asc | gpg --import -
$ \curl -sSL https://get.rvm.io | bash -s stable

As stated on their site, RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems. Pretty neat-o!

Now to run RVM in the sys env, aka anywhere on the server:

$ source /home/<user name>/.rvm/scripts/rvm

Step Three: Install Ruby 2.2.0 from source

$ rvm reload; rvm install 2.2.0

Step Four: Install Sinatra Thin and Lighthttpd

$ gem install sinatra thin; sudo apt-get install lighttpd

Step Five: Setup a Dummy Sinatra App

Before we setup our application template we need to ensure we set permissions for access.

$ sudo mkdir /var/www
# Set folder permissions
$ sudo chmod -R g+rwX /var/www
# Permissions will affect future sub-directories
$ sudo chmod g+s /var/www

Now for the app layout:

$ mkdir /var/www/my_app
$ cd /var/www/my_app
$ mkdir public
$ mkdir views
$ mkdir pids
$ mkdir logs
$ mkdir tmp; touch tmp/restart.txt

This is the expected layout by application servers, should give a nice starting structure to build upon ☺

Now we make our application in app.rb with your favourite text editor

require 'sinatra'
get '/' do
"Sinatra has taken the stage."

Now we make a config.ru file so the servers can run our program.

require File.expand_path('../app.rb', __FILE__)
run Sinatra::Application

Next is the Gemfile to ensure we have the needed gems loaded.

source 'https://rubygems.org'
gem 'sinatra'
gem 'thin'

We can now preform and installation of those gems with:

$ bundle install

Step Six: Config Thin

Create a config.yml. Be sure to update what is in BOLD.

environment: production
chdir: /var/www/my_app
user: root
group: root
port: 4567
pid: /var/www/my_app/pids/thin.pid
rackup: /var/www/my_app/config.ru
log: /var/www/my_app/logs/thin.log
max_conns: 1024
timeout: 30
max_persistent_conns: 512
daemonize: true

Step Seven: Config lighttpd

Replace /etc/lighttpd/lighttpd.conf with the following contents. Be sure to update what is in BOLD.

server.document-root = "/var/www/my_app/public"  
server.modules = (
$HTTP["host"] =~ "(www\.)?mydomain\.com"  {
proxy.balance = "fair"
proxy.server = ("/" => (
( "host" => "", "port" => 4567 ),
( "host" => "", "port" => 4568 )
) }

Stop lighttpd

$ sudo /etc/init.d/lighttpd stop
# if lighttpd persists track it down with:
$ sudo fuser -v 80/tcp
# or if you are feeling angry
$ sudo killall lighttpd

Start lighttpd

$ sudo /etc/init.d/lighttpd start

Step Eight: Spin up Thin

$ thin -s 2 -C config.yml -R config.ru start

This will launch two 2 thin instances using the config files we have in our directory!

Step Nine: Take the Stage

Assuming your DNS | Nameservers | Domain are setup correctly you should now be styling with sinatra!

Step Bonus: nginx via proxy example

upstream www_mydomain_com {

server {
listen www.mydomain.com:80;
server_name www.mydomain.com live;
access_log /path/to/logfile.log;

location / {
proxy_pass http://www_mydomain_com;



Incomplete sinatra recipe

Basic Installation and app structure

Various others lighttpd and thin posts

questions @ alessandro.minali@gmail.com

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.