Deploy Node.js on VPS with PM2, NGINX & Free SSL

This guide will show you how to deploy a Node.js application on a VPS using PM2 for process management, NGINX as a reverse proxy, and Let's Encrypt for free SSL certificates.

๐Ÿ’ก Recommended VPS for this tutorial: ScalaHosting High-Performance VPS โ€“ Fast, reliable, and perfect for Node.js deployment.
ScalaHosting VPS

1. Connect to Your VPS

Open your terminal (or PuTTY on Windows) and use your SSH credentials:

ssh root@your_server_ip
๐Ÿš€ Donโ€™t have a VPS yet? Get one here and follow along instantly .

2. Update Server

sudo apt update && sudo apt upgrade -y

3. Install Node.js & npm

Install the latest LTS version from NodeSource:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs

Check versions:

node -v
npm -v

4. Install PM2

PM2 is a process manager for Node.js apps:

sudo npm install pm2@latest -g

5. Upload Your Node.js App

Clone from GitHub:

git clone https://github.com/your/repo.git
cd repo

Or upload via SFTP (FileZilla, WinSCP). Then install dependencies:

npm install

6. Start Your App with PM2

pm2 start app.js --name "myapp"
pm2 save
pm2 startup

7. Install NGINX

sudo apt install nginx -y

Check status:

sudo systemctl status nginx

8. Configure NGINX as Reverse Proxy

Create a config file:

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

Add:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    location / {
      proxy_pass http://127.0.0.1:3000; # 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;
  }
}

Enable site & reload NGINX:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

9. Point Domain to VPS

In your domain registrar DNS settings:

  • A Record โ†’ your_server_ip

Wait 5โ€“30 minutes for DNS propagation.

10. Install Letโ€™s Encrypt SSL

sudo apt install certbot python3-certbot-nginx -y

Run:

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

11. Auto-Renew SSL

sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer

Test renewal:

sudo certbot renew --dry-run
โœ… Done! Your Node.js app is now running with:
  • PM2 for process management
  • NGINX as reverse proxy
  • Free SSL from Letโ€™s Encrypt
Made with UnminifyDev