Friday, 15 June 2018

Deploy rails application with nginx and unicorn server configurations

NGNIX SETUP With Rails Application
==================================
1. sudo apt-get install nginx

2. sudo vi /etc/nginx/sites-enabled/default -> Open config file

3.Update the below changes in nginx default file

root /home/rajkumar/Rajkumar/Projects/rajuthayaa
server_name localhost;
location @raj{
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://localhost:3001;
}

4. sudo service nginx start

5. if any errors in nginx kindly run the below command and check it.
 sudo vi /var/log/nginx/error.log



UNICORN SETUP With Rails Application
====================================
1. gem 'unicorn' in your gemfiles

2. create unicorn.rb in config/unicorn.rb

3. Update the below changes in unicorn.rb files

APP_PATH = "/home/rajkumar/Rajkumar/Projects/rajuthayaa"

working_directory APP_PATH
stderr_path APP_PATH + "/log/unicorn.stderr.log"
stdout_path APP_PATH + "/log/unicorn.stdout.log"

preload_app true

timeout 30

listen "/home/rajkumar/Rajkumar/Projects/rajuthayaa/shared/sockets/unicorn.rajutaya.sock", :backlog => 64

listen 3002, :tcp_nopush => true

pid  APP_PATH + "/unicorn.pid"

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
      ActiveRecord::Base.establish_connection
end

upstream raj(raj){
server unix:/home/rajkumar/Rajkumar/Projects/rajuthayaa/shared/sockets/unicorn.rajutaya.sock fail_timeout=0;
}

4. Add upstream in nginx config files -->sudo vi /etc/nginx/sites-enabled/default

upstream raj(raj){
server unix:/home/rajkumar/Rajkumar/Projects/rajuthayaa/shared/sockets/unicorn.rajutaya.sock fail_timeout=0;
}

5. create shared/sockets inside your rails app

6. unicorn_rails -c config/unicorn.rb -D for run the unicorn server.

7. if any issues kindly check the log/unicorn.stderr.log files.

8. Restart both nginx and unicorn server it will work.

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...