Ubuntu server setup for RoR

Server config below.  Startting with Ubuntu 22.04LTS.

Install Nginx and set SSL certificates

Install Nginx

sudo apt install nginx

Open firewall ports

sudo ufw allow 'Nginx HTTP'

Set up Nginx server blocks per this example.

Reload Nginx

sudo systemctl restart nginx

Certbot recommends snap package so update snap

sudo snap install core; sudo snap refresh core

Install Certbot

sudo snap install --classic certbot

Link the certbot command from the snap install directory to your path, so you’ll be able to run it by just typing certbot

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Use Certbot to get certificates

sudo certbot --nginx -d example.com -d www.example.com 

Restart Nginx

sudo systemctl restart nginx

Install Ruby Environment

You will be running the commands as root. If you aren’t already root, switch to root: sudo su -

Make sure curl, wget, gnupg, apt-transport-https, lsb-release and ca-certificates are installed first:

apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates

Node.js

curl -sL https://deb.nodesource.com/setup_16.x | bash -

PostgreSQL

wget -O /usr/share/keyrings/postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc
echo "deb [signed-by=/usr/share/keyrings/postgresql.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/postgresql.list

System packages

apt update
apt install -y \
  imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core \
  g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf \
  bison build-essential libssl-dev libyaml-dev libreadline6-dev \
  zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev \
  redis-server redis-tools postgresql postgresql-contrib \
  libidn11-dev libicu-dev libjemalloc-dev

Yarn

corepack enable
yarn set version classic

Installing Ruby

We will use rbenv to manage Ruby versions as it simplifies obtaining the correct versions and updating them when new releases are available. Since rbenv needs to be installed for an individual Linux user, we must first create the user account under which the Ruby app will run:

adduser  new-username

We can then switch to the user:

su - new-username

And proceed to install rbenv and rbenv-build:

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

Once this is done, we can install the correct Ruby version:

RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.2.2
rbenv global 3.2.2

We’ll also need to install the bundler:

gem install bundler --no-document

Return to the root user:

exit

Setting up PostgreSQL

Performance configuration (optional)

For optimal performance, you may use pgTune to generate an appropriate configuration and edit values in /etc/postgresql/16/main/postgresql.conf before restarting PostgreSQL with systemctl restart postgresql

Creating a user

You will need to create a PostgreSQL user that the Ruby app could use. It is easiest to go with “ident” authentication in a simple setup, i.e. the PostgreSQL user does not have a separate password and can be used by the Linux user with the same username.

Open the prompt:

sudo -u postgres psql

In the prompt, execute:

CREATE USER new-username CREATEDB; \q 

Done!

Installing Rails

To install Rails, use the gem install command along with the -v flag to specify the version. For this tutorial, you’ll use version 7.1.2:
Switch back to the Ruby user.

suso su - new-user
gem install rails

Done! Now we can start a test app!

rails new myapp --database=postgresql

You should now be able to navigate to your app and see the default Rails page!