High-Performance Web Deployment with FastAPI and Nginx Proxy Manager
Deploying modern Python web applications requires a balance between rapid development, lightweight resource consumption, and resilient production infrastructure. In this comprehensive guide, we demonstrate the architectural pattern for deploying FastAPI applications alongside Nginx Proxy Manager (NPM) on Ubuntu 24.04 LTS.
Core Architecture Overview
Our deployment pipeline leverages:
1. Host OS: Ubuntu 24.04 LTS bare-metal or cloud virtual server.
2. Application Server: Python 3.12+ running FastAPI managed by uvicorn and systemd.
3. Reverse Proxy & SSL: Nginx Proxy Manager container running in Docker with Let's Encrypt automated TLS certificate renewals.
4. Caching & DB: Embedded SQLite with Write-Ahead Logging (WAL) mode for low overhead and sub-millisecond query execution.
Step 1: Preparing Python Virtual Environment & Systemd
First, isolate the FastAPI environment inside a dedicated directory:
sudo mkdir -p /var/www/my-fastapi-app
cd /var/www/my-fastapi-app
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn jinja2
Create a systemd unit file at /etc/systemd/system/fastapi-app.service:
[Unit]
Description=FastAPI Production Service
After=network.target
[Service]
User=root
WorkingDirectory=/var/www/my-fastapi-app
ExecStart=/var/www/my-fastapi-app/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8092 --workers 2
Restart=always
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now fastapi-app.service
Step 2: Routing via Nginx Proxy Manager
When routing traffic via Nginx Proxy Manager to internal services on the Docker host:
- Forward Hostname / IP: 124.158.184.37 (or Docker bridge gateway 172.17.0.1).
- Forward Port: 8092.
- Enable Block Common Exploits, Websockets Support, and configure Let's Encrypt SSL with HTTP/2 and HSTS enabled.
For long-running AI pipelines or background automations, ensure the proxy timeout is elevated:
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
Key Takeaways
Using FastAPI with Nginx Proxy Manager provides a robust foundation for modern web engineering: - Instant SSL renewals without manual cron hooks. - Microsecond response latencies powered by Starlette and Uvicorn. - Isolated processes that protect backend nodes from direct internet exposure.
Published by Cloudverra Systems Engineering Lab.