How to deploy Ruby on Rails app on Nginx with Puma server in easy way

1. Create sample app:

rails new sample

2. Add to Gemfile:

gem 'puma' # Good server for Rails app

3. Run on terminal:

sudo apt-get update
sudo apt-get install nginx

4. Move default nginx conf file:

sudo mv /etc/nginx/sites-enabled/default.conf /etc/nginx/sites-enabled/old_default.backup

5. Create new file named "rails.conf" in /etc/nginx/sites-available/ folder:
cd /etc/nginx/sites-available/
sudo vi rails.conf

6. Copy and paste the followings to /etc/nginx/sites-available/rails.conf
upstream rails { 
  server localhost:3000; # Puma is running here
}

server { 
  listen 80 default;
  server_name localhost # OR IP ADDRESS

  location / {
    proxy_pass http://rails;
    proxy_set_header origin http://localhost:3000;

  }
}

7. Run:
sudo ln -sf /etc/nginx/sites-available/rails.conf /etc/nginx/sites-enabled/rails.conf
sudo service nginx reload
sudo service nginx restart

8. Enter into your rails project folder and run as follows:
cd ~/sample
rails s

That's all. By running rails s  Nginx and Puma servers will be connected



Comments