1. Introduction
In this tutorial, we are going to learn about PM2, a robust, production-grade process manager for Node.js applications. PM2 empowers you to keep your site and apps available forever. It helps you manage your application's services, logs, exceptions, and even monitor the resource usage in real-time.
By the end of this tutorial, you will be able to:
- Install PM2 on your machine
- Use PM2 to manage your Node.js applications
- Use PM2 to ensure your apps are always running
Prerequisites:
- Basic understanding of Node.js and Express.js
- Node.js and NPM installed on your system
2. Step-by-Step Guide
2.1 Installing PM2
The first step is to install PM2. You can install PM2 globally on your system using NPM (Node Package Manager). Run the following command in your terminal:
npm install pm2 -g
2.2 Starting an Application with PM2
To start an application with PM2, navigate to the directory of your application and run the following command:
pm2 start app.js
Replace app.js
with the entry point of your application.
2.3 Managing Processes
You can list all processes managed by PM2 using the pm2 list
command. To stop a process, use pm2 stop <id>
where <id>
is the id of the process. Use pm2 restart <id>
to restart a process.
2.4 Keeping Applications Running
PM2 can keep your applications running even after a system reboot. You can achieve this by using the pm2 startup
command.
3. Code Examples
Let's assume we have an Express.js application. The entry point of our application is app.js
.
3.1 Starting the Application
pm2 start app.js
This will start the application under PM2's supervision.
3.2 Listing Managed Processes
pm2 list
This will list all processes managed by PM2.
3.3 Stopping a process
If the id of our process is 0, we can stop it using:
pm2 stop 0
This will stop the process with the id 0.
3.4 Restarting a Process
pm2 restart 0
This will restart the process with the id 0.
3.5 Keeping the Application Running
pm2 startup
This command will generate a command that you need to run with superuser privileges. This will make sure your application starts on system reboot.
4. Summary
In this tutorial, we learned how to use PM2 to manage our Node.js applications. We learned how to start, stop, and restart our applications using PM2. We also learned how to make sure our applications keep running even after a system reboot.
Next, you can learn more about the other features of PM2 like clustering, monitoring, etc. You can refer to the official PM2 documentation for more details.
5. Practice Exercises
5.1 Exercise 1
Create a basic Express.js application and use PM2 to start the application.
5.2 Exercise 2
Use PM2 to list all running applications, stop the application you started in Exercise 1, and then restart it.
5.3 Exercise 3
Set up your application to start on system reboot.
Solutions
5.1 Solution to Exercise 1
You can create a basic Express.js application using the Express Generator. You can then navigate to the directory of your application and use pm2 start ./bin/www
to start your application.
5.2 Solution to Exercise 2
You can use pm2 list
to list all running applications. You can stop your application using pm2 stop <id>
. You can restart your application using pm2 restart <id>
.
5.3 Solution to Exercise 3
You can use pm2 startup
to generate a command. You need to run this command with superuser privileges. This will set your application to start on system reboot.