Easy way to configure Nginx and Passenger to run a Rails app in Ubuntu

Steven     24.12.2021

This article details how to setup Nginx and Passenger to run a Rails app in production environment on a Ubuntu machine. It uses specifically:

  • Ubuntu 20.04 LTS
  • Nginx 1.18
  • Phusion Passenger 6.0.12

How Nginx and Passenger work together

First of all, understand why you’re using the combination of Nginx and Passenger: Nginx web server is used as the web server, Passenger is the application server allowing the Rails app to speak HTTP.

Configure Nginx to use passenger and use your app files

#/etc/nginx/sites-available/default

server{
  # Nginx uses the 'server_name' attribute to decide which server should process the request. In case no match is found, 'default server' is used
  listen 80 default_server
  server_name example.com

  # tell Nginx what root folder to use. Make sure to use the public/ folder inside the Rails app folder  
  root /path/to/rails/app/public

  # tell Nginx to enable passenger with this server
  passenger_enabled on
}

Configure Passenger to use app root, local user, ruby and

#/etc/nginx/conf.d/mod-http-passenger.conf

# passenger root and ruby commands. Get these through 'passenger-config --ruby-command
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /path/to/ruby/version;

passenger_default_user <your-local-user>;
passenger_default_group <your-local-user-group>;

Make sure to configure passenger to use the local user account that is owner of the rails app folder. Not only is running the rails app with a local user a good security practice, instruction passenger to use this account also ensures that all local variables (e.g. definitions in .bashrc) are sources when launching the Rails app.

Comment

Related

How to store credentials in Rails 7

Store credentials for your Rails app in credentials.yml. Read them out in configuration .yml and .rb files. Never share your master.key.