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

How to deploy rails app with passenger and nginx with SSL

Hello There!!, In the previous blog we lean how to deploy a rails application with passenger and nginx. Today we are going to learn how to deploy a rails application with passenger and nginx with SSL. Here is an example of that

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:// --recv-keys 561F9B9CAC40B2F7
2. sudo apt-get install -y apt-transport-https ca-certificates
3. sudo sh -c 'echo deb 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 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name _;
    # SSL configuration
    ssl on;
    ssl_certificate SSL.crt_PATH;
    ssl_certificate_key App.key_PATH;
    ssl_prefer_server_ciphers On;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #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
    # Enabling passenger for application
    passenger_enabled on;
    # Rails environment for application
    passenger_app_env staging;
    # Applications root
        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;
    server {
    listen 80;
    rewrite ^/(.*) https://$host$request_uri permanent;

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