Setting up Unicorn with Nginx on Ruby on Rails

In this blog post, I am going to tell you, How to configure Unicorn web sever on top of Nginx. I Assume that you already installed Ruby and Ruby on Rails on your production OS(operating system) and a sample application working fine.!! If not, follow my previous blog post Setup Ruby On Rails on Ubuntu.

Nginx and Unicorn

To get you started, Let’s install Nginx first by using apt-get package manager.

$ sudo apt-get install nginx

Sometime you get ERROR because your machine packages are not upto date. Run apt-get update to downloads the package lists from the repositories and “updates” them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPAs.

$ sudo apt-get update

Once your nignx will install, you command window will prompt:

Setting up nginx-full (1.2.1-2.2ubuntu0.1) ...
Setting up nginx (1.2.1-2.2ubuntu0.1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
Processing triggers for sgml-base ...
Updating the super catalog...

At this point switch to /etc/nginx/ and /etc/nginx/sites-available configuration directory and have a look to nginx.conf and default respective configuration file. It’s self explanatory under commented in-lines. If still you need more details. I will recommend you to check wiki.nginx for more info.

While the server configuration it’s always better to create separate user(which we have more control over than nobody) for security reasons and increased control by using below command:

$ sudo useradd -s /sbin/nologin -r nginx
$ sudo usermod -a -G web nginx

Starting/stopping/restarting Nginx is pretty straight forward.

$ sudo service nginx start/restart/stop

Now let’s install Unicorn, Unicorn is distributed as ruby gem So you can install vi ruby gem.

$ gem install unicorn

Once Unicorn installed successfully. We need to create two configuration file.

1.Replace /etc/nginx/sites-available/ default file with below code:

upstream shop {
  server unix:/tmp/ fail_timeout=0;

server {
   listen 80;# default deferred;
   server_name localhost;
   root /var/www/shop/public;
   try_files $uri/index.html $uri @shop;

location @shop {
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $http_host;
   proxy_redirect off;
   proxy_pass http://shop;

error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
   keepalive_timeout 10;

2. Create a unicorn.config file in your Rails application configuration file with below code:

working_directory "/var/www/shop"
pid "/var/www/shop/tmp/pids/"
stderr_path "/var/www/shop/log/unicorn.log"
stdout_path "/var/www/shop/log/unicorn.log"

listen “/tmp/”
worker_processes 2
timeout 30

You have to tweak a few things to set the right paths in above configuration. Change “/var/www/shop” with your rails application directory.

In my case my Rails application is in /var/www/ with shop name.

Before start your rails application, make sure to include unicorn gem in your Gemfile and run bundle to install all dependencies.

That’s all.

Setup Ruby On Rails on Ubuntu

Installation of Ruby on Rails for Production server is pretty straight forward. Below are the relevant steps.


sudo apt-get install zlib1g zlib1g-dev build-essential openssl libssl-dev libmysqlclient18 libmysqlclient-dev libyaml-dev


sudo apt-get update
 sudo apt-get install ruby1.9.3

Note you can install latest Ruby too.


 sudo apt-get install ruby-dev
 sudo apt-get install build-essential g++
 sudo gem install railsconfigure rails application with nginx and unicorn


 sudo apt-get install libsqlite3-dev

If you passed all above steps without any ERROR, Now it’s time to create a rails demo application OR SCP existing one and test it out.

Below is the steps to create new rails application

 rails new demo
 cd demo
 uncomment therubyracer gem in Gemfile
 sudo apt-get install build-essential g++
 bundle install
 rails g scaffold order name:string
 rake db:create
 rake db:migrate

Open config/routes file and uncomment root ‘welcome/index’ and replaced with ‘orders/index’

$rm -rf public/index

That’s all, Now run your Rails application.

Some insights into PaaS market

Platform as a Service is a one of the GROWING sector of cloud computing. PaaS basically help developer to speed the development of app, saving money and most important innovating their applications and business instead of setting up configurations and managing things like servers and databases.

Other features buying to use PaaS is the application deployment process such as agility, High Availability, Monitoring, Scale / Descale, limited need for expertise, easy deployment, and reduced cost and development time.

PaaS Market

Major forces driving the PaaS

– Pay as you Go
– Low start up cost
– Leave the plumbing to expert
– PaaS handles auto scaling/descaling, Load blancing, disaster recovery
– PaaS manages all security requirements
– PaaS manages reliability, High Availability
– Paas manages manay third party addon’s for you

Barrier to PaaS adoption?

– Less Control over Server and databases
– Have to be expert to mange security controls and audits
– Costs will be very high if not governed properly
– Premature and dobious in current day and age

Major PaaS providers are Heroku, App42 PaaS , Jelastic, and Engine Yard. When we talk about revenue, The global PaaS market is estimated to grow from $1.28 billion in 2013 to $6.94 billion in 2018 at a compound annual growth rate (CAGR) of 32.54% in this period. In terms of geographies, North America continues to be the biggest market for PaaS solutions. In 2012, PaaS revenues ($1.2 billion) was the tenth of the size of SaaS ($14.4 billion), a fifth of IaaS ($6.2 billion), and just a tiny fraction of BPaaS ($84.1 billion).

PaaS has always taken a very small space in the cloud computing arena as compared to the other two segments: IaaS and SaaS. But the trend has recently shown a drift with PaaS market showing a very high growth rate in terms of revenue. Though it is still not as huge as the other two segments but now holds a significant proportion of the pie. PaaS has now been adopted by most of the big cloud computing and IT solution providers like Amazon, IBM, Google etc. as one of their main services.. Many small players have also emerged and made the market very dynamic and competitive. Application developers are benefiting from this fact resulting in more adoption and thus increasing the demand for PaaS all the more in various sectors.

We expect to see more and more application development companies choosing PaaS over IaaS or traditional Hosting, as they can then focus on driving innovation and building apps that change their interactions with customers, partners and community. It will make them free of the details of infrastructure, so they can push the possibilities of the latest technology to build great web and mobile applications.