Installation and configuration of Nginx and Puma for Rails in Ubuntu (16.04) - Run With Code

Latest

Learn Ruby On Rails , Linux and IoT(Internet Of Things).

Amazon

Tuesday 20 February 2018

Installation and configuration of Nginx and Puma for Rails in Ubuntu (16.04)

What are we going to configuration?

(1) Puma server

(2) Nginx server



Hello 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 which 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 already created a Blogging application for my last blog.
   Installation and configuration of Ruby and Ruby On Rails in Ubuntu (16.04)

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.





Right now these all configuration for the development environment.

replace with 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




Puma server is running in a background process.
because we use "daemonize true"We create it a daemon process which 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.


Let's configure the Nginx:-

 First of all install the Nginx.

$sudo apt-get install nginx





Move to the Nginx location.


   $cd /etc/nginx/sites-available



view the default Nginx configuration file.

  $cat default




 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>








It's working. :)


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




No comments:

Post a Comment

Please do not enter any spam link in the comment box.