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:
- Nginx → Reverse proxy and load balancer
- PM2 → Process manager for Node.js
- 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
- Ubuntu server (20.04 or 22.04 recommended)
- Root or sudo access
- A domain name pointing to your server’s IP
- Basic knowledge of Linux terminal
Step 1: Update Your Server
Always start by updating packages:
Step 2: Install Node.js & npm
The best way is to use NodeSource or nvm.
Using NodeSource:
Verify installation:
Step 3: Install PM2
PM2 is a process manager that keeps your Node.js app running, restarts it on crashes, and manages multiple instances.
Start your app:
Check status:
Step 4: Auto-Start PM2 on Boot
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:
Check status:
Step 6: Configure Nginx as Reverse Proxy
Create a config file:
Paste the following:
Enable config and restart Nginx:
Step 7: Secure with SSL (Let’s Encrypt)
Install Certbot:
Obtain SSL:
Auto-renew:
Step 8: Scaling with PM2 Cluster Mode
To utilize multiple CPU cores:
This runs your app across all available cores.
Step 9: Logging & Monitoring
PM2 provides logs:
Monitor performance:
Optional: Enable Keymetrics (pm2.io) for advanced monitoring.
Step 10: Firewall Configuration
If using UFW (Uncomplicated Firewall):
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:
- APIs
- Real-time apps
- SaaS platforms
- 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.