Deploy Express.js Application on Oracle Cloud + Domain Name + Free SSL Certificate
Step by step tutorial on how to deploy your Express.js project on Oracle Cloud and how to link your domain name to your Oracle Instance with IP address, and how to get free HTTPS on your domain name.
 27-Nov-2022
Created By : Saurabh Verma

Prerequisite

  • Oracle Cloud Free Tier Account. You can see details here.
  • Compute instance with assigned public IP address.
  • SSH credential to login into the vm.

When you create an instance, you will be asked to download ssh keys and assign ipv4 address.

1. SSH into the instance using below command

ssh -i ./<private_ssh_key> <username>@<ipaddress>
  • Example:
ssh -i ./ssh-private.key ubuntu@192.168.0.1

2. Make sure your instance is up to date.

sudo apt update
sudo apt upgrade

3. Install node.js

  • you can verify if you already have node.js installed in your system by running the below command
node -v

If you want to install the other version than 18.x, follow this link.

  • Run the below command to install node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

Node.js and NPM should be installed by now. You can verify by running the command node -v; npm -v

4. Place your express project, or you can create a sample express.js project by following the below steps.

  • Create a project directory and run the below command
npm init -y
npm install express
  • Create a file named **app.js** at the same location, and paste below content
const express = require('express');
const app = express();

app.use(express.json());

app.get('/', (req,res) => {
    return res.json({message: 'Welcome to Express Project');
});

app.listen(3000, console.log('APP running on PORT 3000'));

I have hard-coded PORT number for the shake of simplicity, you can use dotenv package to get those details from environment file.

  • Modify your 'package.json' according to sample shown below:
{
  "name": "express-project",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license":"ISC",
  "dependencies": {
    "express": "^4.18.2"
  }
}

5. Start your application

  • Test your project by running below command
npm start

You should see the console log; Terminate the process by pressing CTRL+C

  • Install PM2 to run your node application in background
sudo npm i pm2 -g
  • Run your app.js with pm2
pm2 start app.js
  • You can test your application is up on not by running curl command
curl localhost:3000

6. Add Ingress Rules to your instance subnet to allow HTTP (80) and HTTPS (443)

  • Go to Compute-> Instance -> Instance Details and click on subnet name on right panel

instance details

  • Click on Add Security List

Subnet detail

  • Click on Add Ingres List

Security List

  • Save Ingres Rules

Ingres Rules

7. Install nginx to reverse proxy your project port to HTTP.

npm i nginx
  • Once installed, you can check if it's running or not by running below command
sudo systemctl status nginx

8. Install firewalld and enable HTTP and HTTPS service in linux firewall.

sudo apt install firewalld
  • Run below command to enable http and https service and reload the firewall
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Once done, you can go to your public IP address in your browser, you can see the nginx homepage.

9. Configure nginx to proxy to your application

sudo vi /etc/nginx/sites-available/default
  • Change the file configuration as shown below
server_name _;
location / {
  proxy_pass http://localhost:3000;
}
  • Save the file and restart nginx
sudo systemctl restart nginx

Once done, you can go to your public IP address in your browser, you can see your express js project running! Congratulations for coming this far.

10. Assign your domain name to server

  • Go to your domain name provider, and in your domain name DNS configuration, add your details as sample shown in below image

ssh command

It will take few minutes or hours to update. After that, you can now try going to domain name instead of your ip address, and you can still open the same express server!

11. Get free SSL certificate from letsencrypt using certbot

Follow the steps from official site to install certbot on your instance. or you can follow along with me.

sudo snap install core;sudo snap refresh core
sudo apt-get remove certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
  • Once done, change your server name in /etc/nginx/sites-available/default as shown below
server_name your_domain_name www.your_domain_name;
  • Run below command to add SSH to your nginx configuration file automatically
sudo certbot --nginx

Enter the details, and congratulations, you have the SSL certificate installed on your server for your domain.

Happy Coding ❤️