Rajput Bhavin Logo
Welcome to

RB ENGINEERING

Back to Blog
MERN StackDeploymentNginxLinux Server

MERN Stack Deployment Guide: PM2 and Nginx

15 February 202610 min

Production Deployment for MERN Stack Applications

Building a MERN application locally is straightforward, but deploying it to a secure production Linux server requires robust configuration. We use PM2 for process management and Nginx as a reverse proxy.

---

1. Process Management with PM2

PM2 keeps your Node.js application running in the background and restarts the process automatically if it crashes:

``bash

Start server in cluster mode


pm2 start dist/server.js -i max --name "mern-backend"

Save the PM2 process list to run on system reboot


pm2 save
pm2 startup
`

---

2. Nginx Reverse Proxy Setup

Nginx routes incoming port 80/443 traffic to your Node.js backend port (e.g., 5000), while handling SSL encryption:

`nginx
server {
listen 80;
server_name api.rajputbhavin.engineer;

location / {
proxy_pass http://localhost:5000;
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;
}
}
``

This architecture separates concerns, speeds up static asset delivery, and safeguards backend APIs from direct web exposure.

Connect Now