dev3 ·
I start my app with npm start in an SSH session and it dies when I disconnect. I would rather not install PM2. How do I make it a proper systemd service?
0
DEV3 Developer Forum
Every post is reviewed first. Usually reviewed within 24 hours.
I start my app with npm start in an SSH session and it dies when I disconnect. I would rather not install PM2. How do I make it a proper systemd service?
Create a unit file. Systemd handles start on boot, restart on failure, logging and resource limits.
/etc/systemd/system/myapp.service:
[Unit]
Description=My Node app
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/var/www/myapp
EnvironmentFile=/var/www/myapp/.env
Environment=NODE_ENV=production
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
# Hardening (optional)
NoNewPrivileges=true
ProtectSystem=full
[Install]
WantedBy=multi-user.target
Enable and start:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
sudo systemctl status myapp
journalctl -u myapp -f
Details
node (which node). If you use nvm, the path lives in the user's home, and systemd will not load shell profiles. Install Node system-wide or point to the nvm binary path explicitly.EnvironmentFile takes KEY=value lines without export and without shell quoting tricks.Restart=on-failure restarts on crashes; always also restarts after clean exits.daemon-reload before restarting.journalctl -u myapp --since "1 hour ago".Put Nginx in front as a reverse proxy to http://127.0.0.1:3000 and keep the app bound to localhost so only Nginx is public.