Installing Node.js Environment on Ubuntu VPS with PM2
Installing Node.js Environment on Ubuntu VPS with PM2
What will you learn in this guide?
This guide explains how to make Node.js applications production-ready with PM2 on an Ubuntu-based VPS.
The goal is for applications to run without crashing, restarting, and using multiple cores efficiently.
Technical Summary
This guide covers the steps of PM2 installation, Running a Node.js application, cluster mode, autostart, security and monitoring on Ubuntu VPS.
What is PM2?
PM2 is a process manager developed for Node.js applications.
It runs applications in the background and keeps them online at all times.
Highlights
- Automatic restart after crash
- Automatic operation at server startup
- Multi-core cluster support
- Log collection and monitoring
- Seamless reloading
Prerequisites
- A VPS with the latest Ubuntu version installed
- Non-root user with sudo authority
- Node.js and npm (nvm recommended)
- A simple Node.js application
- An active firewall (UFW)
1. PM2 Setup
- PM2 is installed globally via npm.
This command installs PM2 into the user directory.
npm install pm2 -g
- To verify the installation:
pm2 --version
2. Creating a Sample Node.js Application
- This step installs a simple Express application.
mkdir my-app
cd my-app
npm init -y
npm install express
- These commands create the project directory and the Express dependency.
app.js File
- This code starts a simple HTTP server.
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello from PM2!');
});
process.on('SIGINT', () => {
console.log('SIGINT alındı, uygulama kapatılıyor.');
process.exit(0);
});
app.listen(PORT, () => {
console.log(`Sunucu ${PORT} portunda çalışıyor`);
});
3. Running the Application with PM2
- This command starts the application in the background.
pm2 start app.js
- The application now works even if the terminal is closed.
4. Basic PM2 Commands
1. Below are the most commonly used PM2 commands:
2. pm2 list → Lists running applications
3. pm2 logs app → Shows live logs
4. pm2 restart app → Restarts the app
5. pm2 stop app → Stops the application
6. pm2 delete app → Deletes the app
5. Performance Increase with Cluster Mode
- A single process uses a single core. Cluster mode uses all CPU cores.
pm2 start app.js -i max
- This command starts a worker for each core.
6. Uninterrupted Update (Zero Downtime)
- Prevents service interruption when updating code.
pm2 reload app
- Renews PM2 workers one by one.
7. Autorun When Server Restarts
- This step is critical for production.
pm2 startup
- Run the sudo command given by PM2. Then save the process list:
pm2 save
8. Usage of ecosystem.config.js
- Configuration file is recommended instead of command line.
module.exports = {
apps: [{
name: "my-app",
script: "./app.js",
instances: "max",
exec_mode: "cluster",
watch: false,
env_production: {
NODE_ENV: "production",
PORT: 3000
}
}]
};
To start:
pm2 start ecosystem.config.js --env production
9. Log Management
- Logs fill the disk over time. Automatic log rotation is recommended.
pm2 install pm2-logrotate
- This module limits logs automatically.
10. Recommendations for Security
-
Do not run as root user
-
Enable UFW firewall
-
Do not open the Node.js port to the outside world
-
Use reverse proxy with Nginx
-
Manage private keys with .env
11. Common Problems
- The app keeps crashing
pm2 logs app
-
Examine the logs.
-
Post-server application does not start
-
Is pm2 startup run?
-
Check if pm2 is saved
Frequently Asked Questions (FAQ)
1. What does PM2 do? It runs Node.js applications safely in the production environment.
2. Does PM2 replace Docker? No. PM2 manages the process, Docker isolates the environment.
3. Is cluster mode necessary? Definitely recommended for multi-core servers.
4. Why reload instead of pm2 restart? Reload provides uninterrupted updates.
Result
PM2 makes Node.js applications production-ready on Ubuntu. Cluster, automatic startup and log management provide great advantages.
You can safely run your high-performance Node.js projects on the GenixNode infrastructure.

