Deploy a Django app from GitHub, step by step

· 5 min read

Most failed first deploys of a Django app come from the same handful of settings. Fix those before you push the button and the rest is easy. This guide uses Pharos for the platform steps, but the settings advice applies to any host that runs your app in a container behind a proxy.

What the platform needs from your repository

Put the project in a GitHub repository. It should have a manage.py at the top and a package that contains wsgi.py, which is what startproject gives you. List your dependencies in requirements.txt, including Django and whitenoise.

You do not have to write a Dockerfile. If the repository has none, the platform detects Django, writes a Dockerfile for a Python 3.12 image, installs requirements.txt plus gunicorn, runs collectstatic, and starts the app with gunicorn. If you already have a Dockerfile, yours is used.

Six settings that stop most first deploys

Make these changes in settings.py and commit them.

import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = os.environ.get("SECRET_KEY", "build-only-key-set-a-real-one")
DEBUG = os.environ.get("DEBUG", "") == "1"
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "").split(",")
CSRF_TRUSTED_ORIGINS = [origin for origin in os.environ.get("CSRF_TRUSTED_ORIGINS", "").split(",") if origin]

# HTTPS is handled before your app, so trust the forwarded header.
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": os.environ.get("DATABASE_PATH", BASE_DIR / "db.sqlite3"),
    }
}

STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

Add "whitenoise.middleware.WhiteNoiseMiddleware" to MIDDLEWARE, directly after SecurityMiddleware. Here is what each piece is for:

  • Secrets from the environment. SECRET_KEY must never be in the repository. A public repo with a secret key in it is a leak the moment you push. The fallback text exists only so collectstatic can run during the build, where your variables are not available. Always set a real key on the Environment page.
  • DEBUG off. With DEBUG = True on a live site, a crash shows a page full of settings and code to every visitor. The line above keeps it off unless you set DEBUG=1.
  • Allowed hosts and CSRF origins. Django refuses requests for an unknown host with "Bad Request (400)". CSRF_TRUSTED_ORIGINS needs the full address including https://. Without it, logins and the admin fail with a CSRF error.
  • The proxy header. Without SECURE_PROXY_SSL_HEADER, Django thinks every request is plain http, and the CSRF check on secure forms fails.
  • SQLite at DATABASE_PATH. Each app has its own persistent /data folder, and the DATABASE_PATH variable points into it. Anything you write outside /data disappears on the next deploy. SQLite is included. For an app people depend on, the paid Postgres add-on gives your app its own database and we set DATABASE_URL for it. The generated image uses Python 3.12; for another version, add your own Dockerfile.
  • Static files. WhiteNoise serves them from inside the app, and STATIC_ROOT is where collectstatic puts them. A failure in collectstatic is only a warning in the build log, so open your site afterwards and check the CSS loaded.

The app must listen on port 8000, or on the PORT variable the platform sets. The generated start command already does that.

Create the app

In the dashboard choose New app. Give it a name, paste your GitHub repository address (for example https://github.com/you/yourapp), and leave the branch and framework on their defaults unless detection gets it wrong. If you connect GitHub, you can pick from your repositories, including private ones. The new app gets an address on the platform's domain, named after your app.

Set the environment variables

Open the app's Environment tab and add three values before the first deploy:

  • SECRET_KEY, a long random string. You can make one with python -c "import secrets; print(secrets.token_urlsafe(50))".
  • ALLOWED_HOSTS, your app's address without https://.
  • CSRF_TRUSTED_ORIGINS, the same address with https:// in front.

Values are stored encrypted and never shown again after you save them, so keep your own copy somewhere safe. Replacing one means saving a new value.

Deploy

The Automatic deploys tab gives you a short workflow file to commit to your repository. Each push to the chosen branch then builds on GitHub and goes to the platform. To deploy right away, open the Actions tab in your repository, pick the Pharos deploy workflow and click Run workflow. You can also choose to approve each new version yourself before it goes live.

Watch the Deploys tab. It shows each stage and its output. When the new version has started, the platform checks that it answers before switching to it. If it does not answer, the old version keeps running and the Deploys tab says what went wrong. Open the App logs tab for the traceback.

Migrations and your first admin user

Every deploy runs your migrations, and a backup of the SQLite database is taken just before. If a migration fails, the platform puts the previous version back and restores that backup.

Once the app has run at least once, two buttons on the app's Overview page are enabled. Run migrations runs migrate again inside the running app. Create superuser asks for a username, email and password, then creates the account. Open /admin/ on your app's address and sign in. If the admin page looks unstyled, the static files did not collect, so check the build log.

When it does not work

  • Bad Request (400). ALLOWED_HOSTS does not include the address. Fix the variable and deploy again.
  • CSRF verification failed. CSRF_TRUSTED_ORIGINS is missing https://, or SECURE_PROXY_SSL_HEADER is not set.
  • No CSS. STATIC_ROOT or WhiteNoise is missing, or collectstatic failed in the build log.
  • Data gone after a deploy. The database was not at DATABASE_PATH, so it lived inside the image.

What it costs

You get a 14-day trial, a card is required to start, and nothing is charged if you cancel before day 14. After that it is A$27 a month for one app. Once it is live, the platform also scans the running site for debug mode, open admin pages and exposed files. Read the pre-launch checklist before you share the address with anyone.