
PM2 is a popular process manager for Node.js applications. It allows you to run applications in the background, monitor their performance, and keep them running continuously, even after a server reboot. Whether you’re deploying a single application or managing multiple, PM2 simplifies the process and ensures reliability.
In this guide, we’ll walk you through installing PM2 on Ubuntu and getting started with managing your Node.js applications. Looking for a server to host your applications? Start with a Hostomy Cloud Server.
Why Use PM2?
PM2 offers several features:
- Process management: Start, stop, and restart applications with ease.
- Auto-restart: Automatically restart applications after crashes.
- Cluster mode: Scale your application across CPU cores.
- Monitoring tools: Track resource usage and logs.
Step 1: Update Your System
Before installing PM2, update your system packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install Node.js and npm
PM2 requires Node.js. Install the latest version of Node.js and npm:
- Install Node.js using NVM (recommended):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash source ~/.bashrc nvm install node
- Or install directly from the NodeSource repository:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs
- Verify the installation:
node -v npm -v
Step 3: Install PM2
Use npm to install PM2 globally:
sudo npm install -g pm2
Verify the installation:
pm2 --version
Step 4: Start a Node.js Application with PM2
- Create a simple Node.js application.
mkdir my-app && cd my-app nano app.js
- Add the following code to
app.js:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World!\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
- Start the application using PM2:
pm2 start app.js
- Check the application status:
pm2 list
Step 5: Enable PM2 to Start on Boot
To ensure your applications restart after a server reboot, use PM2's startup script:
pm2 startup
Follow the command displayed on the terminal to enable PM2 at boot. Then, save the current process list:
pm2 save
Step 6: Manage Your Applications with PM2
Stop an Application
pm2 stop app_name_or_id
Restart an Application
pm2 restart app_name_or_id
Delete an Application
pm2 delete app_name_or_id
View Logs
pm2 logs app_name_or_id
Why Choose Hostomy for Your Server Needs?
PM2 ensures your applications run seamlessly, but a robust server is essential for optimal performance. Choose a Hostomy Cloud Server for:
- Reliable uptime.
- Scalable plans to match your application needs.
- Affordable pricing!
Conclusion
PM2 is a powerful tool for managing Node.js applications, ensuring they stay online and operate efficiently. By following this guide, you can easily install and use PM2 on your Ubuntu machine. Pair PM2 with a Hostomy Cloud Server for a reliable hosting solution!
