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:

  1. It is very easy to configure and use
  2. It comes with its own load balancer that makes sure application is available in case of exception
  3. Automatically reloads changes
  4. Provides analytics platform (keymetrics) to see what is going on with server

Installing PM2

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:

pm2

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"

like:

pm2 start app.js -i 0 --name test_server

Now our web application is ready on port defined in app.js. For example:

http://localhost:3000

To restart your server, execute:

pm2 restart test_server

To stop your server, execute:

pm2 stop test_server

To kill pm2 process, execute:

pm2 kill

Use generators for creating Node.js projects