Want to run your Node.js app on a VPS with auto-start, HTTP to HTTPS redirection, and a free SSL certificate?

This step-by-step guide will walk you through:

  • Running a Node.js app with PM2
  • Setting up Nginx as a reverse proxy
  • Securing your site with SSL (HTTPS) using Certbot

🧠 What is PM2 and Why Use It?

PM2 is a process manager for Node.js apps. It helps you:

  • Run your app in the background
  • Automatically restart it if it crashes
  • Start your app on system boot
  • Monitor performance and logs

Without PM2, your app stops as soon as you close the terminal. PM2 solves that.


πŸ› οΈ Step 1: Install PM2 Globally

Make sure you have Node.js and npm installed. Then install PM2:

sudo npm install -g pm2

Start your Node.js app using PM2:

pm2 start app.js --name "backend-app"

πŸ” Replace app.js with your entry file (e.g., index.js, server.js).

Make PM2 auto-start your app on server reboot:

pm2 startup
pm2 save

🌐 Step 2: Set Up Nginx as a Reverse Proxy

Install Nginx (if not already):

sudo apt update
sudo apt install nginx

Edit the Nginx configuration:

sudo nano /etc/nginx/sites-available/default

Replace with the following config (update example.com and port):

server {
    server_name example.com;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        proxy_pass http://localhost:5000;  # your Node.js app port
        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;
    }
}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    }

    listen 80;
    server_name example.com;
    return 404;
}

Reload Nginx to apply:

sudo nginx -t
sudo systemctl reload nginx

πŸ”’ Step 3: Secure Your App with SSL (HTTPS)

Install Certbot (Let’s Encrypt SSL tool):

sudo apt install certbot python3-certbot-nginx

Run the Certbot command:

sudo certbot --nginx -d example.com -d www.example.com

Enter your email when prompted. Certbot will:

  • Generate SSL certificates
  • Automatically update your Nginx config
  • Redirect all HTTP traffic to HTTPS

Now open your website in the browser β€” it should redirect from http:// to https://.


βš™οΈ Useful PM2 Commands

  • Restart app:

bash pm2 restart backend-app

  • View logs:

bash pm2 logs backend-app

  • Delete app:

bash pm2 delete backend-app

  • List all apps:

bash pm2 list

🧠 Final Thoughts

Using PM2 + Nginx + Certbot gives you a stable, secure, and production-ready environment for your Node.js app β€” all on your own VPS.

Next step? Set up auto-deployment with GitHub Actions. Or reach out if you want a guide for that too!


Let me know if you want SEO metadata (title, description, slug) for this post as well.