Launching Production Node Apps With PM2

Intro

This is it, you've been working tirelessly on your Node.js app for months. You have done all you can to make the app truly great and you are ready to release it to the public. So the question is, now what? Going to production can get complicated fast. After choosing your hosting service and OS, you set your app up as a daemon, configure system monitoring, establish automatic restarting, then once you have your configuration in place you will need to replicate it for the sake of redundancy. It's a complex topic in a time when we need things to remain as simple as possible. We have businesses to run, code to optimize, features to implement, and the last thing we need is to spend all of our time struggling with how to launch our app and keep it running. This is where PM2 comes in.

PM2 is a complete production process manager developed for Node.js apps (though it will work for some other languages). In short, it allows you to launch and manage your production app in the quickest yet smartest way possible. This begs the question, how does PM2 do this? To do this, we will walk through launching a fantasy app called MadeUpify and see what PM2 has to offer. You can grab MadeUpify on Github here.

Setup and Installation

For this demo, I will be using FreeBSD but you can follow along with whichever OS you wish. First, we need to install some things before we start running PM2. So from the terminal, let's run a few commands.

pkg install node-8.1.3
pkg install npm
pkg install git

Here we are installing Node.js and NPM. Regarding Node version, I recommend using the latest version you can but it doesn’t have to be version 8.1.3. Let's continue with some terminal commands.

npm install -g yarn
npm install -g pm2

This installs yarn and pm2 globally. Yarn is not required but helpful if you choose to follow along with cloning MadeUpify. Now it is time to set up MadeUpify.

git clone https://github.com/projekt-matara/madeupify.git
cd madeupify
yarn install

This clones MadeUpify then installs the one dependency that is required via Yarn. Upon doing this, you should be ready to begin playing around with PM2. Let's dive in and see what PM2 has to offer.

Getting Familiar with PM2

To begin, it makes sense to start MadeUpify using PM2. This is easy.

pm2 start app.js

That's it. Now it will be running on your system as a daemon and will automatically restart if the app ever crashes. Upon running the command you will also see pm2 give you a list of your running processes shown here. Pay particular attention to the App name, id, and status columns. You will see that MadeUpify is running under the name app. Also, the status should say online.

Now, let's stop the app.

pm2 stop app

Here, we just called pm2 stop then passed the App Name from earlier. Upon running the command you should now see the list pm2 gives you shows the status saying stopped. The screenshot can be found here.

Now, what do we do if we want to view this process list any time?

pm2 list

Now you can view any running process that has been started and managed by pm2.

Before continuing on, let's delete the MadeUpify process altogether.

pm2 delete app

Now that we have deleted the MadeUpify process altogether, let's recreate it with some helpful options.

pm2 start app.js --name "madeupify" -i max

Now we have started MadeUpify and we have given it the name madeupify. If you view the list shown here you can see that under the App name column the app is now shown as madeupify. However, we have done more than just name the app, we have also launched it in Cluster mode which means that PM2 is now scaling your app across multiple CPUs. In this case, we called -i max which uses the maximum number of CPUs available. You can also pass a number in the place of max to explicitly declare how many CPU cores you want your app process to use.

Now that we have this app up and running, let's check out PM2's process manager.

pm2 monit

You should now see a menu that looks like this. You can view your global logs, process list, custom metrics, and metadata from here. Now, let's ctrl-c out of this menu and see how we can set up a startup script for PM2.

pm2 startup

Now, anytime your system is rebooted, PM2 will startup as well. This should work on nearly any OS, for a complete list, check here. Also, if you want to ensure your apps are starting up along with PM2 on a system reboot, it is worth entering the following command.

pm2 save

This will save your current configuration so when your system reboots, all of your apps will restart too with the exact configuration you wanted.

Conclusion

This just scratches the surface of what PM2 is capable of. For those who are launching Node.js apps or even apps in some other languages like Python, PM2 could be a valuable asset to your product launch and continued system deployment. For those who are interested, I suggest checking out the PM2 docs for more detailed information.