What is PM2?
PM2 is a process manager for Node.js applications. It is highly used and recommended in production environments. Following are its standout features:
- It is very easy to configure and use
- It comes with its own load balancer that makes sure application is available in case of exception
- Automatically reloads changes
- Provides analytics platform (keymetrics) to see what is going on with server
Execute the following command to install PM2:
npm install pm2 -g
Running your server with PM2
Move inside your project directory. I assume you have app.js as your startup file.
Execute following command to see the help:
This will show all the options as well as examples of running the server.
To start your server, execute the following command:
pm2 start app.js -i 0 --name "any_name_you_want"
pm2 start app.js -i 0 --name test_server
Now our web application is ready on port defined in app.js. For example:
To restart your server, execute:
pm2 restart test_server
To stop your server, execute:
pm2 stop test_server
To kill pm2 process, execute: