Setting Up a Node.js Script on a VPS with Auto-Start via PM2

Running a Node.js script locally is a five-minute task. But when it comes to a production environment, questions arise: how do you make an application start automatically after a server reboot, restart on failure, and not interfere with other processes? That is exactly what PM2 is for — a process manager that has become the de facto standard in the Node.js ecosystem. In this guide, we will walk through the entire journey: from ordering a VPS to a stably running application with auto-start configured.
Step 1. Creating a Server in the NodexGo Panel
Before moving on to configuration, you need the server itself. In the NodexGo panel, click the "Create Server" button. You will see a list of available locations — each one displays the real ping from your browser, which helps you choose the nearest data center. Select a suitable plan and specify Ubuntu 24.04 as the operating system image — this is the current LTS version with long-term support, on which Node.js runs without unnecessary complications.
Set a server name, choose a billing period — options of 1, 3, 6, or 12 months are available — and confirm the order. In about a minute the server will be ready: the IP address and root password will appear in the panel and will be sent to your email. After that, connect via SSH.
ssh root@your-server-IPDeploy a Node.js application on a ready-made VPS in one minute
Create ServerStep 2. Updating the System and Installing Node.js
First, update the system packages to avoid dependency conflicts and close potential vulnerabilities. After updating, install Node.js via the official NodeSource repository — this guarantees an up-to-date version of the platform rather than an outdated package from the standard Ubuntu repositories.
apt update && apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejsAfter installation, verify that Node.js and npm are available on the system and that the versions match what is expected. This is a simple but important step — it lets you confirm that the installation completed correctly before moving forward.
node -v
npm -vStep 3. Placing the Application on the Server
Create a directory for the project and navigate into it. If you already have ready-made code, upload it via git clone or copy it using scp. For demonstration purposes, we will create a minimal HTTP server manually to verify that the entire chain works before installing PM2.
mkdir -p /var/www/myapp && cd /var/www/myappCreate the application file. The example below starts an HTTP server on port 3000 and responds with the string "Hello from VPS" to any incoming request. This is enough to verify that Node.js is working and that PM2 correctly manages the process.
cat > app.js << 'EOF'
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello from VPS');
}).listen(3000);
EOFStep 4. Installing PM2 and Starting the Application
PM2 is installed globally via npm and immediately becomes available as a system command. The process manager can monitor the state of an application, restart it on failure, maintain logs, and manage multiple processes simultaneously. Installation takes just a few seconds.
npm install -g pm2After installation, start the application through PM2, passing it a process name with the --name flag — this simplifies further management, especially when multiple applications are running on the server at the same time. PM2 will immediately display a table showing the status of running processes.
pm2 start app.js --name myappTo confirm that the application is running, use the pm2 list command — it shows all active processes, their status, memory consumption, and restart count. To view logs in real time, use pm2 logs.
pm2 list
pm2 logs myappStep 5. Configuring Auto-Start on Server Reboot
The key feature of PM2 is its integration with the operating system's init system. The pm2 startup command generates and registers a systemd unit that starts PM2 — and along with it all saved processes — automatically when the system boots. This is precisely what distinguishes a "running application" from a "reliably operating service".
pm2 startup systemdAfter running this command, PM2 will print a line to the terminal with a command that must be executed with superuser privileges — copy it and run it. Then save the current list of processes so that PM2 knows which applications to restore after a reboot.
pm2 saveNow you can verify the auto-start: reboot the server with the reboot command and, after reconnecting, run pm2 list. All saved processes should be in the online status without any manual actions on your part.
rebootStep 6. Useful PM2 Commands for Daily Work
PM2 provides a convenient set of commands for managing the lifecycle of applications. You can restart a process, stop it, remove it from the list, or perform a graceful reload with no downtime — all without having to manually search for a PID and use kill. The most commonly used commands are listed below.
pm2 restart myapp
pm2 reload myapp
pm2 stop myapp
pm2 delete myapp
pm2 monitThe pm2 monit command opens an interactive dashboard directly in the terminal: you can see CPU and memory usage for each process in real time. This is convenient for quick diagnostics without installing additional monitoring tools.
What to Do If SSH Access Is Lost
Sometimes during setup you can accidentally lock yourself out — for example, by changing firewall rules or making an error in the sshd configuration. In the NodexGo panel, a VNC web console is available for each server directly in the server card: it works independently of the SSH state and allows you to log into the system and fix the problem. The same location contains the "Change root password" and "Reinstall OS" buttons — for cases when the situation has gone too far. If your project has grown and the current plan is no longer sufficient, the "Upgrade Resources" button will move the server to a higher plan while preserving the disk and IP address.
Conclusion
The combination of Node.js and PM2 on a VPS is a reliable and proven setup for running production applications of any scale. PM2 handles monitoring, restarts on failure, and systemd integration, allowing you to focus on development rather than administration. The entire path described — from a clean server to a stably running service with auto-start — takes no more than 15 minutes. If you are looking for a reliable platform for your Node.js projects, NodexGo provides VPS with fast deployment, a convenient control panel, and everything you need for comfortable work. Rent a server and launch your application today.
Launch your Node.js application on a reliable VPS
Create Server