How to Deploy a FastAPI Application with Uvicorn

FastAPI is a modern, high-performance Python web framework, particularly popular for building APIs — this guide covers deploying it in production using Uvicorn as the ASGI server.

Why FastAPI Uses an ASGI Server (Not WSGI Like Flask/Django Traditionally)

FastAPI is built on ASGI (Asynchronous Server Gateway Interface), supporting native async/await for genuinely concurrent request handling — Uvicorn is the standard ASGI server implementation used to actually run FastAPI applications.

Prerequisites

  • Python installed with a virtual environment set up (see How to Set Up a Python Virtual Environment Correctly)

Step 1 — Install FastAPI and Uvicorn

pip install fastapi uvicorn[standard]

Step 2 — A Basic FastAPI Application

nano main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, World!"}

Step 3 — Run for Development

uvicorn main:app --reload

Step 4 — Running in Production with Multiple Workers

uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4

Multiple worker processes let you utilize multiple CPU cores — a common rule of thumb is (2 × CPU cores) + 1, though tune based on your actual workload characteristics.

Step 5 — Create a systemd Service

sudo nano /etc/systemd/system/fastapi-app.service
[Unit]
Description=FastAPI Application
After=network.target

[Service]
User=appuser
WorkingDirectory=/opt/myapp
Environment="PATH=/opt/myapp/venv/bin"
ExecStart=/opt/myapp/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
Restart=always

[Install]
WantedBy=multi-user.target

Step 6 — Start and Enable the Service

sudo systemctl daemon-reload
sudo systemctl enable --now fastapi-app

Step 7 — Configure Nginx as a Reverse Proxy

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_http_version 1.1;
    }
}

Step 8 — Add HTTPS

sudo certbot --nginx -d yourdomain.com

Using Gunicorn as a Process Manager for Uvicorn Workers (Alternative Pattern)

pip install gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker

Gunicorn's mature process management (automatic worker restart, graceful reloads) combined with Uvicorn's ASGI worker class is a common production pattern, offering more operational robustness than running Uvicorn's own worker management alone.

Exploring FastAPI's Automatic API Documentation

http://yourdomain.com/docs

FastAPI automatically generates interactive API documentation (Swagger UI) based on your endpoint definitions — genuinely useful for both development and as living documentation for API consumers.

Common Errors

"Address already in use" — see How to Fix "Address Already in Use" Port Binding Errors; verify no other process (including a leftover previous instance) is already bound to the port.

Continue Reading

Browse more articles in Programming Languages & Runtimes.

  • fastapi deployment, uvicorn production, fastapi nginx, fastapi systemd service
  • 0 用戶發現這個有用
這篇文章有幫助嗎?

相關文章

How to Install PHP on Ubuntu & Debian (Complete Guide)

PHP powers a huge share of the web, including WordPress, Laravel, and Magento. This guide covers...

How to Install Node.js on Ubuntu & Debian (with NVM)

Node.js is a fast JavaScript runtime for building APIs, web applications, and backend services....

How to Install Python on Ubuntu & Debian

Python powers web frameworks like Django and Flask, automation scripts, and data applications....

How to Install Java (OpenJDK) on Ubuntu & Debian

Java powers enterprise applications, build tools like Maven and Gradle, and platforms like...

How to Install Composer for PHP Dependency Management

Composer is PHP's standard dependency manager, used by virtually every modern PHP framework...