From 38e46a3c5064601f71941b5771bb172233adbd1a Mon Sep 17 00:00:00 2001 From: Stefan Kempinger Date: Wed, 5 Nov 2025 16:59:37 +0100 Subject: [PATCH] Add systemd service and switch app to port 80 Add c2.service unit to run the FastAPI app as workshop-c2. The unit invokes uv via /usr/bin/env, sets PATH and PYTHONUNBUFFERED, enables automatic restarts with a short backoff, and logs to the journal using SyslogIdentifier. Also update main.py's runtime invocation --- c2.service | 28 ++++++++++++++++++++++++++++ main.py | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 c2.service diff --git a/c2.service b/c2.service new file mode 100644 index 0000000..7aace1b --- /dev/null +++ b/c2.service @@ -0,0 +1,28 @@ +[Unit] +Description=Workshop C2 FastAPI service +After=network.target + +[Service] +Type=simple +WorkingDirectory=/home/kemp/ins/teaching/workshop-c-and-c-server +# Run the requested command. This uses /usr/bin/env to locate `uv` in PATH. +ExecStart=/usr/bin/env uv run main.py +# Ensure common system paths are available when using /usr/bin/env +Environment=PATH=/usr/local/bin:/usr/bin:/bin +Environment=PYTHONUNBUFFERED=1 + +# Automatically restart the process if it exits/crashes +Restart=always +RestartSec=5 + +# Log to the journal/syslog with a clear identifier +StandardOutput=journal +StandardError=journal +SyslogIdentifier=workshop-c2 + +# (Optional) Uncomment and set a non-root user if desired +#User=www-data +#Group=www-data + +[Install] +WantedBy=multi-user.target diff --git a/main.py b/main.py index 150f985..22e3e84 100644 --- a/main.py +++ b/main.py @@ -108,4 +108,4 @@ async def root(): if __name__ == "__main__": # Run with: python main.py - uvicorn.run("main:app", host="0.0.0.0", port=8000, log_level="info") + uvicorn.run("main:app", host="0.0.0.0", port=80, log_level="info")