Menu

Keep a Node.js App Running 24/7 on AWS EC2 with PM2
📰

Keep a Node.js App Running 24/7 on AWS EC2 with PM2

DEV Community: node·Jahid Hiron·9 days ago
#ar7aXBcG
#devtonodejs#devto#dev#start#enter
Long article (418 words) — no AI summary yet.
Reading 0:00
15s threshold

In my last post, I got my Node app running on EC2 with npm start . It worked until I closed the terminal. Then it was gone.

This post fixes that with PM2. It takes about 5 commands.

Why the app stops

When you run npm start over SSH, the app is a child of your shell. Close the SSH window and the shell exits, so the app exits with it. Ctrl+C does the same thing on purpose.

A server needs the app to run with nobody logged in. That's what a process manager is for.

1. Install PM2

npm i -g pm2

Enter fullscreen mode

Exit fullscreen mode

If you get permission denied , your global npm folder belongs to root. Run it with sudo:

sudo npm i -g pm2

Enter fullscreen mode

Exit fullscreen mode

2. Start the app with PM2

From inside the project folder:

pm2 start index.js --name node-api

Enter fullscreen mode

Exit fullscreen mode

PM2 prints a table with an ID (0) and the name you gave it. The app is now running in the background. You can close your SSH session, and it keeps running.

3.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.