Tuesday, February 17, 2009

Deploying Sinatra applications with Capi, nginx and Thin.

I've been working with Sinatra for less than a month and there are some things I have to say about it;

Sinatra is an awesome framework to build tiny web applications that helps you a lot to understand how a framework works, but the documentation it's almost reduced to the Sinatra Book and some blog's posts, I didn't find a full guide in how to deploy a Sinatra app + Capi + Thin + nginx, just clues from different sites that I had to link in order to make it work, and I'm gonna show you how I did it.

I assume you have everything installed, sinatra, rack, thin, nginx and capistrano, also I'm using Git as scm and github as hosting.

Another consideration, my user and application name is: 'acoplet', change it according to your situation, this is the application structure:


project_root
==Capfile
==config.ru
==public
==javascripts
==stylesheets
==thin
==production_config.yml
==tmp
==pids
==thin.pid
==views
==log

Step 1, create and configure the thin config file, usually for production purposes, this is how your production_config.yml file should looks like,


Change the chdir value with the location of your application, also for pid, rackup, and log according to the application structure, please note the /current/ it's capistrano!

Step 2, Create manually the Capfile, you can't use capify here since this is not a Rails app!


Again, change the repository, application, user, deploy_to and host variables according to your project, in my case I'm using a github repository, usually I put my application under ~/www/#{application}/  where each application has a related user with the same name (make things easy for me), in this case /acoplet/www/acoplet/ and then let capistrano make it's stuff.

Step 3, Create the config.ru file


At the moment of writting this post, this is the right way to edit the config.ru file, things like:


Are deprecated now, you have to replace the require 'acoplet', with your main file, the file when you put all the sexy code.

At this moment, you should be able to upload (if you want) your repository and start the deployment process, when done setting up the repository, let's start with the capistrano tasks!, run:

cap deploy:setup
cap deploy:check
cap deploy:cold


If everything went fine, the backend server for the sinatra application should be up and running in the remote host (in the port 8000 for the example) to check it, ssh the remote host and run:

curl -v localhost:8000


step 4, Nginx front server configuration, you have to create vhost for the application, do it unter the the sites-available directory in your nginx main nginx folder (probably /etc/ngninx/ depending in how you installed nginx). and then enable it through a symbolic link under the sites-available folder

The Nginx vhost file should looks like:


I don't have any domain registered for this, but if you have, you should setup your dns configuration and just change the server_name variable with your domain name, again change the location according to your needs, restarting nginx should be enough to let you see the nice Sinatra App up and running!

Hope this little guide help you with the deployment process with Sinatra!

4 comments:

indiehead said...

awesome stuff and thanks for posting this. keep up the good work.

linked in my blog => www.red91.com

jpemberthy said...

Thanks!

Peter said...

Pc games free
gta 4 free download

Sir Swapalot said...

Thanks a lot!!!

Just thought I'd add that I'm using RVM to manage gems and ruby versions, and that I needed to add:

set :default_environment, {
'PATH' => 'usr/local/rvm/gems/ruby-1.9.2-p318@testsinatradeploy/bin:/usr/local/rvm/gems/ruby-1.9.2-p318@global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p318/bin:$PATH',
'RUBY_VERSION' => '1.9.2p318',
'GEM_HOME' => '/usr/local/rvm/gems/ruby-1.9.2-p318@testsinatradeploy',
'GEM_PATH' => '/usr/local/rvm/gems/ruby-1.9.2-p318@testsinatradeploy:/usr/local/rvm/gems/ruby-1.9.2-p318@global'
}



To the top of my Capfile. Haven't yet done the Nginx bit.

- see StackOverflow for more details.