Skip to content

Deployment with Systemd

Emmanuel Nwanochie edited this page Jul 20, 2024 · 1 revision

Systemd Configuration

In this section we will go over setting up a systemd service to run our FastAPI application. Systemd provides robust process management, automatic startup on boot, integrated logging, and fine-grained control over our FastAPI application, all while leveraging built-in Linux capabilities without adding unnecessary complexity to our deployment process.

Steps to configure systemd

  1. For deploying the dev branch, create a new file named fastapi_dev.service in the /etc/systemd/system/ directory with the following configuration. You can use similar configurations for your main and staging branches.
  [Unit]
  Description=FastAPI development server
  After=network.target
  
  [Service]
  User=youruser
  Group=yourgroup
  WorkingDirectory=/path/to/your/code
  Environment="PATH=/path/to/your/code/virtual/env/bin"
  ExecStart=/path/to/your/code/virtual/env/bin/uvicorn main:app --host 0.0.0.0 --port 7001 --reload
  
  [Install]
  WantedBy=multi-user.target

Let's break down each section and explain its purpose:

[Unit] Section

Description: A human-readable description of the service.
After: Ensures that the service starts after the network is available. This is crucial for a web application.

[Service] Section

User and Group: Specifies the user and group under which the service will run. You should set it to your user and your group WorkingDirectory: Sets the working directory for the service which is the directory with your code. This ensures that any relative paths in your application work correctly.
Environment: Sets the PATH environment variable to include the virtual environment's bin directory. This allows the service to find and use the correct Python interpreter and installed packages.
ExecStart: The command to start the service. We're using the uvicorn command from the virtual environment to run the FastAPI application.

--host 0.0.0.0: Allows the application to accept connections from any IP address.
--port 7001: Specifies the port on which the application will run.

[Install] Section

WantedBy: Indicates that this service should be started when the system reaches multi-user mode (normal system operation).

  1. Reload the systemd manager configuration to make it aware of the new or changed service file
   sudo systemd daemon-reload
  1. Start the FastAPI service:
   sudo systemctl start fastapi_dev
  1. Enable the service to start automatically on boot:
   sudo systemctl enable fastapi_dev

This creates the necessary symlinks to start the service automatically when the system boots.

  1. Check the status of the service to ensure it is running successfully
   sudo systemctl status fastapi_dev

Important Configuration Notes

  1. By default, systemd captures stdout and stderr from your application. You can view these logs using:
   sudo journalctl -u fastapi_dev
  1. Port Configuration: Assign unique ports for the main and staging branches to prevent conflicts.

  2. Directory Paths: If your main and staging code reside in separate directories, make sure to update the following fields in the systemd service file accordingly:

    • WorkingDirectory
    • Environment
    • ExecStart

    Ensure these paths correctly point to the respective directories for each environment.

This detailed guide explains each step of the systemd configuration process, providing context and rationale for the choices made. It should help users understand not just how to set up the service, but why each part of the configuration is important.

Clone this wiki locally