Installing Nginx and Passenger on Ubuntu

Techie     January 2023

Introduction

This section describes the installation of Nginx and Passenger on Ubuntu.


1. Install nginx

$ sudo apt-get install nginx


2. Install Passenger packages

# Install our PGP key and add HTTPS support for APT
$ sudo apt-get install -y dirmngr gnupg apt-transport-https ca-certificates curl

$ curl https://oss-binaries.phusionpassenger.com/auto-software-signing-gpg-key.txt | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/phusion.gpg >/dev/null

# Add our APT repository
$ sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger jammy main > /etc/apt/sources.list.d/passenger.list'

$ sudo apt-get update

# Install Passenger + Nginx module
$ sudo apt-get install -y libnginx-mod-http-passenger


3 . Enable the Passenger Nginx module

$ 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


4 . Point Passenger to the correct version of Ruby

$ sudo nano /etc/nginx/conf.d/mod-http-passenger.conf


Change the passenger_ruby directive to match the following:

passenger_ruby /home/username/.rbenv/shims/ruby;

# for example
passenger_ruby /home/ubuntu/.rbenv/shims/ruby;


5 . Start nginx

$ sudo service nginx start


Confirm that NGINX is running by visiting your server’s public IP address in your browser. You should be greeted with the “Welcome to NGINX” message.


6 . Add an nginx server for your application

# Remove the default nginx server
$ sudo rm /etc/nginx/sites-enabled/default


# create a server for your application
$ sudo nano /etc/nginx/sites-enabled/myapp


The contents should resemble this:

server {
listen 80;
listen [::]:80;
server_name _;
root /home/username/projects/myapp/current/public;
passenger_enabled on;
passenger_app_env production;
location /cable {
passenger_app_group_name myapp_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# Allow uploads up to 100MB in size

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }
}

Replace username with the name of your user and change myapp to the name of your app. You will use this same folder later on when you define your Capistrano deploy_to folder. Save the file.


7 . Reload NGINX to load the new server files.

$ sudo service nginx reload


That’s it! Thanks for reading, see you in the next one!