Ruby on Rails

Ruby-on-Rails is a full-stack server-side web application framework that includes everything needed to create a database-backed web application according to the Model-View-Controller (MVC) pattern.

Software Included

Package Version License
Ruby 3.2.0 2-clause BSD License
Rails 7.0.4.2 MIT
Puma 6.0.2 Custom
Postgres 12.4 Postgres SQL
Nginx 1.17.10 Custom
Node.js 12.19.0 Custom
Certbot 0.40.0 Apache 2

Creating an App using the Control Panel

Click this button to create a Droplet based on this 1-Click App. If you aren’t logged in, this link will prompt you to log in with your DigitalOcean account.

Deploy to DO

Creating an App using the API

In addition to creating a Droplet from the Ruby on Rails 1-Click App using the control panel, you can also use the DigitalOcean API. As an example, to create a 4GB Ruby on Rails Droplet in the SFO2 region, you can use the following curl command. You need to either save your API access token) to an environment variable or substitute it in the command below.

curl -X POST -H 'Content-Type: application/json' \
         -H 'Authorization: Bearer '$TOKEN'' -d \
        '{"name":"choose_a_name","region":"sfo2","size":"s-2vcpu-4gb","image": "rubyonrails-20-04"}' \
        "https://api.digitalocean.com/v2/droplets"

Getting Started After Deploying Ruby on Rails

After your droplet is created, you can access your Ruby on Rails installation by typing the droplet’s IP address in your browser:

RoR Welcome Page

Note: If you see the Site can’t be reached error, give it 2-3 minutes and reload the page.

Ruby is configured to use vendored bundles. Log in as a rails user to interact with the default project environment:

$ su - rails

All gems are also installed under the rails user environment.

Nginx is configured to forward requests for rails to a local Unix socket. Nginx configuration is located at /etc/nginx/sites-enabled/rails

Ruby-on-Rails Droplet 1-Click uses Puma to host the default website. Puma is managed via systemd service located at /etc/systemd/system/rails.service

You can manage Puma service via:

  • Restart:
$ systemctl restart rails.service
  • Stop:
$ systemctl stop rails.service
  • Start:
$ systemctl start rails.service

Migrating existing Ruby on Rails project

If you already have a Ruby on Rails (further as RoR) project, you can follow the steps below to migrate it to your droplet.

Note: All droplet commands below are intended to be run as the root user

First of all, you should copy your project folder to the droplet, you can do this by either using a VCS like git or simply using scp command:

$ scp -r /root/<your local ror project directory> [email protected]<your droplet IP>:/home/rails/<your ror project name>

Next, ssh into your droplet and make sure to give user rails permissions to your new Ruby on Rails project:

$ chown -R rails:rails /home/rails/<your ror project name>

At this point you can log in as rails user to install necessary dependencies for your project.

After this, you will have to update the Rails service if you wish to run your project as a service. First, open service file:

$ vim /etc/systemd/system/rails.service

Next, locate WorkingDirectory and ExecStart fields. Set WorkingDirectory as the directory of your RoR project and set ExecStart to the command which you use to run your project, in the example below we use the most common command rails s:

...
WorkingDirectory=/home/rails/<your ror project name>
ExecStart=/bin/bash -lc 'rails s'
...

Save file and exit.

Finally, you need to reload and restart the Rails service:

$ systemctl daemon-reload
$ systemctl restart rails.service

Droplet summary

  • UFW firewall allows only SSH (port 22, rate limited), HTTP (port 80), and HTTPS (port 443) access.
  • rails system user is created to deploy the application.
  • rails database user is created for the application to connect to PostgreSQL.
  • Ruby is configured to use vendored bundles and all Gems are installed and processed locally to the rails user.
  • Nginx is configured to forward requests to a local UNIX socket, acting as a reverse proxy for the application server.
  • The message of the day (MOTD) shows the system and database user passwords, which are also saved in /root/.digitalocean.passwords.
  • The MOTD will also show you SFTP credentials that you can use to upload files using software like FileZilla, WinSCP, or rsync.
  • The unit file for Rails is /etc/systemd/system/rails/service.