Deploy production Node.js backends with automatic daemon restarts, cluster scaling, and Nginx SSL termination
By following this guide, you will have a production-ready Node.js application running continuously in the background using PM2 process manager. It will be securely reverse-proxied behind Nginx with automatic crash restarts and system reboot persistence.
Spin up an Ubuntu 24.04 instance with instant activation to test this tutorial:
Deploy IndSoft VPSAn Ubuntu 24.04 instance with at least 1 GB RAM, non-root user with sudo access. Recommended: IndSoft Node.js Optimized Cloud.
Add the NodeSource repository to install the latest Long Term Support (LTS) release of Node.js and npm.
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install nodejs -y
node -v && npm -vInstall PM2 globally using npm to handle process monitoring, clustering, and auto-restart on crashes.
sudo npm install pm2@latest -gNavigate to your application directory, launch your entry point with PM2, and register the systemd startup hook.
cd /var/www/my-app
npm install
pm2 start app.js --name 'backend-api'
pm2 startup systemd
pm2 saveRoute incoming web traffic on port 80/443 to your Node.js application running locally on port 3000.
sudo nano /etc/nginx/sites-available/nodejs-app
# proxy_pass http://localhost:3000;
sudo ln -s /etc/nginx/sites-available/nodejs-app /etc/nginx/sites-enabled/
sudo systemctl reload nginxRun this quick verification check to confirm your installation is responding properly:
pm2 statusQuick resolutions for frequent setup pitfalls and configuration issues.
pm2 logs backend-api to inspect error tracebacks. Use pm2 start app.js --max-memory-restart 300M to guard against memory exhaustion.
sudo ss -tulpn | grep 3000.
pm2 save after generating the systemd startup script with pm2 startup.