How to deploy rails app with passenger and nginx | Ram Laxman Yadav

How to deploy rails app with passenger and nginx

Hello There!!, Today we are going to learn about how to deploy a rails application with passenger and nginx.

Remove previously installed nginx

if you have already installed nginx then first remove the existing nginx before we start. For remove nginx run the command.

sudo apt-get remove nginx nginx-full nginx-light nginx-naxsi nginx-common

Remove old unicorn

If your site was running on unicorn then first uninstall unicorn and remove all unicorn config. Do the following steps.

  • sudo rm /etc/init.d/YOUR_APP_UNICORN_CONFIG_FILE_NAME
  • sudo update-rc.d -f YOUR_APP_UNICORN_CONFIG_FILE_NAME remove

Install nginx with passenger

First find the release of your ubuntu machine by running lsb_release -a and copy the code name for further use. Now run the following commands to install nginx with passenger:

1. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
2. sudo apt-get install -y apt-transport-https ca-certificates
3. sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger CODE_NAME_OF_RELEASE main > /etc/apt/sources.list.d/passenger.list'
4. sudo apt-get update
5. sudo apt-get install -y nginx-extras passenger

Enable the nginx passenger module

Edit /etc/nginx/nginx.conf and uncomment passenger_root and passenger_ruby. For example, you may see this:

# passenger_root /some-filename/locations.ini;
# passenger_ruby /usr/bin/passenger_free_ruby;
Remove the ‘#’ characters.

Check the installation

After installation you can validate the installation by running the following command: sudo passenger-config validate-install

Create a nginx config for your application

  • Remove the existing file /etc/nginx/site-enabled/default.
  • Create new config file for your application and write the following lines inside that file:
     server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
    
        #Enabling application to being included via the iframes
        add_header X-Frame-Options ALLOW; // DENY if you want to deny your application to include in iframe
    
        passenger_enabled on;
        passenger_app_env YOUR_APP_RAILS_ENV;
    
        root YOUR_APP_PUBLIC_DIRECTORY_PATH;
    
        location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
        }
        error_page 500 502 504 /500.html;
        error_page 503 @maintenance;
    
        location @maintenance {
        rewrite ^(.*)\$ /503.html break;
        }
    
        if (-f \$document_root/../tmp/maintenance.txt) {
        return 503;
        }
        }

Restart nginx service

After setting all the configs you need to restart the nginx service by running sudo service nginx restart.

Restart passenger

If you want to restart passenger then you need to touch a file tmp/restart.txt. touch YOUR_APP_DIRECTORY/tmp/restart.txt

Show maintenance page during maintenance

You just need to do the steps:

  • touch YOUR_APP_DIRECTORY/tmp/maintenance.txt after finish the maintenance just remove this file.
  • rm YOUR_APP_DIRECTORY/tmp/maintenance.txt and your application is live again.

Thanks for the reading !!!!!!

Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer

Related