What are we going to configuration?

(1) Puma server
(2) Nginx serverHello Readers.

Before starting let’s understand what is Puma server and Nginx server, why we use them?

Puma is a simple, fast, threaded, and highly concurrent server for Ruby/Rack applications, which is run our application.

Nginx is a web server that takes a request to your website from a user and does some processing on it. Then, it might give the request to your Rails app.

Let’s configure Puma in our application.

I am using Rails 5.Puma is the default server for Rails 5.+, and should already be included in your Gemfile.
If you are using rails 4 then you have to add it to your gem file. and install the bundle.
“gem  ‘puma’ “
$bundle install

And create a file inside the config folder.
“puma.rb”

As I told you I am using rails 5 and already have a demo blogging project.

the config/puma.rb file looks like this.

 Nginx and Puma

Right now these all configuration for the development environment.
replace the below code.

application_path="<Application path>"
 directory application_path
 port 3000
 environment "production"
 daemonize true
 pidfile "#{application_path}/config/puma.pid"
state_path 'tmp/pids/puma.state'
 stdout_redirect "#{application_path}/log/puma.log", "#{application_path}/log/puma_err.log"
# # quiet
 threads 1,16
 bind "unix://#{application_path}/puma.sock"
 workers 2
 preload_app!
  on_worker_boot do
    puts 'On worker boot...'
  end

and save your file.

and run the server.

$rails s

 Nginx and Puma



Puma server is running in a background process.
because we use “daemonize true”We create it a daemon process that is working in the background.

now you can access it by using
http://0.0.0.0:3000 and http://<your ip>:3000

To know your IP  use ifconfig command in Linux.
Ex:- 192.168.X.X


One more thing we are using Unix socket.
 < bind “unix://#{application_path}/puma.sock” > in puma configuration .

And we access the socket in Nginx.

Right now we are using the port Number in URL, but we don’t want to use it.


Let’s configure the Nginx:-

 First of all install the Nginx.

$sudo apt-get install Nginx

 Nginx and Puma

Move to the Nginx location.

$cd /etc/nginx/sites-available

 Nginx and Puma

view the default Nginx configuration file.

  $cat default

 Nginx and Puma


 Then Open and edit Nginx configuration file.
   $vi default
   Remove all default configuration, using vi commands.    :1,$d
And add the below code

upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:///<Application Path>/puma.sock fail_timeout=0;
}
server {
    listen 80;
    server_name localhost;
    root <Application path>/public;
    try_files $uri/index.html $uri @app;
    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

After the edit saves the file and restarts the Nginx.

   $systemctl restart nginx.service
Go back to your Browser type your IP.

http://<your IP>

 Nginx and Puma

It’s working. ūüôā
Special thanks to https://runwithcode.blogspot.com/2018/02/installation-and-configuration-of-nginx.html for this Article.

Thanks For Reading.
And please share your feedback through comments.

One thought on “Installation and configuration of Nginx and Puma for Rails in Ubuntu (16.04)”

Leave a Reply

Your email address will not be published. Required fields are marked *