Learn / Frameworks / Django / Deployment with Gunicorn

Django · Lesson 15 of 15

Deployment with Gunicorn

Production settings, gunicorn, Nginx, collectstatic and migrations on release.

  • Advanced
  • 16 min read
  • 3 objectives

Before this lessonLesson 14: Security Defaults

What you will learn

  • Split prod settings
  • Run gunicorn
  • Collect static files

Your Progress

0 of 15 lessons 0%

  • Lessons0 / 15
  • Completed0
  • Est. time left~ 4 hours

Create a free account to keep your progress on every device.

A typical production stack is gunicorn workers behind Nginx (or a platform load balancer), with Postgres, Redis, and static files on a CDN or WhiteNoise.

WSGI entry

pip install gunicorn
gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 3 --timeout 30
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN python manage.py collectstatic --noinput
CMD ["gunicorn", "config.wsgi:application", "-b", "0.0.0.0:8000", "-w", "3"]

Release steps

  • Set DJANGO_SETTINGS_MODULE to the prod module.
  • Run migrate as a release command, before traffic hits new code that needs the schema.
  • collectstatic into STATIC_ROOT (or WhiteNoise).
  • Point ALLOWED_HOSTS and CSRF trusted origins at the real domain.

What Nginx does

Terminate TLS, serve /static/ and /media/ directly, and reverse-proxy everything else to gunicorn. Health-check /health/ so a bad deploy is taken out of the pool.

# Write your solution here
Course completeYou finished DjangoReview the full course or pick your next one.