Install Node.js (the right way)
Skip the Ubuntu repo; it ships ancient versions. Use NodeSource or nvm.
With NodeSource (system-wide)
curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
apt install -y nodejsWith nvm (per-user, recommended for dev)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc
nvm install --ltsBuild dependencies
Native modules (bcrypt, sharp, canvas) need:
apt install -y build-essential python3Run in production
Don't run node app.js directly. Use a process manager.
Option A; pm2 (zero-config, restart-on-crash)
npm i -g pm2
pm2 start npm --name web -- start
pm2 save
pm2 startup systemd -u $USER --hp $HOMEThe last command prints a sudo command; copy and run it. pm2 now restarts your app on every reboot.
Option B; plain systemd
/etc/systemd/system/myapp.service:
[Unit]
Description=My App
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/srv/myapp
ExecStart=/usr/bin/node server.js
Restart=on-failure
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.targetsystemctl daemon-reload
systemctl enable --now myapp
journalctl -u myapp -fReverse proxy
Always front Node.js with nginx (see our nginx guide). Node.js doesn't handle TLS termination or static assets as efficiently as nginx does.