When I first started managing Node.js applications on a Linux server, I struggled with keeping them running smoothly without constant manual intervention. That’s when I discovered PM2, a powerful process manager for Node.js that simplifies app management, ensures uptime, and makes scaling a breeze.
In this article, I’ll share a simple, beginner-friendly guide on how to set up PM2 on a Linux server. Whether you’re a developer or a server admin, this step-by-step tutorial will help you get PM2 up and running in no time.
First, I log in to my Linux server using SSH. Open your terminal and use the following command, replacing username
and server-ip
with your actual server details:
ssh username@server-ip
Once connected, I ensure my server is updated to avoid compatibility issues.
I always start by updating the package lists to ensure I have the latest software versions. Run these commands:
sudo apt update
sudo apt upgrade -y
This step is crucial for a smooth installation process, especially on Ubuntu or Debian-based systems.
PM2 is a Node.js-based tool, so I need to install Node.js and npm (Node Package Manager) first. I prefer using the latest LTS version for stability. Here’s how I do it:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
After installation, I verify the versions to confirm everything is set up correctly:
node -v
npm -v
With Node.js and npm ready, I install PM2 globally using npm. This allows me to use PM2 from any directory on the server. Run:
sudo npm install -g pm2
Once installed, I check the PM2 version to ensure it’s working:
pm2 -v
Now, I navigate to my Node.js application’s directory. For example, if my app is in /var/www/myapp
, I run:
cd /var/www/myapp
To start the application with PM2, I use:
pm2 start app.js
Here, app.js
is the entry point of my Node.js app. You can replace it with your main file (e.g., index.js
). PM2 will now manage this process and keep it running.
To ensure my app restarts automatically after a server reboot, I save the PM2 process list:
pm2 save
This creates a dump file that PM2 uses to restore processes on reboot.
To make PM2 start automatically when the server boots, I use the PM2 startup script:
pm2 startup
This command generates a startup script specific to my system. I follow the output instructions, which usually involve running a command like:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u username --hp /home/username
I replace username
with my actual server username. This ensures PM2 and my apps restart after a reboot.
PM2 makes it easy to monitor my apps. I use the following commands to manage processes:
View all running processes:
pm2 list
Monitor resource usage:
pm2 monit
Restart an app:
pm2 restart app_name
Stop an app:
pm2 stop app_name
View logs:
pm2 logs
These commands help me keep track of my apps and troubleshoot issues quickly.
For advanced management, I create an ecosystem file to configure multiple apps. I create a file named ecosystem.config.js
in my project directory:
module.exports = {
apps: [
{
name: 'my-app',
script: './app.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
},
},
],
};
Then, I start the app using:
pm2 start ecosystem.config.js
This setup allows me to manage multiple apps with specific configurations like environment variables or clustering.
Setting up PM2 on a Linux server has been a game-changer for me. It simplifies managing Node.js applications, ensures they stay online, and provides tools to monitor performance. By following these steps, I’ve been able to deploy and manage my apps with confidence, and I’m sure you can too. Whether you’re running a single app or multiple services, PM2 is a reliable tool that saves time and effort. Try it out, and let me know how it works for you!
Q1: What is PM2, and why should I use it?
A: PM2 is a process manager for Node.js applications. I use it to keep my apps running, restart them automatically if they crash, and monitor their performance, making server management much easier.
Q2: Can I use PM2 for non-Node.js applications?
A: Yes, PM2 can manage any script or process, but it’s primarily designed for Node.js. I’ve used it for Python scripts by specifying the interpreter, like pm2 start script.py --interpreter python3
.
Q3: How do I update PM2 to the latest version?
A: I update PM2 by running sudo npm install -g pm2@latest
. Then, I update the PM2 daemon with pm2 update
.
Q4: What if my app doesn’t start with PM2?
A: I check the logs using pm2 logs
to identify errors. Common issues include incorrect file paths, missing dependencies, or port conflicts. Fixing these usually resolves the problem.
Q5: Is PM2 free to use?
A: Yes, PM2 is open-source and free to use. There are paid features for advanced monitoring, but the core functionality I’ve covered here is completely free.
You might also like :