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.…