Linode Tutorial Part 2: Setting Up a Domain, Ubuntu, and Nginx Reverse Proxy for Multiple Routes

Habibi Coding | حبيبي كودنق
Geek Culture
Published in
8 min readFeb 22, 2023
tutorial banner

In Part 1 of this tutorial series, we covered the following steps:

*) Creating a Linode Ubuntu 22.04 instance
*) Generating an SSH Key pair and adding it to Linode
*) Connecting to our server via SSH and setting the domain on Linode
*) Setting up the Domain and Name Server records
*) Use dig command to verify entries

If you missed Part 1, you can find it here: Part 1

Prepare your Ubuntu instance

First, use your Terminal again to connect with your Ubuntu web server ssh root@{your-ip} (you find your IP on your Linode dashboard)

server IP on the dashboard
terminal ssh

After you successfully connected type the following command:

apt update
apt update

You can take a look at which packages are upgradeable with:

apt list --upgradable
apt list — upgradable

If you want to upgrade them just type:

apt upgrade
apt upgrade

To make sure every upgrade works now, I would suggest rebooting your server go to: “Linodes” -> click on your instance -> “Reboot

Reboot

Adding a new User — Yalla | يلا

I would suggest creating a new user on your Ubuntu server and using root only when you need to use it. Connect again with your terminal to your Ubuntu server via SSH.

ssh root@{your-ip}
ssh

After you log in as root, type the command:

adduser {your-user-name}
adduser

When you have created your user then add your user to the sudo group, with the command:

usermod -aG sudo {your-user-name}
add user to sudo group

Next, you can switch from root to your user with:

su {your-user}
switch user

After that type the command:

sudo ls -la /root
sudo ls -la /root

You should see now some directories and files. Next type two times exit in the terminal to get logged out from your user and root.

Now, try to ssh with your user to your Ubuntu server.

ssh {your-user}@{your-server-ip-address}
ssh with new user

Set up Nginx

Create and edit the nginx.list file with:

sudo vim /etc/apt/sources.list.d/nginx.list
sudo vi /etc/apt/sources.list.d/nginx.list

Add Nginx deb repository. By pressing i to switch to INSERT MODE paste the lines with CMD + Vand save and close it with ESC + : + wq!

deb https://nginx.org/packages/ubuntu/ jammy nginx
deb-src https://nginx.org/packages/ubuntu/ jammy nginx
deb repositories

The available NGINX Ubuntu release support is listed on this distribution page. For a mapping of Ubuntu versions to release names, please visit the Official Ubuntu Releases page. For Ubuntu 22.04 LTS Jammy Jellyfish the command is jammy nginx for the newer version Ubuntu 22.10 Kinetic Kudu it would be kinetic nginx .

Run an update with:

sudo apt update

You will see the following message: “ The following signatures couldn’t be verified because the public key is not available:”

To fix the error just type this command:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62
fix error

Run an update again:

sudo apt update
update again

You should see only one warning and to get rid of the warning we can just, copy the /etc/apt/trusted.gpg file to /etc/apt/trusted.gpg.d directory. After all, Ubuntu only complains that it needs the GPG keys in /etc/apt/trusted.gpg.ddirectory (Source: IT’S FOSS).

sudo cp /etc/apt/trusted.gpg /etc/apt/trusted.gpg.d
copying *.gpg files

Run an update again and you should see no warnings:

sudo apt update
zero warnings now

Check the version of Nginx to be installed

apt policy nginx
overview nginx version

You can also see the available versions of Nginx here: http://nginx.org/en/download.html

Then install the latest one, by the time of writing this article it has been nginx=1.22.1–1~jammy

sudo apt install nginx=1.22.1-1~jammy
install Nginx

Start your Nginx webserver:

sudo systemctl start nginx
start Nginx

Ensure Nginx is enabled to start automatically on reboot:

sudo systemctl enable nginx
enable Nginx

Check the status of your web server:

sudo systemctl status nginx
Nginx status

Just to make sure that our configuration was successful go ahead and restart your Linode instance by navigating back to: “Linode dashboard” -> “Linodes” -> click on your label for your server instance -> click on “Reboot

Linode reboot

When your Linode instance is up and running again. Jump back to your terminal and connect again with your Linode instance.

ssh

Make sure Nginx is running with:

service nginx status
service nginx status

Time to jump back again to the Linode dashboard and copy out the IP-Address of your instance

Linode dashboard IP-Address

Paste the IP-Address in a web browser and you should see the Nginx welcome message.

web browser

Configure two demo NodeJS apps

First, install NodeJS

Install the Node Version Manager (NVM) for Node.js. This program helps you manage different NodeJS versions on a single system.

sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
get nvm

To start using nvm in the same terminal run the following commands:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
export nvm

Verify that you have access to NVM by printing its current version:

nvm --version
nvm — version

Install NodeJS:

nvm install 18.13
nvm install 18.13

Use NVM to run your preferred version of NodeJS:

 nvm use 18.13
nvm use 18.13

Configure the demo Backend app

Create a directory for the backend example app:

mkdir node_backend_app && cd node_backend_app
create and navigate to the folder

Initialize a NodeJS app in the directory and accept all defaults when prompted, just press Enter

npm init
npm init

Install ExpressJS:

npm install --save express
npm install — save express

Use Vim (or any other text editor) to create app.js just type:

vim app.js
vim app.js

If you use Vim type i you are now in INSERT mode and add these lines:

const express = require('express')
const app = express()

app.get('/api', (req, res) => res.send('Hello Backend!'))

app.listen(9090, () => console.log('Node.js for the backend listening on port 9090.'))

Close Vim ESC + : + wq!

Run the app:

node app.js
node app.js

In a separate terminal window, use curl to verify that the app is running on localhost:

curl localhost:9090/api
curl localhost:9090/api

Configure the demo Frontend app

Spoiler alert these steps will be similar to the “Configure the demo Backend app”. Navigate to your home directory and type:

mkdir node_frontend_app && cd node_frontend_app
create and navigate to the frontend folder

Initialize a NodeJS app in the directory and accept all defaults when prompted by pressing Enter.

npm init
npm init

Install ExpressJS:

npm install --save express
npm install — save express

Use Vim (or any other text editor) to create app.js just type:

vim app.js
vim app.js
const express = require('express')
const app = express()

app.get('/', (req, res) => res.send('Hello Frontend!'))

app.listen(9091, () => console.log('Node.js for the backend listening on port 9091.'))

Close Vim ESC + : + wq!

Run the app:

node app.js
node app.js

In a separate terminal window, use curl to verify that the app is running on localhost:

curl localhost:9091
curl localhost:9091

With that, we conclude the second part of this tutorial series. If you found it useful and informative, give it a clap. Here is Part 3:

Don’t forget to check out the video version of this article on our YouTube channel at https://www.youtube.com/@habibicoding.

Big shout out to Anton Putra’s tutorial and his documentation on GitHub.

I would also like to extend my gratitude to the Linode team for their comprehensive documentation, check also check out their video. I highly recommend taking a look at their resources as well.

--

--