How to Install PM2 on Debian 12

To install PM2 on Debian 12, you can follow these steps:

Step 1 : Open a terminal session.

Step 2 : Update your package index:

Step 3 : Install PM2 globally using npm:

This command will download and install PM2 globally on your Debian 12 system.

Step 4 : Verify the installation by checking the version of PM2:

You should see the version number displayed if the installation was successful.

Step 5 : (Optional) Initialize PM2 as a startup service:

To ensure that PM2 starts automatically on system boot, you can run the following command:

Step 6 : Create a simple app.js file:

Create a file named app.js in your desired directory using a text editor. For example:

Step 7 : Add the following code to the app.js file:

In this code, we're creating an HTTP server that listens on port 3000 and responds with a "Welcome to PM2!" message when a request is made. The server starts and logs a message to the console indicating that it's running.

Step 8 : Save the file and exit the text editor. In Nano, you can do this by pressing Ctrl + X, then Y to save the changes, and finally Enter to confirm the file name.

Step 9 : Start the application with PM2:

Navigate to the directory where your app.js file is located. For example:

Use the following command to start the application with PM2:

PM2 will start the application and provide you with information about the process ID and other details. You can use the pm2 list command to verify that the application is running.

Step 10 : Access the application:

Open your web browser and visit http://localhost:3000/. You should see the "Welcome to PM2!" message displayed.

Step 11 : Monitor your application with PM2:

To view the status and logs of your application managed by PM2, you can use the following command:

This command will display a real-time monitoring dashboard showing the status, CPU, memory, and logs of your application.

Manage your applications with PM2:

Step 12 : List all running applications managed by PM2:

To view a list of all running applications managed by PM2, use the following command:

This command will display information about each running application, including the application name, ID, status, and other details.

Step 13 : Stop an application:

To stop a running application managed by PM2, use the following command:

Replace <app_name_or_id> with the actual name or ID of the application you want to stop. This command will gracefully stop the application.

Step 14 : Restart an application:

To restart a running application managed by PM2, use the following command:

Replace <app_name_or_id> with the actual name or ID of the application you want to restart. This command will stop and then start the application.

Step 15 : Delete an application from PM2's management:

To remove an application from PM2's management and stop it, use the following command:

Replace <app_name_or_id> with the actual name or ID of the application you want to delete. This command will stop and remove the application from PM2.

Step 16 : Save the PM2 process list:

To save the list of applications managed by PM2 and automatically restore them on system restart, use the following command:

This command will save the current PM2 process list, including the application configurations, to ensure they are automatically restored on system boot.

PM2 is now installed on your Debian 12 system. You can use it to manage your Node.js applications, such as starting, stopping, and monitoring them.