Deploying a FastAPI Project on a Linux Server with Nginx and Systemd Service: A Simplified Guide with Uvicorn and Hot Reload
Introduction
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python based on standard Python type hints. Deploying a FastAPI project on a Linux server can be a bit tricky, but with the help of Nginx and Systemd service, you can easily serve your FastAPI application to the world. In this blog, we will walk you through the process of setting up a FastAPI project on a Linux server using Nginx as the reverse proxy and Uvicorn as the ASGI server for managing the FastAPI application. We will also cover hot reloading of your Python project and how to restart the service when changes are made.
Prerequisites
- A Linux server with Python 3.6 or higher installed.
- A working FastAPI project.
- A domain name and SSL certificate (optional but recommended).
Step 1: Install Required Packages
First, let’s install the required packages for our FastAPI project:
pip install fastapi uvicorn
Step 2: Configure Your FastAPI Application
In your FastAPI project, create a file named main.py
and set up your FastAPI application:
from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"}
Step 3: Create a Systemd Service for Your FastAPI Application
Create a new file in /etc/systemd/system/fastapi-project.service
with the following content:
[Unit] Description=FastAPI Project [Service] User=www-data Group=www-data WorkingDirectory=/path/to/your/fastapi-project ExecStart=/usr/local/bin/uvicorn main:app --host 0.0.0.0 --port 8000 --reload [Install] WantedBy=multi-user.target
Replace /path/to/your/fastapi-project
with the actual path to your FastAPI project. The --reload
flag is used for hot reloading.
Step 4: Start and Enable the Systemd Service
Start the systemd service:
sudo systemctl start fastapi-project.service
Enable the service to start on boot:
sudo systemctl enable fastapi-project.service
Step 5: Install and Configure Nginx
Install Nginx:
sudo apt-get update sudo apt-get install nginx
Step 6: Test Your FastAPI Application
Test your FastAPI application by visiting http://your_domain_or_server_ip:8000
in your web browser or using a tool like curl
:
curl http://your_domain_or_server_ip:8000
You should see the JSON response {"Hello": "World"}
if everything is set up correctly.
Step 7: How to Restart the Service
To restart the FastAPI service, use the following commands:
- Restart the service:
sudo systemctl restart fastapi-project.service
- Check the status of the service:
sudo systemctl status fastapi-project.service
With these commands, you can restart the FastAPI service when changes are made to the Python project.
In conclusion, deploying a FastAPI project on a Linux server with Nginx and Systemd service, along with Uvicorn for hot reloading, simplifies the process of serving your application to the world. With this setup, you can easily manage and update your FastAPI application while ensuring a smooth experience for your users.