Install and use the PM2 NodeJS process manager to deploy your apps in production on Ubuntu 16

Try it in our public cloud & Get $5 Credit
CLAIM NOW

NodeJS applications are used in backends all over the web to perform various tasks. But unlike most web software, NodeJS scripts run in the foreground, rather than in the background as a daemon process, which inconveniently requires an open terminal window using a program such as screen or tmux to run on the server.

However, it is possible to convert a NodeJS application to run as a daemon process. PM2 is a production process manager for NodeJS that can be used for this purpose.

Getting started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 16.
• All commands should be run as the root user
• Do not run NodeJS applications as root in production. To increase security, create a new user specifically for running NodeJS applications.
• NodeJS/NPM must be installed.
• You should have a pre-existing NodeJS application to daemonize.

Tutorial

You will first need to install PM2 globally via NPM.

npm install -g pm2

Now you can run your NodeJS application using PM2.

cd /home/nodeapp
pm2 start nodetest.js

Here is a sample of what the expected output should look like:

root@nodejs:/home/nodeapp# pm2 start nodetest.js
[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
[PM2] Starting nodetest.js in fork_mode (1 instance)
[PM2] Done.
???????????????????????????????????????????????????????????????????????????????? ?????
? App name ? id ? mode ? pid ? status ? restart ? uptime ? memory ? watch ing ?
???????????????????????????????????????????????????????????????????????????????? ?????
? nodetest ? 0 ? fork ? 26935 ? online ? 0 ? 0s ? 21.680 MB ? disab led ?
???????????????????????????????????????????????????????????????????????????????? ?????
Use `pm2 show ` to get more details about an app

PM2 is now keeping track of your application. If it crashes, PM2 will restart it and keep a log of any errors.

PM2 has another feature whereby when the server reboots, PM2 can make sure to start up any applications it manages.

First, let’s configure the PM2 utility to boot up automatically with your system. Generate and install the PM2 init scripts.

pm2 startup ubuntu

root@nodejs:/home/nodeapp# pm2 startup ubuntu
[PM2] Generating system init script in /etc/init.d/pm2-init.sh
[PM2] Making script booting at startup...
[PM2] -enabled- Using the command:
su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
[PM2] Done.Make the PM2 utility boot automatically with your system

Take note of the command that this outputs. In order for the PM2 utility to start automatically, you will need to run this command as root.

su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"

Afterwards, verify the status of PM2, and make sure that it has been added to the init portion of your operating system.

systemctl status pm2

This is the output that you should see.

? pm2.service - PM2 next gen process manager for Node.js
Loaded: loaded (/etc/systemd/system/pm2.service; disabled; vendor preset: enabled)
Active: inactive (dead)

Enable PM2 to boot automatically.

systemctl enable pm2

If you see the following output, then it should be the case that the symlink has been created and that PM2 will load upon system boot.

Created symlink from /etc/systemd/system/multi-user.target.wants/pm2.service to /etc/systemd/system/pm2.service.

Conclusion

Now you will be able to use PM2 to easily manage and automatically restart your NodeJS applications. If this guide was helpful to you, kindly share it with others who may also be interested.

Quick reference

Below are a few useful commands that you can use with PM2 to manage the status of an application. For the purpose of example, we are using an application named nodetest.

List applications

pm2 list

Output:

root@nodejs:/home/nodeapp# pm2 list
?????????????????????????????????????????????????????????????????????????????????????
? App name ? id ? mode ? pid ? status ? restart ? uptime ? memory ? watching ?
?????????????????????????????????????????????????????????????????????????????????????
? nodetest ? 0 ? fork ? 26935 ? online ? 0 ? 24m ? 31.672 MB ? disabled ?
?????????????????????????????????????????????????????????????????????????????????????
Use `pm2 show ` to get more details about an app

Stop your application

pm2 info nodetest

Output:

root@nodejs:/home/nodeapp# pm2 info nodetest
Describing process with id 0 - name nodetest
????????????????????????????????????????????????????????????
? status ? online ?
? name ? nodetest ?
? restarts ? 0 ?
? uptime ? 25m ?
? script path ? /home/nodeapp/nodetest.js ?
? script args ? N/A ?
? error log path ? /root/.pm2/logs/nodetest-error-0.log ?
? out log path ? /root/.pm2/logs/nodetest-out-0.log ?
? pid path ? /root/.pm2/pids/nodetest-0.pid ?
? interpreter ? node ?
? interpreter args ? N/A ?
? script id ? 0 ?
? exec cwd ? /home/nodeapp ?
? exec mode ? fork_mode ?
? node.js version ? 6.2.2 ?
? watch & reload ? ? ?
? unstable restarts ? 0 ?
? created at ? 2016-07-01T06:27:55.763Z ?
????????????????????????????????????????????????????????????
Code metrics value
???????????????????????
? Loop delay ? 0.62ms ?
???????????????????????
Add your own code metrics: http://bit.ly/code-metrics
Use `pm2 logs nodetest (--lines 1000)` to display logs
Use `pm2 monit` to monitor CPU and Memory usage nodetest

Restart your application

pm2 restart nodetest

Output:

root@nodejs:/home/nodeapp# pm2 restart nodetest
[PM2] Applying action restartProcessId on app [nodetest](ids: 0)
[PM2] [nodetest](0) ?
?????????????????????????????????????????????????????????????????????????????????????
? App name ? id ? mode ? pid ? status ? restart ? uptime ? memory ? watching ?
?????????????????????????????????????????????????????????????????????????????????????
? nodetest ? 0 ? fork ? 27341 ? online ? 1 ? 0s ? 15.570 MB ? disabled ?
?????????????????????????????????????????????????????????????????????????????????????
Use `pm2 show ` to get more details about an app

PM2 Monitor (similar to the top program for Linux)

pm2 monit

Output:

root@nodejs:/home/nodeapp# pm2 monit
? PM2 monitoring (To go further check out https://app.keymetrics.io)
? nodetest [ ] 0 %
[0] [fork_mode] [|||||||||||||||||||||||||| ] 35.691 MB