Node.js Environment Setup and Management on Ubuntu with PM2
💡 What You Will Learn in This Guide
In this guide, you will learn how to make your Node.js applications secure, scalable and uninterrupted on Ubuntu virtual servers (example: GenixNode Virtual Machine). With PM2's Cluster Mode, you will increase performance by using all CPU cores, perform zero-interruption updates with the pm2 reload command, and optimize logging and security configurations.
⚙️ Requirements
- Ubuntu 22.04+ virtual server
- Non-root, sudo authorized user
- Node.js and npm (preferably installed with nvm)
- Application directory (example:
/var/www/genixnode-node-app) - Firewall (UFW)
🔧 Step 1: PM2 Setup
Install PM2 globally:
npm install pm2 -g
Verify the installation:
pm2 --version
If it shows the PM2 version, the installation has been completed successfully.
🌐 Step 2: Creating a Node.js Application for Testing
Create a test application directory:
mkdir tr-pm2-app && cd tr-pm2-app
npm init -y
npm install express
Create the file app.js:
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => res.send('PM2 ile Merhaba!'));
process.on('SIGINT', () => {
console.log('SIGINT alındı. Sunucu kapatılıyor.');
process.exit(0);
});
app.listen(PORT, () => console.log(`Sunucu ${PORT} portunda dinlemede`));
Start with PM2:
pm2 start app.js
Check the status:
pm2 list
If the application appears as “online”, it is running successfully.
⚙️ Step 3: Basic PM2 Commands
| Command | Description |
|---|---|
pm2 list | Lists all applications |
pm2 stop <isim> | Stops the application |
pm2 restart <isim> | Restarts |
pm2 reload <isim> | Seamless reboot |
pm2 logs <isim> | Real-time log tracking |
pm2 monit | CPU and RAM monitoring panel |
pm2 delete <isim> | Removes process from list |
The
pm2 reloadcommand provides zero-downtime (uninterrupted) reloading in cluster mode.
💪 Step 4: Cluster Mode
If your server has multiple CPU cores, PM2 can use them automatically:
pm2 start app.js -i max
-i maxload balances using all cores.
After app update:
pm2 reload app
PM2 refreshes the code without traffic interruption by restarting each thread in turn.
🔁 Step 5: Autorun When Server Restarts
To start the application automatically when the server is restarted:
pm2 startup
Run the sudo env PATH=... line in the command output. Then save the process:
pm2 save
After this process, PM2 will automatically start the applications at system startup.
⚙️ Step 6: Configuration with ecosystem.config.js
To manage application configuration in a central file:
pm2 ecosystem
Edit the file:
module.exports = {
apps : [{
name : "genixnode-node-app",
script : "./app.js",
instances: "max",
exec_mode: "cluster",
watch: false,
env_production: {
NODE_ENV: "production",
PORT: 3000
}
}]
}
Run:
pm2 start ecosystem.config.js --env production
pm2 save
watch: falseprevents unnecessary reboots in the production environment.
🧱 Step 7: Log Management
Log files grow over time; install loop module to save disk space:
pm2 install pm2-logrotate
This module automatically rotates logs and deletes old files.
🔒 Step 8: Safety and Production Recommendations
Work with a Non-Root User: Create an application-specific user instead of the root account:
adduser nodeapp
UFW Firewall: Open only required ports:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
Use Reverse Proxy (Nginx):
- Manage HTTPS traffic
- Do not open Node.js application directly to the internet
- Serve static content quickly
Secret Data with .env File: Store database passwords and API keys in file .env and add .gitignore.
❓ Frequently Asked Questions (FAQ)
1. Why is PM2 preferred?
It keeps Node.js applications online at all times, automatically restarts them after errors, and provides cluster mode support.
2. Why doesn't the application start when the server starts?
Make sure you run the pm2 startup and pm2 save commands completely.
3. What is the difference between PM2 and Docker?
PM2 manages the processes, Docker isolates the environment. Best practice: Use PM2 (pm2-runtime) in Docker.
4. Logs are filling up the disk, what should I do?
Enable log looping with the pm2 install pm2-logrotate module.
5. How does cluster mode affect performance?
Increases CPU efficiency to 100% by using all cores on the server.
🏁 Result
Congratulations 🎉 Now your Node.js application is fully ready for production environment with PM2! You've built a solid structure with zero-downtime reloads, log cycling, autostart, and security measures.
💡 You can now publish your app on GenixNode Platform in minutes.

