PM2 is an advanced, production process manager for Node.js that auto-restarts Node apps after crashes or file changes. PM2 can also configure your server with a few simple commands to automatically statup your node apps when the server reboots. In some respect it's similar to forever and nodemon but it has many more powerful features that make it a better choice for running Node.js in production.

PM2 Official site: http://pm2.keymetrics.io/

This tutorial assumes you already have Node.js installed on a server. If you don't then go back and run through my previous tutorial. Build and Install Node.js from source on Amazon EC2 Linux.

Let's install PM2 on an Amazon EC2 Linux instance and explore some of it's features. If you're running a different flavor of Linux or on a Mac these commands should be the same.

On an AWS EC2 Linux instance we need to add npm to sudo's path so it can find the command. By default npm gets installed in /usr/local/bin which isn't in sudo's path. To fix we will edit the secure_path in the /etc/sudoers file using the nano text editor.

$ sudo nano /etc/sudoers

Find the line in the file that begins with:

Defaults    secure_path = 

and append the path :/usr/local/bin See screen shot below for reference.

alt

Type ctrl+o to save (WriteOut) the file to disk. Then crtl+x to exit the nano editor.

Now that we've told sudo where to find the npm command we can install PM2 and since it's a command line tool we need to install it globally using the -g option.

$ sudo npm install pm2 -g

Verify that PM2 is installed.

$ pm2 -v

You should get some output like...

[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
1.0.0

Now let's create a simple "Hello World" Node.js application and use it to experiment with PM2.

Move to the home directory and create a www directory.

$ cd /home/ec2-user or $ cd ~ if not using AWS Linux.

$ mkdir www

$ cd www

Create an app.js file.

$ sudo nano app.js

The command above opens the nano text editor with a blank document. Copy/Paste the text below into it.

var http = require('http');

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/html"});
  response.end("<h1>Hello from Node.js</h1>\n");
});

server.listen(8000);

console.log("Node.js is listening on port 8000");

Type ctrl+o to save (WriteOut) the app.js file to disk. Then crtl+x to exit the nano editor.

Startup a node process using PM2. The flags are optional but the --watch flag automatically restarts your app when a file changes in the current directory or its subdirectories.

$ pm2 start app.js --name "helloworld" --watch

If you're running a production application you can "feed" the NODE_ENV environment variable into pm2 like this ...

$ NODE_ENV=production pm2 start app.js --name "helloworld" --watch

The --watch flag has more powerful features. For more info read the official docs here.

http://pm2.keymetrics.io/docs/usage/watch-and-restart/

PM2 can generate a server startup script and remember your processes in order to bring them all back online the server reboots.

Let's generate a startup script for the server. I'm running on an Amazon EC2 Linux instance so I chose amazon as the platform.

$ pm2 startup amazon

The platform can be any one of the following. Choose darwin if you're running through this tutorial on a Mac.

ubuntu, centos, redhat, gentoo, systemd, darwin, amazon.

However, when I run the command above PM2 complains "You have to run this command as root." and then echos back the appropriate command. How nice of PM2! Let's run it.

$ sudo su -c "env PATH=$PATH:/usr/local/bin pm2 startup amazon -u ec2-user --hp /home/ec2-user"

You'll see some output like this.

Let's instruct PM2 to save all running processes so that it can remember and resurrect them after a server reboot.

$ pm2 save

Now that PM2 has installed your startup script and saved your list of processes, reboot your server to make sure that PM2 automatically restarts your Node.js app(s).

$ sudo reboot

When the server comes back online PM2 will have automatically started itself up and then looked at your saved processes (from the pm2 save command) and start them all up.

Experiment with the basic PM2 commands and then read through the PM2 official docs. It has some really awesome and powerful features that we will explore in future tutorials.

Show all running apps and their status
$ pm2 list

Show more details about an app
$ pm2 show helloworld

View logs for one of your apps
$ pm2 logs helloworld then ctrl+c to exit log view.

Stop your node app.
$ pm2 stop helloworld

Start your app.
$ pm2 start helloworld