Installing Nginx on Ubuntu Server for Node.js Apps

Proxying a Node.js app and enabling HTTPS with Let's Encrypt

Target: Ubuntu 20.04–24.04 LTS
Use case: Running Node.js app on port 3000 via PM2
Domain: taleem.help (must already point to your droplet's IP via A-record)


Step 1 – Update Your Server

Keep your system up to date:

sudo apt update && sudo apt upgrade -y

Step 2 – Set Up the Firewall (UFW)

Enable UFW to secure your server, allowing only essential ports.

1. Install UFW (if not already installed)

sudo apt install -y ufw

2. Set Default Rules

sudo ufw default deny incoming
sudo ufw default allow outgoing

3. Allow Specific Ports

sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw allow https

Optional (Advanced): Lock SSH to your IP only

sudo ufw allow from YOUR.IP.ADDR.HERE/32 to any port 22 proto tcp

4. Enable UFW

sudo ufw enable
sudo ufw status verbose

Step 3 – Install and Start Nginx

sudo apt install -y nginx
sudo systemctl enable --now nginx

Test by visiting:

http://YOUR_SERVER_IP

You should see the Nginx welcome page.


Step 4 – Create Nginx Server Block

We'll proxy http://taleem.helphttp://localhost:3000

1. Create the config file

sudo nano /etc/nginx/sites-available/taleem.help

2. Paste this configuration

server {
    listen 80;
    server_name taleem.help www.taleem.help;

    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;
    }
}

3. Enable the site and reload Nginx

sudo ln -s /etc/nginx/sites-available/taleem.help /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Now, http://taleem.help (without port 3000) should open your app.


Step 5 – Install SSL (HTTPS) Using Certbot (Let's Encrypt)

1. Install Certbot

sudo snap install core && sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

2. Get SSL Certificates and Configure Nginx

sudo certbot --nginx -d taleem.help -d www.taleem.help

Certbot will:

3. Test Auto-Renewal

sudo certbot renew --dry-run

Step 6 – Final Check

Visit:


Summary

What you did Tool Used
Secured ports UFW
Installed reverse proxy Nginx
Set up HTTPS Certbot (Let's Encrypt)
Reverse proxied Node app http://localhost:3000https://taleem.help

Everything is now production-ready: