🚀 How to Run Your npm start App on a VPS with PM2 (and Keep It Alive Forever)

9/7/20255 min read250 views
BusinessTechnology
#Learning#Web Development#programming

Deploying a Node.js application on a VPS is exciting — you finally get to share your project with the world. But if you’ve tried running your app with npm start over SSH, you might have noticed something frustrating:

As soon as you close your terminal or lose internet connection, your app shuts down. That’s because the process is tied to your SSH session.

In this article, I’ll walk you through how to use PM2, a production-ready process manager, to run your Node.js app so it stays alive 24/7, even if your VPS reboots. We’ll cover installation, setup, monitoring, and best practices.

Why You Need PM2

When you run:

npm start
  • Your app is launched inside the SSH session.
  • Closing SSH sends a “hangup” signal (SIGHUP) that kills the process.
  • If your app crashes, it won’t automatically restart.

PM2 solves this by:

  • Running your app as a daemon (independent background service).
  • Restarting it if it crashes.
  • Starting it again after a VPS reboot.
  • Giving you handy commands for monitoring, logs, and scaling.

Step 1: Install PM2

SSH into your VPS and install PM2 globally:

npm install -g pm2

Check the version to confirm it’s installed:

pm2 -v

Step 2: Start Your npm start App with PM2

If your project has a start script in package.json, run:

pm2 start npm --name "myapp" -- start

Explanation:

  • npm → tells PM2 to run npm.
  • --name "myapp" → gives your process a readable name.
  • -- start → passes the start argument to npm, which runs npm start.

Now, your app is running in the background!

Step 3: Monitor Your App

You can check the status of your app:

pm2 list

View logs:

pm2 logs myapp

Stop logs with Ctrl + C.

If you want a top-like dashboard:

pm2 monit

Step 4: Keep Your App Alive After Reboot

By default, PM2 processes vanish after a reboot. To fix this:

pm2 startup

This will print a command — copy and paste it back into your terminal.

Then, save your current PM2 process list:

pm2 save

Now your app will automatically restart after every reboot.

Step 5: Manage Your App

Here are the most useful PM2 commands:

pm2 restart myapp   # restart the app
pm2 stop myapp      # stop the app

Step 6: Extra Tips for Production

Use NGINX as a reverse proxy → run your app on port 3000 (or another port) and let NGINX serve it on port 80/443 with SSL.

Enable monitoring → PM2 can send metrics to Keymetrics.io

Cluster mode → use pm2 start app.js -i max to run multiple instances and maximize CPU usage.

Conclusion

Running your Node.js app with npm start alone isn’t enough for production. With PM2, you get:

✅ Persistent background processes

✅ Automatic restarts on crash or reboot

✅ Simple monitoring and logs

✅ Scalability with clustering

Now, your app will stay alive forever — even if you close SSH, lose connection, or restart your VPS.

Recent posts