Blog Photo

How to Setup a VPS for Node.js Backend with Nginx, PM2, and Let's Encrypt

Want to take your Node.js backend live on a VPS like a pro? In this guide, we’ll walk you through setting up a VPS, configuring Nginx as a reverse proxy, managing your app with PM2, and securing it with Let's Encrypt for free SSL! 🔐✨ Whether you're a beginner or an experienced developer, this step-by-step tutorial will ensure your backend runs smoothly, securely, and efficiently. Say goodbye to unreliable hosting and hello to full control over your deployment! 🖥️🔥

Death Code - 2/6/2025, 8:03:07 AM

How to Setup a VPS for Node.js Backend with Nginx, PM2, and Let's Encrypt - DeathCode

Introduction

If you want to host your Node.js backend on a VPS (Virtual Private Server), you need a proper setup for better performance, security, and reliability. In this guide, we will learn:

  • How to set up a VPS for hosting a Node.js backend
  • How to use Nginx as a reverse proxy
  • How to use PM2 for managing your Node.js process
  • How to secure your site using Let's Encrypt SSL (optional)

Step 1: Set Up Your VPS

First, you need a VPS. You can use providers like DigitalOcean, Linode, Vultr, AWS, etc.

Connect to Your VPS

After getting a VPS, connect to it using SSH:

ssh root@your-vps-ip

If prompted, enter your password.

Create a New User (Security Practice)

Never run applications as root. Create a new user:

adduser myuser

Give sudo access:

usermod -aG sudo myuser

Now, switch to the new user:

su - myuser

Step 2: Install Node.js and Git

# Download and install fnm:
curl -o- https://fnm.vercel.app/install | bash
# Download and install Node.js:
fnm install 22 # version: 22 (enter latest version)
sudo apt install git

Verify installation:

node -v
npm -v

Install Git:

sudo apt install git

Step 3: Clone and Run Your Node.js App

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

Move into the project folder and install dependencies:

cd your-repo
npm install

Run the application:

node server.js

But running like this means the app stops if the terminal closes. We fix this using PM2.

Step 4: Install and Use PM2

npm install -g pm2

Start your app with PM2:

pm2 start server.js --name "myapp"

Save the PM2 process to restart on reboot:

pm2 save
pm2 startup

Step 5: Install Nginx as a Reverse Proxy

sudo apt install nginx

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Edit the Nginx config file:

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

Add this configuration:

server {
    listen 80;
    server_name yourdomain.com;
 
    location / {
        proxy_pass http://localhost: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;
    }
}

Save and exit. Test Nginx:

sudo nginx -t

If no errors, restart:

sudo systemctl restart nginx

Step 6: Secure with Let's Encrypt (Optional)

sudo apt install certbot python3-certbot-nginx

Run:

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

Certbot will auto-configure SSL. To renew automatically:

sudo certbot renew --dry-run

Step 7: Test Everything

Now visit http://yourdomain.com and your Node.js app should be live.

Tips and Tricks

  • Always use a non-root user.
  • Keep your server updated: sudo apt update && sudo apt upgrade
  • Use a firewall: sudo ufw allow OpenSSH and sudo ufw enable
  • Monitor logs with PM2: pm2 logs
  • Use pm2 restart myapp after updates.

Conclusion

Now you have a fully hosted Node.js backend with Nginx and PM2 on a VPS. This setup ensures stability, security, and scalability.

© 2024 DeathCode. All Rights Reserved.