How to Install Rails and Nginx with Passenger on Ubuntu

linux

If you’re looking to set up a robust web application with Ruby on Rails on an Ubuntu server, using Nginx and Passenger as your application server and web server, respectively, is a great choice. This combination provides excellent performance and ease of use. In this blog post, we’ll walk you through the process step-by-step.

Prerequisites

Before we start, make sure you have:

  1. An Ubuntu Server: You can use a cloud provider like Greenhost.Cloud.
  2. A Non-root User: It’s best practice to perform installations through a user with sudo privileges.
  3. Basic Command Line Knowledge: Familiarity with the terminal will help you navigate through the installation.

Step 1: Update Your System

Before installing new packages, it’s a good idea to update your package list and upgrade the installed packages:

sudo apt update
sudo apt upgrade -y

Step 2: Install Ruby

Rails is a Ruby framework, so you’ll need to install Ruby first. We’ll use a version manager called RVM (Ruby Version Manager) for easy installation and version management.

  1. Install Required Dependencies:
sudo apt install curl gpg -y
  1. Install RVM:
\curl -sSL https://get.rvm.io | bash -s stable
  1. Load RVM:
source ~/.rvm/scripts/rvm
  1. Install Ruby:

You can check for the latest stable version of Ruby on the official Ruby website. For this guide, we’ll install Ruby 3.1.0:

rvm install 3.1.0
rvm --default use 3.1.0
  1. Verify Ruby Installation:
ruby -v

Step 3: Install Rails

Now that Ruby is installed, you can install Rails. We’ll use the latest version available.

gem install rails

You can verify the installation by checking the version of Rails:

rails -v

Step 4: Install Nginx

Next, we will install Nginx, which will serve your Rails application.

sudo apt install nginx -y

Once installed, you can verify that Nginx is running by visiting your server’s IP address in a web browser. You should see the default Nginx welcome page.

Step 5: Install Passenger

Passenger is an application server that integrates well with Nginx. To install Passenger:

  1. Install the Passenger gem:
sudo apt install -y libnginx-mod-http-passenger
  1. Ensure Nginx recognizes Passenger:

Passenger should automatically add itself to your Nginx configuration. You can verify this by opening the Nginx config file:

sudo nano /etc/nginx/nginx.conf

Find the line that includes Passenger and uncomment it. It should look something like:

passenger_root /usr/lib/ruby/gems/3.1.0/gems/passenger-6.0.0;
passenger_ruby /usr/bin/ruby;

Step 6: Configure Nginx for Your Rails App

  1. Create a new Nginx configuration file for your Rails app (replace yourapp and the server_name with your application name and domain or IP address):
sudo nano /etc/nginx/sites-available/yourapp
  1. Insert the following configuration:
server {
    listen 80;
    server_name your_domain_or_IP;

    root /path/to/yourapp/public;

    passenger_enabled on;
    passenger_app_env production;

    location / {
        try_files $uri/index.html $uri @app;
    }

    location @app {
        passenger_enabled on;
    }
}

Replace /path/to/yourapp with the actual path to your Rails application.

  1. Enable the configuration:
sudo ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled/
  1. Test Nginx configuration:
sudo nginx -t
  1. Reload Nginx:
sudo systemctl reload nginx

Step 7: Deploy Your Rails Application

  1. Clone or copy your Rails application to the server:
git clone https://github.com/yourusername/yourapp.git /path/to/yourapp
  1. Navigate to your app directory:
cd /path/to/yourapp
  1. Install dependencies:
bundle install
  1. Precompile assets:
RAILS_ENV=production rails assets:precompile
  1. Migrate the database:
RAILS_ENV=production rails db:migrate

Step 8: Testing Your App

Now that everything is set up, navigate to your server’s IP address or domain name in a web browser. You should see your Rails application running smoothly.

Conclusion

Congrats! You’ve successfully set up a Ruby on Rails application using Nginx and Passenger on Ubuntu. This powerful stack not only elevates performance but also simplifies deployment. For further tuning and configuration, don’t hesitate to explore the official documentation for Rails, Nginx, and Passenger.

As always, ensure your server is secured adequately and consider using SSL for your web application. Happy coding!


Note: Don’t forget to replace placeholder text (like your domain or application name) with actual values relevant to your setup.