NodeJS with PM2

Setting up NodeJS with PM2 is a fairly straightforward process and shouldn't take much time at all.

Install PM2

The first step is getting to your applications folder:

cd /path/to/your/application  

Once in your applications folder, install pm2:

sudo npm install -g pm2  

Depending on your environment you are running, set the "NODE_ENV" variable. In this example, it will be for production;

echo "export NODE_ENV=production" >> ~/.profile  
source ~/.profile  
pm2 kill  
Start PM2

The next step will be an initial start of the application using pm2:

pm2 start index.js --name test  

Note: You may not be using index.js and you will need to use another file. Additionally you can set the application name to something other than "test".

Once that is done, your NodeJS/pm2 process is running!

There is another option you can use to make sure NodeJS is taking advantage of the multiple cores your system may have. This is the -i flag. For example:

pm2 start index.js --name test -i 8  

This will start up eight PM2 processes in "cluster" mode:

$ pm2 status
┌──────────┬────┬─────────┬───────┬────────┬─────────┬────────┬─────┬────────────┬──────────┐
│ App name │ id │ mode    │ pid   │ status │ restart │ uptime │ cpu │ mem        │ watching │
├──────────┼────┼─────────┼───────┼────────┼─────────┼────────┼─────┼────────────┼──────────┤
│ test    │ 0  │ cluster │ 22126 │ online │ 0       │ 10h    │ 0%  │ 113.6 MB   │ disabled │
│ test    │ 1  │ cluster │ 22132 │ online │ 0       │ 10h    │ 0%  │ 89.1 MB    │ disabled │
│ test    │ 2  │ cluster │ 22142 │ online │ 0       │ 10h    │ 0%  │ 93.8 MB    │ disabled │
│ test    │ 3  │ cluster │ 22148 │ online │ 0       │ 10h    │ 0%  │ 94.5 MB    │ disabled │
│ test    │ 4  │ cluster │ 22158 │ online │ 0       │ 10h    │ 0%  │ 93.1 MB    │ disabled │
│ test    │ 5  │ cluster │ 22164 │ online │ 0       │ 10h    │ 0%  │ 92.5 MB    │ disabled │
│ test    │ 6  │ cluster │ 22174 │ online │ 0       │ 10h    │ 0%  │ 93.0 MB    │ disabled │
│ test    │ 7  │ cluster │ 22180 │ online │ 0       │ 10h    │ 0%  │ 93.1 MB    │ disabled │
└──────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────┴────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

Using cluster mode can be very helpful with ensuring your application has enough power to serve your website (provided you have the CPU power as well!)

You can use the -i max value to automatically set pm2 to use the number of CPU cores you have.

Starting on Boot

You will want to make sure it starts on boot though so there are a few more steps to take.

The first step to making sure the process starts on boot is:

pm2 dump  

This will dump information about the current running process(es) for the next startup.

Second, you will want to run pm2's "startup" command to create the appropriate init script for your OS and enable it at the appropriate runlevel:

sudo pm2 startup <centos|ubuntu|amazon>  

Replace the value after "startup" with the OS appropriate for your environment.

I'm running Gentoo and they do not have an option for startup with the pm2 startup command so I had to create an init script. A copy of the init script is below:

#!/sbin/openrc-run
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

NAME=pm2  
PM2=/usr/lib64/node_modules/pm2/bin/pm2  
pidfile=/home/application/.pm2/pm2.pid  
export PATH=/usr/bin:$PATH  
export PM2_HOME="/home/application/ghost/.pm2"

depend() {  
  need net
  need localmount
  after bootmisc
}

start() {  
  ebegin "Starting pm2"
  start-stop-daemon --start --pidfile ${pidfile} --user myuser:myuser --exec ${PM2} -- resurrect
}

stop() {  
  ebegin "Stopping pm2"
  $PM2 dump
  $PM2 delete all
  $PM2 kill
}

You'll likely need to change a few of the variables around for your environment but it should be a good place to start.

If you have any questions feel free to leave a comment!