Skip to content
COL0

How to deploy Django or FastAPI on a server (Gunicorn, Uvicorn and Nginx)

Running `runserver` or `uvicorn --reload` is fine for development, not for production. These are the pieces you need to make your Python API stable and secure.

Written by Victor Barrera

What you need before starting

  • A Linux server with Python (the same version you develop with) and SSH access.
  • A requirements.txt or pyproject.toml with pinned dependencies.
  • A database (PostgreSQL is the usual choice) and a domain.

The steps, in short

  1. Prepare the server: your own user, firewall and updated packages.
  2. Create a virtual environment and install the dependencies.
  3. Configure for production. In Django: DEBUG=False, ALLOWED_HOSTS with your domain and SECRET_KEY from an environment variable. In FastAPI: turn off the open docs if the API is private.
  4. Apply migrations and, in Django, run collectstatic.
  5. Run with an application server: Gunicorn for Django (gunicorn project.wsgi) or Gunicorn with the Uvicorn worker for FastAPI.
  6. Create a systemd service with automatic restart.
  7. Put Nginx in front to serve static files and forward the rest, and enable HTTPS.

Common mistakes

  • Leaving DEBUG=True in production, which shows code and configuration on any error.
  • Committing the SECRET_KEY or passwords to the repository.
  • Using SQLite with several processes and a lot of traffic.
  • Forgetting CSRF_TRUSTED_ORIGINS when the site is behind an HTTPS proxy.

How we help

We leave your Python application running with Gunicorn, Nginx, HTTPS and automatic deploys from your repository.

Frequently asked questions

Can I use Django's development server in production?

No. It is not designed for production traffic or security. Use Gunicorn (or Uvicorn/Gunicorn for FastAPI) behind Nginx.

How do I serve Django's static files?

Run `collectstatic` and have Nginx serve that folder directly, or use WhiteNoise. Files uploaded by users go in another folder or in external storage.

Ready to publish your project?

Book your free 30-minute session and leave with a clear plan.

Book your free session