AdminsForum

CloudPanel Node.js & Python Guides

Guide Node.js App on CloudPanel Shows 502 or Keeps Restarting: PM2 Fixes

Started by JAY 0 replies 91 views
1JAY
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#1
Node.js app on CloudPanel shows 502 or keeps restarting: PM2 fixes

Your Node.js site in CloudPanel shows 502 Bad Gateway, or it works for a while and then dies, or it's gone after every server reboot. For a Node.js site, CloudPanel's Nginx simply forwards visitors to your app on a local port. A 502 means Nginx knocked on that port and nobody answered. This guide shows how to find out why. Setting up a new app? See How to host a Node.js app on CloudPanel with PM2.

Step 1: Compare the ports
When you created the Node.js site, you entered an App Port (for example 3000). Nginx sends traffic to
Code:
127.0.0.1:3000
. Your app must listen on exactly that port. Check what's listening:
Code:
ss -tlnp | grep node
  • Nothing listed: the app isn't running. Go to Step 2.
  • Listening on another port (say 8080): change your app's port, or change the site's App Port in CloudPanel to match.
  • Listening on
    Code:
    ::1
    only, or on a specific external IP: make the app listen on
    Code:
    127.0.0.1
    (or
    Code:
    0.0.0.0
    ). Many frameworks read
    Code:
    PORT
    and
    Code:
    HOST
    environment variables.

Step 2: Is PM2 running the app?
Log in as the site user (PM2 is per user, so root's PM2 list is a different one):
Code:
pm2 list
  • online: running. If you still get 502, recheck the port (Step 1).
  • errored or a high restart count (↺): the app crashes on start. Go to Step 3.
  • Empty list: the app was never started as this user, or PM2 lost it after a reboot (Step 4).

Step 3: Read the app's error log
Code:
pm2 logs --lines 50
The usual crash reasons:
  • Cannot find module '...': dependencies aren't installed. Run
    Code:
    npm ci
    (or
    Code:
    npm install
    ) in the app folder as the site user.
  • EADDRINUSE: the port is already taken, often by an old copy of the app.
    Code:
    pm2 delete all
    (as the site user), then start it once.
  • Missing environment variables or
    Code:
    .env
    file: copy them over from your old server or your project's example file.
  • Wrong Node version: check
    Code:
    node -v
    . Choose the Node.js version your app needs in the site's settings.
  • Build not run: Next.js, Nuxt and similar apps need
    Code:
    npm run build
    before
    Code:
    npm start
    .
After fixing, restart:
Code:
pm2 restart all

Step 4: App gone after a reboot
PM2 doesn't remember your apps across reboots unless you tell it to. As the site user, after the app runs correctly:
Code:
pm2 save pm2 startup
Code:
pm2 startup
prints a command that starts with
Code:
sudo env PATH=...
. Copy that exact line and run it as root. Then reboot once to test.

Step 5: App dies under load or every few hours
Often the server runs out of memory and Linux kills the biggest process. Check:
Code:
free -h dmesg -T | grep -i 'killed process' | tail
If you see your app there, add swap or more RAM, or limit the app's memory so PM2 restarts it cleanly before the system kills it:
Code:
pm2 start npm --name myapp --max-memory-restart 400M -- start
Also check Fix 502 Bad Gateway in CloudPanel for general 502 causes.

Quick checklist
  1. Code:
    pm2 list
    (as the site user) shows the app online.
  2. Code:
    ss -tlnp | grep node
    shows it on the App Port, on 127.0.0.1 or 0.0.0.0.
  3. Code:
    curl -I http://127.0.0.1:3000
    on the server returns a response.
  4. Code:
    pm2 save
    and the startup command are done.
If all four are true, the site works.

Sources: PM2: quick start · PM2: startup script · CloudPanel: site:add:nodejs


More CloudPanel troubleshooting
Applies to CloudPanel v2 on Ubuntu and Debian, October 2026. Replace 3000 with your site's App Port.
Reply
Possibly Related Threads…matched by title
Replies:0Views:98
Replies:0Views:89

Users browsing this thread: 1 Guest(s)
Forum Jump: