How to host a Node.js app on a VPS with PM2 and Nginx reverse proxy
To run a Node.js application (Express, Next.js, NestJS and others) in production, you need three things: Node.js itself, a process manager that keeps the app running and restarts it after a crash or reboot, and a web server in front of it for the domain and SSL. This guide shows how to host a Node.js app on a VPS with PM2 and an Nginx reverse proxy on AlmaLinux.
Step 1: Install Node.js
AlmaLinux offers several Node.js versions as module streams. List them and enable an LTS version that your app supports (20 in this example):
sudo dnf module list nodejs
sudo dnf module reset -y nodejs
sudo dnf module enable -y nodejs:20
sudo dnf install -y nodejs
node -v && npm -v
Step 2: Create a user and upload the app
Do not run your app as root. Create a separate user, put the code in its home folder and install the dependencies:
sudo useradd -m -s /bin/bash nodeapp
sudo su - nodeapp
mkdir -p ~/app && cd ~/app
# upload or git clone your project here, then:
npm ci --omit=dev
Your app should listen on a port from the PORT environment variable (for example 3000) and on 127.0.0.1 or all interfaces. Keep secrets such as API keys in an .env file or environment variables, not in the code.
Step 3: Run the app with PM2
sudo npm install -g pm2
# as the app user:
cd ~/app
PORT=3000 pm2 start server.js --name myapp
pm2 status
pm2 logs myapp
PM2 restarts the app automatically if it crashes. Use pm2 restart myapp or pm2 stop myapp to control it.
Step 4: Start the app automatically after a reboot
pm2 save
pm2 startup systemd
# PM2 prints a "sudo env PATH=... pm2 startup systemd -u nodeapp --hp /home/nodeapp" line:
# copy it and run it as root
Step 5: Put Nginx in front as a reverse proxy
- Install Nginx, allow it to connect to the app (SELinux), start it and open the firewall:
sudo dnf install -y nginx
sudo setsebool -P httpd_can_network_connect 1
sudo nginx -t && sudo systemctl enable --now nginx
sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload
- Create
/etc/nginx/conf.d/app.example.com.conf. TheUpgradeheaders are needed if your app uses WebSockets:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
- Point
app.example.comto the server IP with an A record, reload Nginx withsudo systemctl reload nginx, then add HTTPS as explained in How to install a free Let's Encrypt SSL with Certbot on a VPS.
Deploying updates
cd ~/app
git pull # or upload the new files
npm ci --omit=dev
pm2 reload myapp
pm2 reload restarts the app with minimal downtime.
Common problems
- 502 Bad Gateway: the app is not running or listens on a different port. Check
pm2 status,pm2 logsand the port inproxy_pass. - 502 with "Permission denied" in
/var/log/nginx/error.log: SELinux blocks Nginx from connecting. Runsudo setsebool -P httpd_can_network_connect 1. - The app stops after reboot: you did not run the command printed by
pm2 startup, or you forgotpm2 save. - "EADDRINUSE": another process uses the port. Find it with
sudo ss -ltnp | grep 3000. - The app crashes with "JavaScript heap out of memory": the VPS is short of RAM. Add swap (How to create swap space on Linux) or upgrade the plan.
Frequently asked questions
Do I have to use PM2?
No. A systemd service or Docker also work. PM2 is popular because it is simple and includes logs, restarts and cluster mode.
Can I host several Node.js apps on one VPS?
Yes. Run each app on its own port with its own PM2 name, and add a separate Nginx server block for each domain.
Should I open port 3000 in the firewall?
No. Only Nginx needs to reach it locally. Keep ports 80 and 443 open and leave the app port closed.