Skip to main content

🚢 Deploying

This guide takes the project from the set-up guide to production: settings from the environment, a shared cache and task queue, static files and media, a worker, health checks, backups and error reports. It ends with the checks to run before you go live. The deployment checklist is the short version of the same, and Docker Compose has complete files for running it all in containers.

1. Install the server extras​

pip install "django-content-studio[server,huey,images]" gunicorn uvicorn-worker

[server] installs the PostgreSQL driver with its connection pool, the Redis client and WhiteNoise. Add [s3] to store media in an S3 bucket.

2. Configure from the environment​

With environment settings, production is the same settings.py with other variables. A server behind one reverse proxy that ends TLS might use:

DJANGO_SECRET_KEY=a-long-random-value
SITE_URL=https://example.com
DATABASE_URL=postgres://blog:secret@db:5432/blog
DATABASE_POOL=1
CACHE_URL=redis://cache:6379/0
TASKS_URL=db://
EMAIL_URL=smtp+tls://apikey:secret@smtp.example.com
DEFAULT_FROM_EMAIL="Blog <hello@example.com>"
NUM_PROXIES=1

What each one does, and why it matters:

  • SITE_URL with https turns on the HTTPS settings: secure cookies and HSTS. Let the proxy redirect HTTP to HTTPS.
  • CACHE_URL: a shared cache, here Redis. The login limits, ended sessions and presence are kept in the cache; with a per-process cache, every worker process keeps its own.
  • TASKS_URL: a task queue, here in the database, so emails and backups run on the worker instead of in the request.
  • NUM_PROXIES: the number of proxies in front of Django, so the login limits count per client and not per proxy.

DJANGO_DEBUG is off unless you set it.

3. Serve static files and media​

  1. Add WhiteNoise's middleware right after SecurityMiddleware. With DEBUG off, configure() stores the static files hashed and compressed. A fragment of the settings:

    # mysite/settings.py (a fragment)
    MIDDLEWARE = [
    "content_studio.logs.RequestIdMiddleware",
    "content_studio.health.middleware.HealthCheckMiddleware",
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    ]

    The first two lines add a request ID to the log lines and answer the health checks before Django's host check.

  2. Collect the static files on every release:

    python manage.py collectstatic --no-input
  3. Store media where your web server or a CDN serves it. With S3_BUCKET set, uploads go to an S3 bucket; otherwise they stay in MEDIA_ROOT and your web server serves /media/. Serve them with X-Content-Type-Options: nosniff, from a separate domain when you can; see the deployment checklist.

4. Run the application and the worker​

Run Django with an ASGI server, and the Huey worker next to it:

python manage.py migrate
gunicorn mysite.asgi:application --worker-class uvicorn_worker.UvicornWorker --workers 3
python manage.py run_huey

The worker runs the background tasks: password reset and notification emails, scheduled publishing records and backups.

5. Add health checks​

Include the health URLs at the root of your site:

# mysite/urls.py
from django.urls import include, path

urlpatterns = [
path("", include("content_studio.health.urls")),
path("admin/", include("content_studio.urls")),
]

Point your load balancer or container healthcheck at /readyz, which checks the database and the cache, and the worker's at manage.py content_studio_worker_alive. See Health checks.

6. Back up and hear about errors​

Three apps watch the site once it runs. Add them to INSTALLED_APPS:

# mysite/settings.py (a fragment)
INSTALLED_APPS = [
# ...
"content_studio.contrib.status",
"content_studio.contrib.backups",
"content_studio.contrib.email_activity",
]
  • Backups save the database to your storage on a schedule. Keep that storage private: a backup holds every account's password hash.
  • Email activity logs every email sent, so you can see why an invite did not arrive. It needs its logging email backend; see its page.
  • System status shows superusers the state of the database, cache, storage, queue and email in one place.

Then turn on error reports, by email or to Sentry. Set them before the configure() call, from variables of your own:

# mysite/settings.py (a fragment)
import os

CONTENT_STUDIO = {
"ADMIN_SITE": "mysite.studio.admin_site",
"ERROR_REPORTS": os.environ.get("ERROR_REPORTS", "email"),
"ERROR_EMAIL": os.environ.get("ERROR_EMAIL", ""),
}

With DEBUG off, logs are JSON lines with the request ID, ready for a log collector.

7. Check before you go live​

python manage.py check --deploy

Besides Django's own checks, this reports a weak secret key, a per-process cache, a missing NUM_PROXIES behind HTTPS, and presence or password reset emails that need a shared cache or a worker. See System checks, and go through the deployment checklist once.

With Docker Compose​

Docker Compose puts the steps above in files: a Dockerfile, compose.yaml with the backend, worker, PostgreSQL and Caddy for HTTPS, and an .env to fill in. Copy them into your project, write .env on the server and run docker compose up -d --build.