Pagoda deploys node projects to automatically restart after the project encounters an abnormal stop.

When we write our own node projects, we often encounter situations where the project is closed due to an error. This article will provide you with how to implement the automatic restart function after the node project encounters an abnormal stop. Get rid of the embarrassment of having to restart manually every time.

Install pm2 globally:

npm install pm2 -g

Check the global npm module path, run the following command to see where the global npm module is installed:

npm root -g

Manually configure environment variables:

Check the global installation path of `pm2` using the following command:

which pm2

If the global installation path of `pm2` is not in the system's environment variables, you need to configure it manually. Open your terminal profile (usually `~/.bashrc` or `~/.bash_profile`) and add the following lines:

export PATH=$PATH:/usr/local/bin

Or replace the path `/usr/local/bin` with the actual global installation path of `pm2`.

For example, the following is my actual global installation path:

export PATH=$PATH:/www/server/nodejs/v14.17.6/lib/node_modules/pm2/bin

Next, let’s modify the environment variables

Open the terminal configuration file (run in the server root directory):

Open the terminal profile (usually `~/.bashrc` or `~/.bash_profile`) using the following command:

vi ~/.bashrc

or

vi ~/.bash_profile

Add the path to pm2: Add the following lines at the end of the file:

export PATH=$PATH:/www/server/nodejs/v14.17.6/lib/node_modules/pm2/bin

Save and exit:

Press the `Esc` key, then type `:wq` and press Enter to save and exit the vi editor.

Reload environment variables:

Use the following command to reload the environment variables:

source ~/.bashrc

or

source ~/.bash_profile

Verify pm2 installation:

Try running the following command again to verify that `pm2` was installed successfully:

pm2 --version

If the installation is successful, you will see the version number of `pm2`.


Next, we will get to the point.

To keep the Node.js project created in the Pagoda panel automatically restarted, we need to use some tools or scripts to achieve this. A common approach is to use a process management tool like `pm2`:

Install pm2:

Use the terminal in the Pagoda panel to enter the following command to install `pm2` (you have done this before, this step can be omitted)

npm install pm2 -g

Start the application:

In the project directory, use `pm2` to start the application (replace start with restart when restarting), for example:

pm2 start npm -- start

Save the current `pm2` process list:

This will ensure that the `pm2` process is still running after the server is restarted. You can save the current `pm2` process list using the following command:

pm2 save

Generate startup script:

To have `pm2` load automatically on server restart, you can generate a startup script. The script can be generated with the following command:

pm2 startup 

Now, when the server is restarted, `pm2` will automatically load and start the Node.js application. With these steps, your Node.js application should be able to automatically restart after encountering an unexpected stop.


One last thing to note is that if the Node.js version is updated or reinstalled in the future, the global path to `npm` may change. In this case, you need to update the path to `pm2` accordingly. Make sure to replace the path `/www/server/nodejs/v14.17.6` in the above steps with the path to your actual Node.js version.

Guess you like

Origin blog.csdn.net/qq_25501981/article/details/134017353