Sat Apr 05 2025

Deploying Node.js Apps Using PM2 and Nginx: A Production Guide

Node Js

Article thumbnail

When you’re ready to move your Node.js app from development to production, it’s important to optimize it for performance, reliability, and scalability. Tools like PM2 and Nginx help make this process smoother and more efficient.

In this guide, we’ll show you how to deploy your Node.js app using PM2 (a process manager) and Nginx (a high-performance reverse proxy and load balancer).

Why Use PM2 and Nginx?

Let’s first look at why these tools are commonly used:

PM2:

  • Keeps your app running with automatic restarts.
  • Supports log management, clustering, and zero-downtime reloads.
  • Provides easy process monitoring and management.

Nginx:

  • Acts as a reverse proxy, handling requests between the internet and your Node.js app.
  • Manages SSL (HTTPS) termination.
  • Efficiently serves static files.
  • Adds security and load balancing for better performance.

1. Prepare Your Node.js App

Make sure your app has a proper structure and a single entry point like app.js or index.js.

Example basic index.js:

Sweet Christmas!

2. Install PM2

On your production server (e.g., Ubuntu):

npm install pm2@latest -g

Then start your app with

PM2:pm2 start index.js --name my-node-app

Other useful PM2 commands:

  • View process list: pm2 list
  • View logs: pm2 logs
  • Restart app: pm2 restart my-node-app
  • Enable startup on reboot:

pm2 startup

pm2 save

3. Install and Configure Nginx

Install Nginx:

sudo apt update

sudo apt install nginx

Configure Nginx as a Reverse Proxy:

Create a new Nginx config file:

sudo nano /etc/nginx/sites-available/my-node-app

4. Secure Your Site with HTTPS (Optional but Recommended)

Use Let's Encrypt to add SSL for free:

sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d yourdomain.com

Certbot will handle SSL installation and renewal.

5. Monitor & Maintain

With PM2 and Nginx, you can now monitor and maintain your app more easily:

Monitor with PM2 Dashboard:

pm2 monit

Auto-Restart on File Change (for development only):

pm2 start index.js --watch

Logging:

PM2 automatically saves logs:

~/.pm2/logs/my-node-app-out.log

6. Test Your Deployment

Check everything is working:

  • Visit your domain: https://yourdomain.com
  • Check app logs: pm2 logs
  • Validate Nginx config: sudo nginx -t

Final Thoughts

By combining PM2 and Nginx, you gain:

  • Robustness: Your app restarts on crash or reboot
  • Security: Hide Node.js from the internet using Nginx
  • Scalability: Easily add more Node.js processes or servers
  • Performance: Nginx handles static files and HTTP efficiently

This setup is perfect for most production Node.js applications running on VPS or cloud servers like AWS, DigitalOcean, or Linode

Have questions or tips of your own? Share them in the comments!

Let's Discuss Your Project

Book Your Free Consultation to Get Started!

Continue Reading

Discover more insights and stories

View All Articles