Simple Web Server Service
Create a systemd service unit file for a simple web server process. Learn about the simple service type, basic dependencies, and how systemd manages foreground processes.
Simple Services
Detailed Explanation
Running a Web Server with systemd
The most common systemd service type is simple. When you set Type=simple, systemd considers the service started as soon as the ExecStart process is launched. The process must stay in the foreground — it should not fork or daemonize itself.
[Unit]
Description=My Web Server
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/my-webserver --port 8080
Restart=on-failure
RestartSec=5
User=www-data
Group=www-data
[Install]
WantedBy=multi-user.target
Key Directives Explained
After=network.target: Ensures the network stack is initialized before the service starts. This does not create a hard dependency — useRequires=for that.Type=simple: The default type. The process started by ExecStart is the main service process. Systemd tracks its PID and monitors it directly.Restart=on-failure: Automatically restarts the process if it exits with a non-zero exit code or is killed by a signal (excluding clean exits with code 0).RestartSec=5: Waits 5 seconds between restart attempts to prevent rapid restart loops that could overwhelm the system.User=www-data: Runs the process as thewww-datauser, following the principle of least privilege.
When to Use Simple Type
Use Type=simple when your application:
- Runs in the foreground and doesn't fork
- Is ready to accept connections immediately after starting
- Is a modern application (Go binary, Node.js, Python with Gunicorn in foreground mode)
Service Lifecycle
systemctl start mywebserver→ systemd runs ExecStart- Process stays in foreground → systemd tracks PID
- Process crashes → systemd detects exit, waits RestartSec, restarts
systemctl stop mywebserver→ systemd sends SIGTERM, waits TimeoutStopSec, then SIGKILL
Use Case
Deploying a lightweight web server (such as a Go HTTP server or a static file server) that runs in the foreground and needs automatic restart on failure with proper user isolation.