How To Run a NodeJS application in Ubuntu 22 with SSL via Letsencrypt

You are currently viewing How To Run a NodeJS application in Ubuntu 22 with SSL via Letsencrypt

Running a NodeJS application in Ubuntu 22 with SSL via Letsencrypt is a great way to ensure that your application is secure and accessible to all users. SSL, or Secure Sockets Layer, is a protocol for establishing secure connections between networked computers. Letsencrypt is a free, open-source certificate authority that makes it easy to obtain and install SSL certificates for your applications.

Here’s a step-by-step guide on how to run a NodeJS application in Ubuntu 22 with SSL via Letsencrypt:

1 Install Nginx in Ubuntu 22

To install Nginx in Ubuntu, login to your terminal via shell and run the below command.

sudo apt update -y 
sudo apt install nginx -y

This command will Nignx into your Ubuntu machine.

2. Install NodeJS and npm (Node Package Manager)

To Install NodeJS and npm (Node Package Manager) run the below command

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

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" # This loads nvm

To verify that nvm has been installed, do run this command

command -v nvm

After that, run this command to install Node 16 using NVM

nvm install 16

3. Create an Nginx server block for the domain/subdomain

Here is one sample Nginx server block for a Nodejs application. Create a configuration file in this location

sudo vi /etc/nginx/conf.d/nodejs.conf
server {
    listen 80;
    server_name atiqur.xyz;
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

replace the domain name with your domain name.

Once you created the server block then restart the nginx by running this command

sudo systemctl restart nginx 

4. Create a simple Nodejs application with express

Create a simple Nodejs application with express with the below code.

sudo vi index.js

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

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

This code example runs a Nodejs application in port 3000 and then displays a hello world message.

Create a package.json file in the same directory

sudo vi package.json

{
  "name": "atiq-app",
  "version": "1.0.0",
  "description": "A simple Node.js Express application",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  },
  "author": "Your Name",
  "license": "MIT"
}

Then run npm install to install the necessary node modules

5. Install PM2 to run this as a background job

PM2 is a process manager for Node.js applications that allows you to run your application in the background as a daemon. To run your Express application with PM2, you first need to install PM2 using the command:

npm install pm2 -g

This installs PM2 globally on your system.

Once PM2 is installed, you can start your application with the command:

pm2 start index.js --name "atiq-app"

6. Install SSL with Letsencrypt

For SSL you need to install Certbot by running the commands below

sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo certbot --nginx

To ensure that your SSL certificate is always up-to-date, you can set up a cron job to renew it automatically. To do this, open the crontab file by running the following command:

sudo crontab -e

Then add the code to run it on a regular interval for SSL renewal

0 3 * * * sudo certbot renew >/dev/null 2>&1

Conclusion

In conclusion, this article provides a comprehensive guide on how to run a NodeJS application in Ubuntu 22 with SSL via Letsencrypt. It covers all the necessary steps, including the installation of NodeJS, setting up a domain and SSL certificate, and configuring the application to run over HTTPS. By following the instructions outlined in the article, developers can easily secure their NodeJS applications and ensure that they are accessible over a secure connection. Additionally, the use of Letsencrypt makes it easy to obtain and renew SSL certificates, making it an excellent choice for developers who want to secure their applications without incurring additional costs.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply