For Express project deployment, we need a daemon. Like Tomcat in Java, PM2 is a Node process management tool that simplifies the management of Node applications and makes it easy to use. This section describes the basic configuration.

1. Install the PM2

npm install pm2 -g
Copy the code

If you are on a Windows server, be careful to add the PM2 environment variable. Note that the path bar points to the address where the software startup file resides.

2. Common commands

Here are some common commands posted first:

Pm2 start app.js --name=fork --name=fork --name=fork --name=fork Pm2 list # List of all applications pm2 monit # Display the CPU and memory usage of each application pm2 show Pm2 logs pm2 stop all pm2 stop 0 pm2 restart all Pm2 delete all # Close and delete all applications pm2 delete 0 # Delete application ID 0 Pm2 scale API 10 # Pm2 reset [app-name] # resets the number of restarts. Pm2 startup # Create pm2 save # Save the list of applications. Pm2 resurrect # reload the list of saved applications Pm2 start app.js --max-memory-restart 20MCopy the code

3. Pm2 launch project

pm2 start ./bin/www
Copy the code

4. Manage memory overflow

If the process crashes due to an overflow problem, restart the process.

One way to restart is to set the memory threshold at boot time, like this:

pm2 start www --max-memory-restart 4096M
Copy the code

However, I found that this command is invalid in the actual operation. I changed it to 6G and could not prevent the restart. Once the service is called, the JS heap out of memory will be directly.

Finally, there was no choice but to use NPM instead of PM2

This requires a maximum memory configuration for the startup node in the package.json file:

"scripts": {
    "start": "node --max_old_space_size=4096 ./bin/www"
  }
Copy the code

And then just boot it up

npm start
Copy the code

The process also restarts automatically if it crashes due to memory overflow