Deployment
How to Set Up PM2 With Nginx on Ubuntu
A practical PM2 and Nginx setup guide for Ubuntu with commands, reverse proxy config, startup persistence, and debugging tips.
Advertisement
Why PM2 and Nginx Work Well Together
PM2 and Nginx solve two different production problems. PM2 keeps your Node.js application running as a managed process, while Nginx handles incoming HTTP and HTTPS traffic at the edge. Together they form one of the most common production patterns for self-hosted Node applications on Ubuntu.
This setup is practical because it is easy to understand and easy to recover when something breaks. PM2 gives you logs, restarts, and boot persistence. Nginx gives you domain routing, reverse proxying, TLS support, and cleaner public access.
Install Node.js, Nginx, and PM2
Start with the core runtime layer first. You want Node.js installed, Nginx enabled, and PM2 available globally before you try to launch the app.
sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs nginx
sudo npm install -g pm2
node -v
npm -v
pm2 -v
sudo systemctl enable nginx
sudo systemctl start nginxStart the Node.js App With PM2
Move into your project directory, install packages, and make sure the app can start. Then hand it to PM2 so it runs in the background and survives restarts.
cd /var/www/my-node-app
npm install
pm2 start npm --name "my-node-app" -- start
pm2 list
pm2 logs my-node-app
pm2 save
pm2 startup- Use a clear PM2 process name
- Read PM2 logs before touching Nginx
- Run pm2 save after startup configuration
Configure Nginx as a Reverse Proxy
Once PM2 is running the app, Nginx should proxy traffic from your domain to the local Node.js process. This keeps the app behind 127.0.0.1 and lets Nginx handle domain routing and TLS later.
server {
listen 80;
server_name example.com www.example.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;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}sudo nano /etc/nginx/sites-available/my-node-app
sudo ln -s /etc/nginx/sites-available/my-node-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxEnable HTTPS and Reboot Safety
After the domain works over HTTP, add HTTPS with Certbot. Also make sure PM2 startup has been configured so the process comes back after a server reboot. This is what moves the setup from “working now” to “usable in production.”
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
pm2 saveUseful Commands for Daily Operations
Once the setup is live, you will mostly interact with PM2 and Nginx through a small set of operational commands. Keeping these close at hand makes production support much easier.
pm2 list
pm2 logs my-node-app
pm2 restart my-node-app
pm2 stop my-node-app
sudo systemctl status nginx
sudo nginx -t
sudo tail -f /var/log/nginx/error.logFAQ
Why use PM2 with Nginx on Ubuntu?
PM2 keeps the Node.js app alive and manageable, while Nginx handles public traffic, reverse proxying, domains, and TLS.
Should PM2 or Nginx listen on port 80?
Nginx should listen on the public web ports, while PM2-managed apps usually stay on an internal port like 3000.
What causes 502 Bad Gateway in this setup?
Usually the app is not running under PM2 or Nginx is proxying to the wrong internal port.
Do I need PM2 if I already use Nginx?
Yes, because Nginx handles traffic but does not manage the Node.js app lifecycle like PM2 does.
How do I make the PM2 process survive reboots?
Use pm2 startup, run the generated command, then save the process list with pm2 save.
Related free tools
If you want to turn this topic into action, use one of ShortIQ's free tools for campaign planning, UTM structure, or QR distribution.
Continue Reading
Explore more guides on link shortener SaaS strategy, Bitly alternatives, and white label link management.