Skip to main content

Systemd

Introduction

With Systemd, we can run any application as a service.

FastAPI

As an example, consider running the FastAPI framework:

  1. Create a website using the "Systemd" template.

Systemd site wizard

  1. Connect to the server via SSH as the root user.
  2. Install the required packages:

Debian/Ubuntu:

apt update && apt install python3-pip python3-venv -y

Rocky Linux/AlmaLinux:

yum install python3-pip python3-virtualenv -y
  1. Switch to your site’s system user:
su - yoursiteuser

Replace yoursiteuser with the actual username.

note

You can determine the site user in the site card, field "Site user". To change the site user password, go to "Management" → "Users" section in FASTPANEL®.

  1. Create a virtual environment and activate it:
python3 -m venv myenv
source myenv/bin/activate

Replace myenv with the desired environment name.

  1. Install the required packages for FastAPI:
pip install fastapi uvicorn
  1. Deactivate the virtual environment:
deactivate
  1. Create a file main.py in the website directory with the following content:
import os
import uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"hello": "world"}

if __name__ == "__main__":
port = int(os.getenv("SERVICE_PORT", 8000))
uvicorn.run(app, host="0.0.0.0", port=port)
  1. In the site dashboard, go to “Settings” → “Backend”.
  • Set "Launch command" to: python3 main.py
  • Save and check the website

Logs

A Systemd service can generate logs. In FASTPANEL®, they are located in the site dashboard, in the "Logs" section, under the "Backend log" tab.