systemd service file example: run your app in the background
Run your app in the background and start it automatically at boot.
A systemd service file tells Linux how to start your app, which user to run it as, and when to restart it. Enter your details below, save the file, and enable it with systemctl.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
[Unit]
Description=My app
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node /var/www/myapp/server.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.targetHow this setup works
Step-by-step setup
- Create the file: sudo nano /etc/systemd/system/myapp.service and paste the config.
- Reload systemd: sudo systemctl daemon-reload.
- Start it now and at every boot: sudo systemctl enable --now myapp.
- Check it: systemctl status myapp.
- Watch the logs: journalctl -u myapp -f.
What the important lines mean
- ExecStart runs /usr/bin/node /var/www/myapp/server.js. systemd needs the full path to the program.
- Restart=on-failure decides when the app is started again after it stops.
- WantedBy=multi-user.target is what makes it start at boot.
Common errors and how to fix them
- status=203/EXEC
- The program in ExecStart was not found or is not executable. Check the path: /usr/bin/node /var/www/myapp/server.js.
- status=217/USER
- The user www-data does not exist on this server.
- Unit myapp.service not found
- The file must end in .service and sit in /etc/systemd/system. Then run sudo systemctl daemon-reload.
Frequently asked questions
- Where do I save a systemd service file?
- In /etc/systemd/system/ with a name ending in .service, then run sudo systemctl daemon-reload.
- How do I see the logs of a systemd service?
- Run journalctl -u your-service -f to follow the log live.
- What does status=203/EXEC mean?
- systemd could not run the program in ExecStart. The path is wrong or the file is not executable. Use the full path and check permissions.
Free tools that help
- Cron expression explainerPaste a cron expression to see what it means in plain English and when it will run next. Checks ranges, steps, names and @daily-style shortcuts.
- chmod calculatorTick read, write and execute for owner, group and others to get the chmod number, symbolic form and command. Or type 755 and see what it means.
Fix a problem with this setup
- Port already in useFind which program holds a port on Windows, macOS or Linux, then stop it or use another port.
- Refused to connectWork through this checklist in order. Most cases are a server that is not running or the wrong port.
More server configs
- Apache virtual hostHost a website from a folder with Apache, with or without .htaccess.
- logrotate configKeep log files from filling your disk: rotate, compress and delete old logs on a schedule.
- fail2ban jail.localBan addresses that keep failing to log in. Choose ban time, retries and which services to protect.
- PM2 ecosystem.config.jsRun a Node.js app with PM2: restarts on crash, memory limit, cluster mode and start at boot.
- WireGuard server config (wg0.conf)Set up a WireGuard VPN server on Linux with one peer, NAT and the right commands.
- WireGuard client configCreate the client file that connects to your WireGuard server, with DNS, allowed IPs and keepalive.