Ojasa Mirai

Ojasa Mirai

FastAPI

Loading...

Learning Level

🟢 Beginner🔵 Advanced
🚀 What is FastAPI📚 Creating Your First API📚 Running the Server📚 HTTP Methods📚 API Endpoints📚 Request and Response📚 Documentation📚 Testing Basics
Fastapi/Api Basics/Running Server

Running the Server — Production Setup 🏢

Production servers require different configuration than development. Here's how to set up FastAPI for production environments.

Development vs Production

Development:

uvicorn main:app --reload --host 127.0.0.1 --port 8000

Production:

gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:8000

Using Gunicorn with Uvicorn

Install:

pip install gunicorn

Run:

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

The `-w 4` creates 4 worker processes for handling concurrent requests.

Worker Count Calculation

workers = (2 × CPU_cores) + 1

Example:

  • 2 CPU cores → 5 workers
  • 4 CPU cores → 9 workers
  • 8 CPU cores → 17 workers

Configuration File

Create `gunicorn_config.py`:

import multiprocessing

workers = (multiprocessing.cpu_count() * 2) + 1
worker_class = "uvicorn.workers.UvicornWorker"
bind = "0.0.0.0:8000"
timeout = 120
keepalive = 5
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(D)s'

Run with config:

gunicorn -c gunicorn_config.py main:app

Docker Deployment

Dockerfile:

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "main:app", "--bind", "0.0.0.0:8000"]

Build and run:

docker build -t fastapi-app .
docker run -p 8000:8000 fastapi-app

Nginx Reverse Proxy

upstream uvicorn {
    server localhost:8000;
    server localhost:8001;
    server localhost:8002;
    server localhost:8003;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://uvicorn;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Environment Variables

from pydantic import BaseSettings

class Settings(BaseSettings):
    debug: bool = False
    database_url: str
    secret_key: str

    class Config:
        env_file = ".env"

settings = Settings()

.env file:

DEBUG=false
DATABASE_URL=postgresql://user:password@db:5432/mydb
SECRET_KEY=your-secret-key-here

Health Checks

@app.get("/health")
async def health_check():
    return {"status": "healthy"}

Docker healthcheck:

HEALTHCHECK --interval=30s --timeout=10s --start-period=5s \
    CMD curl -f http://localhost:8000/health || exit 1

Performance Monitoring

import time
from starlette.middleware.base import BaseHTTPMiddleware

class TimingMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        start = time.time()
        response = await call_next(request)
        duration = time.time() - start
        response.headers["X-Process-Time"] = str(duration)
        return response

app.add_middleware(TimingMiddleware)

🔑 Key Takeaways

  • ✅ Use Gunicorn + Uvicorn for production
  • ✅ Calculate workers based on CPU cores
  • ✅ Use Nginx as reverse proxy for load balancing
  • ✅ Containerize with Docker
  • ✅ Monitor with health checks
  • ✅ Log request times and metrics

Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn