Running a NodeJS app in production with PM2

In this article we will see how to bring a NodeJS application to production using the PM2 process manager, to keep the service always running, regardless of server crashes or reboots.

This article is dedicated to solving a need that most people will have when putting an application built with NodeJS into production. Due to the style of server-side programs that we develop in NodeJS, your application will surely cost a process, or several, that have to be executed continuously. Usual cases could be a static file server or web service (or similar).

During the development stage you were executing “node index”, or something like that, to start your application, but this mechanism has several problems. One of them is that in the event of an error in your application, the execution will simply stop, leaving the server down. Also, in cases of rebooting the server, it would be necessary to restart the processes manually.

The solution is to install a process manager, or run your application as a service. In this article we are going to explain how to use a popular NodeJS Process Manager that is really complete. This is PM2, a free library that I have been using in a couple of projects with great success, capable of supporting huge amounts of traffic with a really low consumption of resources and with tools that allow remote monitoring of applications.

You can start by taking a look at the

Install PM2

If you want to have your process manager on the server, you will have to install it like any other Node.

npm install -g pm2

Once installed, you will enjoy all the functionalities of this process manager, such as the possibility of stopping and starting processes, monitoring them in real time, managing application logs, etc.

Start and stop processes

The first thing you should learn is to start and stop processes, or start them again when necessary. Obviously, instead of asking Node to execute this or that file, we will ask PM2 directly.

pm2 start index.js

Note: It seems obvious, but in the above command, instead of index.js you will have to start the main file of your Node.js application. Sometimes it’s main.js or app.js, it depends on your project.

See also  FIELDSET and LEGEND tags of forms

With this you will be able to start a process, ensuring that your server stays on. It is common that you want to assign a name to the process, so that you can refer to it later in other types of commands.

pm2 start index.js –name “my-api”

Note: If you did not explicitly give the process a name, doing “pm2 start” with the “–name” option will still give it a name, generated from the name of the file being run, without the extension. “.js”.

To stop the process, the stop command will be used, indicating the name of the process you want to stop.

pm2 stop my-api

Or to restart it, the restart command.

pm2 restart my-api

Process maintenance

As you have it now, because PM2 controls these listed processes, they will start again in case of an error, staying on for as long as the machine stays on. That is, in the hypothetical case that one of them is terminated for any reason, such as a bug in the program that causes the process to terminate, PM2 will start it again automatically.

To find information about the processes controlled by PM2 you can list them with the list command:

pm2 list

In the “restart” column we can find a counter with the number of times the process has been restarted. If that number grows continuously it means that PM2 is having to re-start the process and it will be an indicator that something is not working right.

The list of processes gives you interesting information, but to know what is happening internally it will generally be more useful to take a look at the logs.

Note: The logs not only notify you of what is happening, such as errors or restarts of the processes, it is also where all the “console.log()” made from the code of the programs appear, so they are also useful for debug a code

Log management in PM2 is quite configurable. Without going into the topic of configuring the log system, file rotation and stuff, it is interesting to mention that there is a command that allows you to see all the logs that are being produced in your processes in real time.

See also  Cursors in SQL

pm2log

This command will show you the last logs and it will stay started, showing new messages that the processes send as console output. In the event of a restart of a process started with PM2, you will be able to observe in real time how the process manager is in charge of restarting it. In the following image you have a piece of PM2 real-time log monitoring output when a process restart occurs:

As you have been able to verify, your processes restart automatically, but what would happen if the machine shuts down? We still need to configure one last step to allow processes to start on machine reboot as well.

Startup script generation

To finish our basic PM2 setup we need to set up the server startup script.

With your processes running, started by PM2, you can now automatically generate the corresponding script, without having to worry about programming, since PM2 will generate it for you. To do this we have the following command:

pm2 startup

That command will detect the platform you are running on, if it is Linux, Mac, Windows and it will generate the boot script. However, automatic platform detection doesn’t always work, or at least doesn’t always recognize your server’s Linux distribution, so it’s sometimes necessary to explicitly tell our platform:

pm2 startup centos

There are several.

In some cases I have had to do an additional command to save the process list afterwards, I guess this is when you need to change the process list after having generated the startup script:

pm2 save

Configuration of your application processes

The next step to take advantage of PM2 is to configure a file with the information of the processes that the system has to manage. In this file we can specify a lot of useful information for the control of the processes, which will be taken as values ​​when they start, or before restarts.

The file format can be done in JSON, YAML or Javascript code and you can find a lot of information in the documentation of the .

The good thing about this file is that it allows you to set many types of configurations that would not be possible through the command line. It also allows you to specify a set of processes and start them all with a single command.

See also  Prevent a textarea from being resized

The file can have any name, for example “process.json”. For example this could be a possible content for the configuration in JSON format:

{apps:}

We are specifying two processes and a small sample of possible configurations. If you want to start these processes in one go, run the command:

pm2 start process.json

In a similar way, you can restart the processes with “restart” or stop them with “stop. As we are relying on the configuration file, all the ones that have been indicated are started and stopped, with the configured options.

Now we will see something basic, but that you will need in most of your projects, such as the definition of values ​​for the .

If for example you want to define values ​​to environment variables you could use a process.json file like this:

{apps:}

In this file you are indicating that the environment variable “NODE_ENV” will have the value “development”. But we are also indicating that the same variable in the production environment has the value “production”.

If you start the processes without indicating anything:

pm2 start process.json

The environment variable “NODE_ENV” will have the value “development”. In the case that you want to specify a different environment, you will indicate it this way:

pm2 restart process.json –env production

In this case the variable “NODE_ENV” will have the value “production”.

conclusion

With what we have discussed you will have enough to manage the processes in applications made with Node, but behind PM2 there is much more.

Later we could talk about many other functionalities that we have not touched on and that it will surely be useful for you to know, such as log management, monitoring tools, the process of observing files for automatic restart when the application code changes, the utilities from deploy, etc.

Loading Facebook Comments ...
Loading Disqus Comments ...