Install Nginx and Passenger on Ubuntu 18.04

Install Nginx and Passenger

Install the pre-requisite packages and Ruby before following the instructions in this article. SSH into the server as the deploy user and run:

sudo apt-get install -y dirmngr gnupg
sudo apt-key adv --keyserver hkp:// --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
sudo sh -c 'echo deb bionic main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
sudo apt-get install -y nginx-extras libnginx-mod-http-passenger
if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load /etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi
sudo ls /etc/nginx/conf.d/mod-http-passenger.conf
sudo service nginx start

You can validate the installation:

sudo /usr/bin/passenger-config validate-install

Browse to your IP or domain to see if Nginx is running.

Configure Nginx


which ruby

Copy the location of Ruby in the output of which command in mod-http-passenger.conf:

sudo vim /etc/nginx/conf.d/mod-http-passenger.conf


passenger_ruby /home/deploy/.rvm/rubies/ruby-2.6.1/bin/ruby

The file will now look like this:

### Begin automatically installed Phusion Passenger config snippet ###
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/deploy/.rvm/rubies/ruby-2.6.1/bin/ruby;
### End automatically installed Phusion Passenger config snippet ###

Troubleshooting Tip

If you make syntax error, you will get error. You can test your configuration using the command:

sudo nginx -t
[sudo] password for deploy: 
nginx: [emerg] unexpected end of file, expecting ";" or "}" in /etc/nginx/conf.d/mod-http-passenger.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed

Restart the server to pickup the configuration changes.

sudo service nginx restart

You can check the Nginx status:

service nginx status

Browse to the IP address. You should see the welcome to nginx page.

Configure Passenger and Nginx

Edit the file /etc/nginx/sites-enabled/default:

server {
        listen 80;
        listen [::]:80 ipv6only=on;

        passenger_enabled on;
        rails_env    production;
        root         /home/deploy/developertask/current/public;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;

You can now restart Nginx to make it pickup the configuration changes:

sudo service nginx restart

You have to map the IP address to your domain to see nginx serving up the default Rails home page on your domain. Next steps, create a shell script with these commands and provision the server using Vagrant.


Related Articles