Linux MySQL

How to Install Ruby On Rails on Ubuntu 20.04 with MySQL, Nginx, Passenger, SSL

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

How to Install Ruby On Rails on Ubuntu 20.04 with MySQL, Nginx, Passenger, SSL. Ruby is a dynamic, open-source programming language that prioritizes simplicity and productivity. It features an easy syntax that is natural to read and write. Ruby on Rails is a widely used web framework for Ruby that was created to help software developers be more productive.

Nginx is a web server which can provide HTTP transactions and serve static files but cannot run Ruby applications directly. So we use Phusion Passenger which is a free, open-source web application server. It is designed to handle HTTP requests, monitor and manage processes and resources, as well as allow administration, monitoring, and problem diagnosis.

In this guide, you will set up Passenger to assist Nginx with serving your Ruby on Rails web application and install Let’sEncrypt SSL to secure your application.

Prerequisites

  1. A server with Ubuntu 20.04
  2. Root access to your server or user with sudo privileges.
  3. Intermediate knowledge in using Linux terminal commands.
  4. Domain name which is pointed to the external IP of your server.

Initial Server Setup

Update your server packages to its latest.

sudo apt update
sudo apt dist-upgrade -y

Install Ruby on Rails with Rbenv

To make sure the setup goes fine we shall start by adding the Node.js and Yarn repositories.

curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Install Dependencies

Now you can install all the dependencies.

sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn

Install Rbenv and Rbenv Build

Rbenv is a Ruby version manager which is used to install Ruby.

Here we will install Rbenv and Rbenv-build.

Install Rbenv

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

Install Rbenv Build

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

Configure Ruby Version

Now you can setup default and global Ruby version.

rbenv install 3.1.1
rbenv global 3.1.1

Confirm Ruby version using the following command.

ruby -v

Now you need to install bundler.

gem install bundler

Install Rails

gem install rails -v 7.0.2.3

Make rails command to be executable.

rbenv rehash

Confirm Rails version.

rails -v

Install MySQL

In this tutorial we shall use MySQL for database.

sudo apt-get install mysql-server mysql-client libmysqlclient-dev
sudo mysql_secure_installation

You can configure new user and password in MySQL if you need.

Configure Rails for MySQL

Now install MySQL adapter for Rails

gem install mysql2

Install Passenger and Nginx

Now we will install Nginx and configure Passenger.

Install Nginx.

sudo apt -y install nginx

Install Passenger.

sudo apt install -y dirmngr gnupg apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7

Add Passenger APY repository.

sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger focal main > /etc/apt/sources.list.d/passenger.list'

Update and install Passenger.

sudo apt update
sudo apt install -y libnginx-mod-http-passenger

Now a new passenger configuration will be added inside conf.d directory of Nginx installation.

Restart Nginx for the Passenger module installation to take effect.

sudo service nginx start

Configure Passenger with Rbenv

Edit the Passenger configuration file.

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

Comment out lines starting with passenger_root and passenger_ruby.

Append the following lines into the file.

Replace username in the passenger_ruby option path with the username through which you have installed Rbenv.

passenger_ruby /home/example_user/.rbenv/shims/ruby;
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;

Save and exit the file.

Restart Nginx.

sudo service nginx restart

Create New Rails Project

Create a new directory for your Rails project or just navigate to any directory. Here we are navigating to the default Nginx web directory.

cd /var/www

Create Rails project.

rails new project-name -d mysql
cd project-name

Edit the database configuration to connect to your database.

sudo nano config/database.yml

Replace the username with your username, password with your password

Make sure your database.yml looks like this

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: username
  password: password
  host: locahost
  database: database_name

development: 
  <<: *default 

test: 
  <<: *default

production:
  <<: *default

Initialize Rails database.

rake db:create

Restart Rails.

rails restart

Configure Nginx with Ruby on Rails

Remove default Nginx configurations.

sudo rm -rf /etc/nginx/sites-available/default
sudo rm -rf /etc/nginx/sites-enabled/default

Create a new configuration for your Ruby application

sudo nano /etc/nginx/sites-available/domainname.conf

Paste this new configuration setting and hit Ctrl+X followed by Y to save the file

server {
    listen 80 default_server;
    server_name domainname.com;

    passenger_enabled on;
    passenger_app_env production;

    root /var/www/html/project-name/public;
}

Enable your new configuration

sudo ln -s /etc/nginx/sites-available/domainname.conf /etc/nginx/sites-enabled/domainname.conf

Validate Nginx configuration and restart nginx

sudo nginx -t
sudo service nginx restart

Install Let’sEncrypt SSL

We can use Certbot to install free Let’s Encrypt SSL certificate for your domain.

sudo apt install python3-certbot-nginx

Execute the following command to install certificate and configure redirect to HTTPS automatically.

sudo certbot --nginx --redirect --agree-tos --no-eff-email -m [email protected] -d domain.com -d www.domain.com

Now you should receive SSL certificate and it will be configured automatically.

Setup auto renewal.

sudo certbot renew --dry-run

Now Ruby on Rails is installed and configured with Passenger and Nginx and connected with MySQL database.

Visit your domain name on your browser, you can view the Ruby on Rails welcome page.

How to Install Ruby On Rails on Ubuntu 20.04 Welcome page

Conclusion

Now you have learned how to Install Ruby On Rails on Ubuntu 20.04 with MySQL and configure Nginx with Passenger and configure Let’sEncrypt SSL.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.