How to use pm2 to manage node.js application in production

PM2 can be used to manage and run application in production. For this article we are going to use Ubuntu 14.04.2 LTS. But it should work on Amazon linux or any other Linux with minor changes. Login to your system using user ubuntu (or the user you want to use for running pm2).

Install node and pm2

In case you have not installed node, npm and pm2 you can install these using the following instructions:

$ sudo apt-get install node
$ sudo apt-get install npm
$ sudo npm install -g pm2

Create a node.js hello world server app

create a server app using the following code and save it in hello.js

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080) ;
console.log('Http server running http://127.0.0.1:8080/');

Now run the app using

$ node hello.js
Http server running http://127.0.0.1:8080/

Test the app server using

$ curl  http://127.0.0.1:8080/
Hello World

Using pm2 to start the app

$ pm2 start hello.js --name hello-app

--name hello-app is optional and needed when you want to give a custom name to your app. You should see the following output:

[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
[PM2] Starting hello.js in fork_mode (1 instance)
[PM2] Done.
┌───────────┬────┬──────┬───────┬────────┬─────────┬────────┬────────────┬──────────┐
│ App name  │ id │ mode │ pid   │ status │ restart │ uptime │ memory     │ watching │
├───────────┼────┼──────┼───────┼────────┼─────────┼────────┼────────────┼──────────┤
│ hello-app │ 0  │ fork │ 11077 │ online │ 0       │ 0s     │ 6.680 MB   │ disabled │
└───────────┴────┴──────┴───────┴────────┴─────────┴────────┴────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

pm2 has spawned a daemon as current user. Run the following command to see its process id, etc.

$ ps aux | grep PM2
ubuntu   11071  0.2  2.5 706528 25800 ?        Ssl  16:59   0:00 PM2 v0.14.3: God Daemon
$

Note that pm2 contains info about pm2 daemon and other process it is monitoring in ~/.pm2 (which can be change using PM2_HOME environment variable).

Making pm2 run on boot/reboot

To make pm2 run on boot run this:

$ sudo pm2 startup -u ubuntu
[PM2] Generating system init script in /etc/init.d/pm2-init.sh
[PM2] Making script booting at startup...
[PM2] -linux- Using the command:
      su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
...

Note the presence of user ubuntu in /etc/init.d/pm2-init.sh (you can use some other user also).
Now save the current state (assuming hello.js is still running with name hello-app)

$ pm2 save
[PM2] Dumping processes
$ cat ~/.pm2/dump.pm2

The processes are dumped in file ~/.pm2/dump.pm2. Now when you reboot the machine, the PM2 daemon with start and it will then start all processes managed by it. Note that pm2 resurrect uses data saved in ~/.pm2/dump.pm2 to start various processes. In case pm2 dies for some reason, you can run pm2 resurrect manually also.

Deleting an app

To delete a running app managed by pm2 run this:

pm2 delete hello-app

You can also look at more documentation about pm2 various commands at pm2 Advanced README on github.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments