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 — use Requires= 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 the www-data user, 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

  1. systemctl start mywebserver → systemd runs ExecStart
  2. Process stays in foreground → systemd tracks PID
  3. Process crashes → systemd detects exit, waits RestartSec, restarts
  4. 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.

Try It — Systemd Unit File Generator

Open full tool