```html How I Used PM2 to Run a Node.js App in Production

Himanshu Joshi

← Back to Blog

How I Used PM2 to Run a Node.js App in Production

Published on June 7, 2026 by Himanshu Joshi

PM2 Node.js Production Deployment
Summary: In this article, I share my practical experience using PM2 to run a Node.js or Next.js application in production. I explain why normal terminal-based running is not enough, how PM2 helps keep the app alive, and what I learned while managing a real live project.

When I first started deploying web applications, I used to think that running a project with a simple command like npm run dev or npm start was enough. It works on a local machine, but production is different. A production app should keep running even after the terminal is closed, the server restarts, or the process crashes.

While working on real deployment, I understood why a process manager is needed. This is where PM2 becomes very useful. PM2 helps run Node.js applications in the background, monitor them, restart them when required, and manage logs.

Why Normal npm Start Is Not Enough

If we run a Node.js application directly from the terminal, it stays active only while that terminal session is active. If the SSH connection closes or the process stops, the application can go down. This is not acceptable for a live website.

For example, a Next.js or Express app may run on port 3000. If the process stops, Nginx may still be active, but the backend application will not respond. Users will see an error, and the website may look broken.

A live application needs better process control. It should run in the background and restart automatically if something goes wrong.

What Is PM2?

PM2 is a process manager for Node.js applications. It allows us to run apps in the background, check their status, view logs, restart processes, and configure startup behavior after server reboot.

In simple words, PM2 acts like a manager for the app process. Instead of manually keeping the app running, PM2 handles the process and gives useful commands to monitor it.

Installing PM2

PM2 is usually installed globally on the server using npm.

npm install -g pm2

After installation, I can check whether PM2 is available by running:

pm2 --version

If this command returns a version number, PM2 is installed successfully.

Starting a Node.js App with PM2

For a normal Node.js app, PM2 can start the main server file directly. For example:

pm2 start server.js --name "my-node-app"

For a Next.js app, I used PM2 with npm start:

pm2 start npm --name "my-next-app" -- start

The name is important because it helps identify the app later when checking status, logs, or restarting the process.

Checking App Status

After starting the app, I checked whether it was running properly:

pm2 status

This command shows app name, process id, status, memory usage, CPU usage, and restart count. It is very useful because it gives a quick overview of all apps running under PM2.

If the status is online, the app is running. If the status is errored or stopped, then logs should be checked.

Viewing Logs

Logs are very important in production. When something goes wrong, logs help us understand the actual reason. PM2 provides a simple command to view logs:

pm2 logs

To check logs for a specific app:

pm2 logs my-next-app

Logs helped me debug build issues, environment variable problems, server errors, and runtime exceptions. Without logs, production debugging becomes guesswork.

Important learning: In production, logs are not optional. They are one of the most useful tools for understanding why an app is failing.

Restarting the App

After updating code or changing configuration, the app needs to be restarted. PM2 makes this simple:

pm2 restart my-next-app

Sometimes I restart all PM2 processes:

pm2 restart all

Restarting through PM2 is better than manually killing processes because PM2 keeps the process organized and easy to manage.

Stopping and Deleting a Process

If I need to stop an app:

pm2 stop my-next-app

If I need to remove the process from PM2:

pm2 delete my-next-app

These commands are useful when changing the app name, changing deployment structure, or cleaning old processes.

Saving PM2 Process List

One important step after starting the app is saving the PM2 process list:

pm2 save

This tells PM2 to remember the current running processes. Without saving, PM2 may not restore the same processes later.

Auto Start After Server Reboot

A production server can restart due to updates, maintenance, or unexpected reasons. The app should automatically start again after reboot. PM2 provides a startup command for this.

pm2 startup

PM2 usually prints another command that needs to be copied and executed. After that, I run:

pm2 save

This helps the app come back online automatically after a server restart.

How PM2 Fits with Nginx

In my deployment setup, PM2 runs the Node.js or Next.js app on an internal port such as 3000. Nginx works as a reverse proxy and sends public domain traffic to that internal app port.

This means users visit the website through a domain, Nginx receives the request, and PM2 keeps the app process alive behind the scenes.

Both PM2 and Nginx are important. PM2 manages the app process. Nginx manages public traffic and domain routing.

Common Problems I Faced

While using PM2 in real deployment, I faced some common issues:

Most of these problems were solved by checking pm2 logs, verifying environment variables, and checking Nginx configuration.

Useful PM2 Commands I Used

These are the commands I found most useful:

pm2 status
pm2 logs
pm2 logs my-next-app
pm2 restart my-next-app
pm2 stop my-next-app
pm2 delete my-next-app
pm2 save
pm2 startup

These commands are enough for most basic production deployments.

What I Learned

PM2 taught me that deployment is not only about uploading code to a server. A live app needs process management, logging, restarting, and monitoring. Without these things, a website can go down easily and it becomes difficult to understand the reason.

I also learned that production stability comes from small correct steps. Running the app with PM2, checking logs, saving the process list, and configuring startup are simple steps, but they make the deployment much more reliable.

Conclusion

PM2 is one of the most useful tools I used for running Node.js and Next.js apps in production. It keeps the app running in the background, helps with logs, allows easy restart, and supports automatic startup after server reboot.

For any developer deploying a Node.js project on a server, learning PM2 is very helpful. It makes the difference between a temporary local-style setup and a more stable production-ready setup.

```