Node.js: production management with PM2

  • Post by Nicolas Ramz
  • Jun 16, 2014
Node.js: production management with PM2

It’s real easy to come with having your site running with Node.js: simply start it with “node server.js” and here you go. But what if you wanted to take advantage of the multiple cores of your server ? What about log aggregation ? What about process management ? Well, none of these are easy to deal with.

There were already some useful tools like nodaemon or node-forever but unfortunately, these tools are either not ready for production environment, or lack process-mangement/cluster support.

This was a problem before PM2!

PM2: a very complete process manager for Node.js

Here are the main features of PM2:

  • Built-in load balancer (using the native cluster module
  • Script daemonization
  • 0s downtime reload for Node apps
  • Generate SystemV/SystemD startup scripts (Ubuntu, Centos…)
  • Pause unstable process (avoid infinite loop)
  • Restart on file change with –watch
  • Monitoring in console
  • Free server/pm2 monitoring by ping strategy

As you can see, PM2 is quite complete, and fills a huge gap when it comes to Node.js production management.

Cluster mode made easy

If you wanted to take advantage of cluster mode, you could either use the built-in cluster Node.js module, but this required so code modification, or use some Node.js fork supporting cluster module. With PM2, taking advantage of all your cores is as easy as adding a command line switch:

pm2 start myServer.js -i max

This will automatically launch the maximum process of node servers with a built-in load-balancer:

Monitoring your running processes is then as easy as calling:

pm2 monit

Screen Shot 2014-06-16 at 09.35.44

By default, pm2 won’t watch for file modifications but again, a single command line option will make your Node.js server start in watch mode.

pm2 start myServer.js --watch

Passing arguments to nodejs or your server script

Using standard unix double-dash can be used to send option parameters to your script file, for example:

# -p 8080 will be sent to your myServer.js and accessible through process.argv array
pm2 start myServer.js -- -p 8080

It’s also possible to pass arguments to nodejs itself by using the –node-args argument, for example:

# will send--debug=7001 --trace-deprecation to nodejs executable 
pm2 start myServer.js --node-args="--debug=7001 --trace-deprecation"

Small tip: listening to port 80 on BSD/Darwin (OSX/*BSD)

Most Unix OS will prevent users other than root from listening to port 80: by default, these users can only bind to port 1025 and up. If your router can route ports it’s not a problem, but if it is limited to IP redirection, like mine, it can be a problem. You could run your node server as root but you of course don’t want to do that! What you can do is to use the ipfw command line tool:

# this will redirect any incoming requests on port 80 to port 8080
ipfw add 1443 fwd 127.0.0.1,8080 tcp from any to me 80 in

This way, any request to port 80 on your machine will be redirected to port 8080: you can simply make your nodejs server listen to port 8080.

What’s next ?

PM2 is being quickly updated: right now, version 0.8.13 is the current version but version 0.9 is already coming. Among other things, this version will integrate deploy.sh to allow ease deployment of your projects.

pm2Next

Next version will also focus on improving analytics with a new side-project called Keymetrics: having logs is a good thing, but consolidating these metrics is as important. This new project will allow you to analyze, group, filter your metrics with realtime support.

If you want to contribute to PM2: PM2 on GitHub