What Is FastAPI?
FastAPI is a modern Python web framework for building APIs. It leverages async capabilities and automatic OpenAPI documentation. Deploying FastAPI on your Breeze with Uvicorn as the ASGI server and Nginx as a reverse proxy provides a production-ready setup.
Prerequisites
- A Breeze running Ubuntu 22.04 or later
- Python 3.10+ installed
- Nginx installed
Step 1: Set Up the Project
cd /var/www/fastapi-app
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn gunicornStep 2: Create a Systemd Service
sudo nano /etc/systemd/system/fastapi.service[Unit]
Description=FastAPI Application
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/fastapi-app
ExecStart=/var/www/fastapi-app/venv/bin/gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 127.0.0.1:8000
Restart=always
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now fastapiStep 3: Configure Nginx
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxStep 4: Enable SSL
sudo certbot --nginx -d yourdomain.comPerformance Tips
Adjust the -w flag to match your Breeze CPU count. Use --access-logfile - to log requests to stdout for monitoring with journalctl.