Skip to content
VDS · Websites and web servers

A Node.js application under systemd

How to run a Node application on VDS so it survives an SSH disconnect and a machine reboot: systemd unit, environment variables, logs.

Updated August 23, 2026

A manually started node index.js dies with the SSH session. To keep the application running permanently and have it come back up after a reboot, run it as a systemd service — this is a built-in system mechanism, so there is nothing to install.

Preparation

Install Node from the official repository, not from the old system package:

curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
apt install -y nodejs
node -v

Create a dedicated user — the application must not run as root:

useradd -r -m -d /srv/app -s /usr/sbin/nologin appuser
chown -R appuser:appuser /srv/app
sudo -u appuser npm ci --omit=dev --prefix /srv/app

systemd service

The /etc/systemd/system/myapp.service file:

[Unit]
Description=My Node app
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=appuser
WorkingDirectory=/srv/app
EnvironmentFile=/srv/app/.env
ExecStart=/usr/bin/node /srv/app/index.js
Restart=always
RestartSec=5
NoNewPrivileges=true
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now myapp
systemctl status myapp

Restart=always brings the process back up after a crash. EnvironmentFile is a plain KEY=value file with no quotes and no export; set its permissions to 600, since it holds passwords.

Exposing it through a proxy

The application should listen only on 127.0.0.1:3000, and a web server with a certificate serves it to the outside:

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

For more details, see reverse proxy and Let's Encrypt. If the application listens on 0.0.0.0, it is reachable from the internet bypassing the proxy and without encryption.

Logs and updates

journalctl -u myapp -f
journalctl -u myapp --since "1 hour ago" -p err
systemctl restart myapp

Output to stdout goes to the journal automatically — you don't need to set up your own log files or log rotation.

Common errors

SymptomCause
status=203/EXECWrong path to node or to the file in ExecStart — use absolute paths
The service restarts in a loopThe application crashes on startup: check journalctl -u myapp -n 50
EADDRINUSEA manually started process is still running: ss -tulpn | grep 3000
Works over SSH, but not as a serviceEnvironment variables are missing — systemd doesn't have them, set them in EnvironmentFile
The build runs out of memory on VDS-1Build locally and upload the finished build to the machine

Don't put pm2 on top of systemd. Two supervisors over one process create confusion: systemd considers the process alive while pm2 is restarting it.