Deploying Rails 7 application with Nginx and Passenger

Part 1: Installation

This deployment is done in Ubuntu 22.04 as Passenger has not officially supported 24.04 at this point in time.

  1. Launch a new instance, and perform an update if there is one.
    sudo apt update
    sudo apt upgrade
  2. Set the timezone if needed.
    sudo timedatectl set-timezone Asia/Kuala_Lumpur
  3. Generate identity (Whitelist your repository with the public key if needed)
    ssh-keygen
  4. Install the necessary tools
    sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev mysql-client libmysqlclient-dev
  5. Install nginx
    sudo apt install nginx -y
  6. Install rbenv and ruby-build
    https://github.com/rbenv/rbenv
    git clone https://github.com/rbenv/rbenv.git ~/.rbenv
    ~/.rbenv/bin/rbenv init
    git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
  7. Install the intended Ruby version and make it global
    rbenv install 3.x.x
    rbenv global 3.x.x
  8. Follow the official guide to install Passenger.
  9. Follow the official guide to install certbot for Let's Encrypt.

Part 2: Application

Clone your code, or setup Capistrano deployment

Part 3: Setup Nginx & Passenger

  1. Remove the default nginx site config
    sudo rm /etc/nginx/sites-available/default
  2. Create your site config
    # /etc/nginx/sites-available/mysite
    server {
            server_name my.domain;
            passenger_enabled on;
            rails_env production;
            root /path/to/public;
            listen 80 default_server;
            listen [::]:80 default_server;
    }
  3. Create a symlink of this config in the sites-enabled directory
    sudo ln -s /etc/nginx/sites-available/mysite mysite
  4. Update the Passenger config to point to the correct Ruby.
    ### Begin automatically installed Phusion Passenger config snippet ###
    passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
    passenger_ruby /home/ubuntu/.rbenv/shims/ruby;
    
    passenger_instance_registry_dir /var/run/passenger-instreg;
    
    ### End automatically installed Phusion Passenger config snippet ###
    
    passenger_preload_bundler on;
  5. Start the Nginx server
    sudo service nginx start
  6. Check passenger status
    passenger-status
  7. Install SSL Cert using Certbot
    sudo certbot --nginx

Part 4: Optional Steps

  1. Install node
    sudo apt install nodejs

AI Summary
gpt-4o-2024-05-13 2024-07-16 00:41:00
This guide explains how to deploy a Rails 7 application using Nginx and Passenger on Ubuntu 22.04. It covers installation steps, application setup, Nginx and Passenger configuration, and optional steps such as installing Node. Key steps include updating the server, installing necessary tools, configuring Nginx, and setting up SSL with Certbot.
Chrome On-device AI 2024-07-20 04:40:28

Share Article