Server Setup Guide: Nginx + PM2 + Node.js on Ubuntu

Server Setup Guide: Nginx + PM2 + Node.js on Ubuntu
AuthorBy Dharmendra Singh Yadav· 3 min readDevOps & Deployment👁️ 5 views

When deploying Node.js applications to production, stability, performance, and scalability are key. While Node.js is fantastic for building APIs and web apps, running it directly with node app.js is not production-grade.

To achieve a robust deployment, you need:

  1. Nginx → Reverse proxy and load balancer
  2. PM2 → Process manager for Node.js
  3. Ubuntu → A reliable and developer-friendly Linux distribution

In this guide, I’ll walk you through setting up a production-ready server with Nginx, PM2, and Node.js on Ubuntu.

Prerequisites

  1. Ubuntu server (20.04 or 22.04 recommended)
  2. Root or sudo access
  3. A domain name pointing to your server’s IP
  4. Basic knowledge of Linux terminal

Step 1: Update Your Server

Always start by updating packages:


sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js & npm

The best way is to use NodeSource or nvm.

Using NodeSource:

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

Verify installation:

node -v
npm -v

Step 3: Install PM2

PM2 is a process manager that keeps your Node.js app running, restarts it on crashes, and manages multiple instances.

sudo npm install -g pm2

Start your app:

pm2 start app.js --name "myapp"

Check status:

pm2 status

Step 4: Auto-Start PM2 on Boot

pm2 startup systemd
pm2 save
sudo systemctl enable pm2-$(whoami)

This ensures your Node.js app runs automatically after a server reboot.

his ensures your Node.js app runs automatically after a server reboot.

Step 5: Install & Configure Nginx

Install Nginx:

sudo apt install nginx -y

Check status:

systemctl status nginx

Step 6: Configure Nginx as Reverse Proxy

Create a config file:

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

Paste the following:

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

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

Enable config and restart Nginx:

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

Step 7: Secure with SSL (Let’s Encrypt)

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Obtain SSL:

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

Auto-renew:

sudo systemctl enable certbot.timer

Step 8: Scaling with PM2 Cluster Mode

To utilize multiple CPU cores:

pm2 start app.js -i max --name "myapp"

This runs your app across all available cores.

Step 9: Logging & Monitoring

PM2 provides logs:

pm2 logs myapp

Monitor performance:

pm2 monit

Optional: Enable Keymetrics (pm2.io) for advanced monitoring.

Step 10: Firewall Configuration

If using UFW (Uncomplicated Firewall):

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Benefits of Nginx + PM2 + Node.js

Nginx handles static files, SSL termination, and load balancing.

PM2 ensures uptime and crash recovery.

Node.js runs your app efficiently.

✅ Together → Production-ready, secure, and scalable setup.

Deploying Node.js apps with Nginx + PM2 on Ubuntu gives you a stable, secure, and fast production environment.

This setup is battle-tested for:

  1. APIs
  2. Real-time apps
  3. SaaS platforms
  4. Enterprise dashboards

At DharmSy, I’ve used this stack for projects handling tens of thousands of concurrent users with minimal downtime.

🚀 With this guide, you now have a blueprint for setting up your own production server.

🤝 Let’s Turn Ideas into Impact

Great startups are built on strong collaborations. If you’re a founder with a vision, I’ll bring the technical expertise to design, build, and scale your product — so we can create something game-changing together.

WhatsApp Icon