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.
PM2 offers several features:
Before installing PM2, update your system packages:
sudo apt update && sudo apt upgrade -y
PM2 requires Node.js. Install the latest version of Node.js and npm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash source ~/.bashrc nvm install node
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs
node -v npm -v
Use npm to install PM2 globally:
sudo npm install -g pm2
Verify the installation:
pm2 --version
mkdir my-app && cd my-app nano app.js
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/'); });
pm2 start app.js
pm2 list
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
pm2 stop app_name_or_id
pm2 restart app_name_or_id
pm2 delete app_name_or_id
pm2 logs app_name_or_id
PM2 ensures your applications run seamlessly, but a robust server is essential for optimal performance. Choose a Hostomy Cloud Server for:
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!