
Use Cluster Mode to Scale Node.js Apps Hosted in cPanel
Use Cluster Mode to Scale Node.js Apps Hosted in cPanel
What is Cluster Mode?
Cluster Mode allows Node.js applications to create multiple worker processes to handle incoming requests. This helps in maximizing CPU utilization and improving app responsiveness by running on multiple cores.
Benefits of Using Cluster Mode
- Improved Performance: Handles more concurrent requests efficiently.
- Reliability: If a worker crashes, the master process can spawn a new one automatically.
- Better Resource Utilization: Uses all available CPU cores effectively.
Prerequisites
- Node.js application deployed via cPanel on Hiverift or similar hosting.
- Access to cPanel and ability to edit Node.js app files.
Step 1: Access Your Node.js Application Files
Login to your cPanel account and open the File Manager or connect via FTP to your Node.js application directory.
Step 2: Create or Modify Your Cluster Script
Instead of running a single instance, use the built-in cluster
module in Node.js to spawn multiple workers. Here’s an example cluster setup:
const cluster = require('cluster');
const os = require('os');
const http = require('http');
if (cluster.isMaster) {
const numCPUs = os.cpus().length;
console.log(`Master process is running. Forking ${numCPUs} workers...`);
for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Starting a new worker.`);
cluster.fork();
});
} else {
// Worker processes run your app code
http.createServer((req, res) => {
res.writeHead(200);
res.end(`Handled by worker ${process.pid}\n`);
}).listen(process.env.PORT || 3000);
console.log(`Worker ${process.pid} started`);
}
Step 3: Update Your cPanel Node.js Application Entry Point
In your cPanel Node.js app setup, ensure the Application Startup File points to your cluster script (e.g., cluster.js
), instead of the usual app.js
or server.js
.
Step 4: Restart Your Node.js Application
After saving your changes, restart your Node.js app from cPanel’s Setup Node.js App interface. This will launch your cluster workers.
Step 5: Verify Your Cluster is Running
Open your application in the browser multiple times or refresh rapidly. You should see different worker PIDs handling requests (if you use the example code above).
process.pid
in your app helps monitor which worker handled each request.Troubleshooting
- If your app fails to start, check your startup script path in cPanel.
- Ensure your app listens on the port assigned by cPanel (
process.env.PORT
), not a hardcoded port. - Check error logs in cPanel’s Errors or Raw Access Logs section for clues.
Additional Recommendations
- Use process managers like
pm2
if you have SSH access for advanced clustering and monitoring. - Regularly monitor resource usage to avoid overloading your server.
- Implement health checks and graceful shutdowns for your workers to improve stability.
Need Help?
For assistance with Node.js clustering or app deployment on Hiverift, contact our support team at dev@hiverift.com.