Saturday, 2 April 2016

Setup Nginx with Puma on Ubuntu



Hope you enjoyed our previous blog for setting up the rails app using Nginx and Unicorn .
In this series, today we are going to cover Puma . Puma has gained quite popularity among rails developer in recent time so we thought, its worth writing about puma as Rails Developer. 

Why Puma ??


As the rails framework got matured over the time, its app servers also got evolved. There are couple of options available when you look for deploying your rails app. Passenger, Unicorn, Thin and Puma. Puma got quite popular over last year. Heroku also recommend using puma for its Cedar platform.
Puma is fast, threaded and highly concurrent server. For best throughput it is recommend to use Ruby implementation with real thread like Rubinius or JRuby. You can scale puma on thread level as well as worker level.

For a quick look about puma's comparison with its other counterparts, visit http://puma.io/ .

Lets Start...


Prepare your instance/server for deployment. 

Please follow below steps from our previous blog as they are not worth repeating here. 

Prepare your instance & Install dependent libraries
Install Nginx
Install Ruby

Puma

Add puma to your Gemfile  and run bundle install 

gem 'puma'

Add puma.rb in config/ directory of your application.


environment 'production'

daemonize true
workers 2

threads 0,8

bind  "unix://your/app/path/tmp/puma.sock"
pidfile "/your/app/path/tmp/pids/puma.pid"

preload_app!

on_worker_boot do

  ActiveRecord::Base.establish_connection

end



You are probably using capistrano or mina to automate your deployment. If so in that case please put puma.rb in shared/config  and make sure your to create symbolik link in deployment script. I should probably have a separate blog for automating deployment. 


Configure Nginx to use Puma



upstream your_app {
    # Path to Puma SOCK file, as defined previously
    server unix://your/app/path/tmp/puma.sock;
}

server {

    listen 80;
    server_name yoursite.com;

    # Application root, as defined previously
    root /your/app/path/public;

    try_files $uri/index.html $uri @app;

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

    error_page 500 502 503 504 /500.html;

  
}  


Start Puma

Considering you have adjusted db setting in config/database.yml and all other environment related changes, its time to start puma. Lets do it .

From root of your application directory, run below


bundle exec puma -C config/puma.rb



Its time to visit your site. Enjoy simple, fast, threaded and concurrent PUMA!!!!!!!!!




No comments:

Post a Comment