Using pm2 can help keep our node apps alive when things go wrong. Our first step in getting that set up is to SSH into our production server. Once we're logged into our server, we want to go ahead and gain root access. Then we can install the pm2 module using the npm installer.
Once the installation has completed, we need to configure a few things on pm2. To do so, we're going to issue the pm2 command followed by two different arguments.
The first argument is the startup argument, and that just tells pm2 to configure itself for startup and generate the necessary scripts so that it can launch when the system boots. The second argument is centos, or centOS, which tells pm2 the type of Linux distribution we're running on so that it can configure the init.d scripts correctly.
Once that's complete, we get a little bit of output here. The first one tells us that it generated the init scripts and placed them in the /etc/init.d folder. It also set the script so that it will start at bootup.
By default, pm2 configures itself to run as root, and that's perfectly OK and it'll work fine, but it's not considered to be best practice in the Linux community. The general rule to remember is that you never want to run anything on your production servers as root unless it's absolutely necessary. In our case it's not, so we're going to generate a user account that pm2 can use to run.
We'll do that issuing a useradd command, followed by a -s to tell it we want to use the bash shell. Then a -m so that it knows to manage the home directory, which should be placed in /home/node. Finally the name of our user account is going to be node. Now that that's created, we need to modify the init.d scripts that were created.
As we look in the init.d script here, we see two different variables that are of interest to us. The first is the user variable, which specifies the username the script should run as. The second is an exported variable called PM2_HOME which is going to be the folder that pm2 looks in to find the scripts for the applications that it's responsible for managing.
The first thing we'll do is change our username, and we're going to change it to node, which is the name of the user account we created. Then we want to change the PM2_HOME variable to be the home directory of our user account.
Next we want to actually switch to the user account. Inside of our home folder, we just want to create a simple server app. If we take a look at the code here, we can see that it's not doing anything other than just returning "Hello World" to our browser.
Now let's kill that, and let's do the same thing using our pm2 program. To start it with pm2, we'll call the pm2 command followed by the start argument, and then the name of the file that we would like pm2 to use as our application.
When we press return we get a little bit of output here. The first one says that the pm2 daemon was successfully spawned, and it's going to be monitoring our processes. The second piece of output that we want to look at here is that a process was launched containing our application code from the server.js file.
If we come back over to our browser again and we reload the page, we get "Hello World" loading just like we did previously. The difference this time is that now pm2 is monitoring our application for us, so if the application stops responding or crashes, which we can actually simulate by issuing a kill -9 followed by our pid. We've killed the process. But if we reload our page, we see we still get "Hello World."
If we finally issue the pm2 list command, we'll get that same table, console table. But this time if we look at the pid, we can see that there's a new pid, indicating that pm2 was able to detect that our application crashed and it started a new process so that the application remained available to our end users.