Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fzrhrs/d787de2af976c5fb13c2a55f70bdce4d to your computer and use it in GitHub Desktop.
Save fzrhrs/d787de2af976c5fb13c2a55f70bdce4d to your computer and use it in GitHub Desktop.

Step 1 - Initial Server Setup

Assuming you already ssh-ed into the server as root.

Create a new user

We need to set up a new user account with reduced privileges for day-to-day use.

sudo adduser deploy

You will be asked a series of questions, starting with a password. Make sure you create a strong password. Other information is not required. I usually leave it empty.

Grant administrative privilleges

Sometimes you will have to perform administrative tasks as the root user. To avoid logging in and out, you can setup a superuser role or root privillages to the new user. This allows the user to run adminstrative commands with sudo.

As root, run this command to add your new user to the sudo group:

usermod -aG sudo deploy

Enable access for your regular user

Now that you have a regular user for daily use, you will need to make sure that you can SSH into the account directly.

SSH to your new user account by typing:

ssh deploy@your_server_ip_address

After you enter your regular user's password, you will be logged in. Remember, if you need to run adminstrative commands, you need to type sudo before it like this:

sudo command_to_run

You will receive a prompt for your regular user's password when using sudo for the first time each session (and periodically afterward).

To enhance your server's security, it is strongly recommended to set up SSH keys instead of using password authentication.

To do this, first create a .ssh folder in the home directory of the user.

In this case, it should be: /home/deploy

Then, simply create the folder by typing:

mkdir .ssh

cd to the folder, and create a new file called authorized_keys by typing:

vi authorized_keys

Assuming you already copy your personal ssh keys from your local, paste the keys inside the authorized_keys file and save.

Now, open up a new terminal session on your local machine, and use SSH with your new username:

ssh deploy@your_server_ip_address

You should be connected to your server with the new user account without using a password.

If you have multiple remote systems over SSH, having a config file would definitely save you from remembering all the IP addresses, keys etc. To learn how to do this, head over to: https://linuxize.com/post/using-the-ssh-config-file/.

Step 2 - Install RVM, Ruby, Puma, Postgres & NGINX

RVM

First, you'll need to install or update GPG (GNU Privacy Guard) to the most recent version in order to contact a public key server and request a key associated with the given ID:

sudo apt update
sudo apt install gnupg2

Next, you'll request the RVM project’s public key to verify the legitimacy of your download:

gpg2 --keyserver hkp://keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Go to the /tmp folder by typing:

cd /tmp

Here, download a stable version of the RVM script.

sudo apt update && sudo apt install curl && curl -sSL https://get.rvm.io | bash -s stable

When the installation is complete, source the RVM scripts from the directory they were installed:

source ~/.rvm/scripts/rvm

To confirm that rvm is now installed, type:

rvm -v

You should see something like this:

rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

Ruby

If you need to install a specific version of Ruby for your application, rather than just the most recent one, you can do so with RVM. First, check to see which versions of Ruby are available by listing them:

rvm list known

Then, install the specific version of Ruby that you need through RVM, replacing the highlighted version number with your version of choice, such as ruby-3.1.3 or just 3.1.3, like so:

rvm install ruby-3.1.3

After the installation, you can list the available Ruby versions you have installed by typing:

rvm list

Make default of the version you intend to use:

rvm --default use 3.1.3

To confirm the Ruby version that is currently active, type:

ruby --version

Node.js

Out of the box, Rails does not require Node.js since importmaps is now used by default. In practice, you may run into apps that still require it.

Now, to install Node.js, you can do so by typing:

sudo apt install nodejs

Verify that you've installed the new version by running node with the -v version flag:

node -v

PostgreSQL

To install PostgreSQL, first refresh your server's local package index:

sudo apt update

Then, install the Postgres package along with other packages to add additional utilities and functionality:

sudo apt install postgresql postgresql-contrib libpq-dev

NGINX

Installing NGINX is easy by typing:

sudo apt install nginx

At the end of the installation process, Ubuntu 22.04 will start NGINX. The web server should already be up and running:

We can check the system by typing:

sudo service nginx status

Puma

We can install Puma by using the apt command.

Before that, refresh our apt database by typing:

sudo apt update

Now, install Puma by typing:

sudo apt install puma

Swap Space

One way to guard against out-of-memory errors in applications is to add some swap space to your server.

Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.

We can see if the system has any configured swap by typing:

sudo swapon --show

If you don’t get back any output, this means your system does not have swap space available currently.

You can verify that there is no active swap using the free utility:

free -h

Before you create the swap file, check the current disk usage to make sure you have enough space. Do this by entering:

df -h

The device with / in the Mounted on column is our disk in this case.

Assuming the server has 1G of RAM, let's create a 1G file.

sudo fallocate -l 1G /swapfile

Verify the correct amount of space was reserved by typing:

ls -lh /swapfile

Now, you must make the file only accessible to root by typing:

sudo chmod 600 /swapfile

Then verify the permission change by typing:

ls -lh /swapfile

You can now mark the file as swap space by typing:

sudo mkswap /swapfile

Then, to enable:

sudo swapon /swapfile

Verify that the swap file is available:

sudo swapon --show

You can check the output of the free utility again to corroborate your findings:

free -h

Next, we have to make the swap file permanent.

But before that, back up the file in case anything goes wrong:

sudo cp /etc/fstab /etc/fstab.bak

Add the swap file information to the end of your /etc/fstab file by typing:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Fine tune the swap settings

There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap:

  1. Adjust swappiness property
  2. Adjust cache pressure setting

To do this, simply open the /etc/sysctl.conf file, and then add:

vm.swappiness=10
vm.vfs_cache_pressure=50

Make sure to save before you exit.

Done.

The server is now ready and it's time for deployment.

References

  1. https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-22-04
  2. https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rvm-on-ubuntu-20-04
  3. https://www.vultr.com/docs/install-ruby-with-rvm-on-ubuntu-18-04-and-19-10/
  4. https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-22-04-quickstart
  5. https://installati.one/install-puma-ubuntu-22-04/
  6. https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-22-04
  7. https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment