Technology news, reviews and explainers

How-to Apps & Software

How to Deploy a Django Project to Production With Gunicorn and HTTPS

Deploy a Django project step by step: production settings, Gunicorn under systemd, static files, and free automatic HTTPS with the Arka web server.

Diagram of a Django production deployment: a visitor connects over HTTPS on port 443 to a reverse proxy, which forwards requests to Gunicorn on 127.0.0.1:8000 and on to the Django app, with collectstatic and systemctl reload commands below
Image: VulnTech News illustration

Key points

  • Django's development server is not for production: run the project with a WSGI or ASGI server such as Gunicorn, behind a web server that handles HTTPS and static files.
  • Before going live, set DEBUG = False, load SECRET_KEY from the environment, list your domains in ALLOWED_HOSTS, turn on secure cookies and run manage.py check --deploy.
  • Gunicorn's documentation suggests starting with (2 × CPU cores) + 1 workers and says its default sync workers need a proxy that buffers slow clients.
  • Arka, VulnTech's web server, handles HTTPS, static files and request queueing for Django in one short host file, with Let's Encrypt certificates issued and renewed automatically.

To deploy a Django project to production, you run it with a production application server such as Gunicorn, put a web server in front of it to handle HTTPS and static files, and switch Django to production settings. This guide walks through each step on an Ubuntu or Debian server, using Django 6.1, the current release, which came out on August 5, 2026. The same steps work for Django 5.2 LTS.

Commands and settings were checked against the Django, Gunicorn and Arka documentation on October 28, 2026.

Disclosure: VulnTech News is part of VulnTech, which makes the Arka web server covered in this guide. Django and Gunicorn are independent open-source projects.

What a production Django setup looks like

Django’s built-in runserver is for development only. The deployment checklist says to use a production WSGI or ASGI server instead. A typical single-server setup has four parts:

PartWhat it doesExamples
Web server (reverse proxy)Accepts visitors on ports 80 and 443, handles HTTPS, serves static files, buffers slow clientsArka
Application serverRuns your Django code in several worker processesGunicorn (WSGI), Uvicorn (ASGI)
Process managerStarts the application server at boot and restarts it if it stopssystemd
DatabaseStores your dataPostgreSQL, MySQL, MariaDB, SQLite for small sites

Before you start

You need:

  • A Linux server (Ubuntu or Debian) with sudo access.
  • A domain name whose DNS A record (and AAAA record for IPv6) points at the server, so a certificate can be issued for it.
  • Ports 80 and 443 open in the server’s firewall and in your cloud provider’s firewall.
  • Your Django project in a Git repository, with a requirements.txt file.
  • A Python version your Django release supports. Django 6.1 needs Python 3.12 or newer, and Django 5.2 LTS supports 3.10 to 3.14, according to Django’s installation FAQ.

The examples use a project called mysite in /srv/mysite, served at example.com. Replace them with your own names.

Step 1: Prepare Django’s production settings

These settings come from Django’s deployment checklist:

  • DEBUG = False. With debug on, Django shows visitors source code, local variables and settings when an error happens.
  • SECRET_KEY: a long random value that never goes into source control. Load it from the environment.
  • ALLOWED_HOSTS: the domain names your site answers to. Django doesn’t run with DEBUG = False unless it is set.
  • CSRF_COOKIE_SECURE and SESSION_COOKIE_SECURE: set both to True once HTTPS works, so the cookies are never sent over plain HTTP.
  • STATIC_ROOT: the folder where collectstatic gathers your CSS, JavaScript and images for the web server to serve.

A minimal production block in settings.py:

import os

DEBUG = False
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
ALLOWED_HOSTS = ["example.com", "www.example.com"]
CSRF_TRUSTED_ORIGINS = ["https://example.com", "https://www.example.com"]

CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

# The reverse proxy handles HTTPS and tells Django the original scheme
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

STATIC_URL = "/static/"
STATIC_ROOT = BASE_DIR / "staticfiles"
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"

Only use SECURE_PROXY_SSL_HEADER when a proxy you control sets that header and strips any copy sent by visitors. Otherwise anyone could tell Django that a plain HTTP request was secure.

Keep database passwords out of the settings file too, and restrict database access to your application server. The checklist also suggests CONN_MAX_AGE for persistent database connections when connecting is slow, and an error-monitoring service, since email error reports don’t scale well.

Step 2: Install the project on the server

Create a user for the app, copy the code and install its dependencies in a virtual environment:

sudo apt update
sudo apt install python3-venv python3-pip git
sudo adduser --system --group --home /srv/mysite mysite
sudo -u mysite git clone https://github.com/you/mysite.git /srv/mysite/app
cd /srv/mysite/app
sudo -u mysite python3 -m venv /srv/mysite/venv
sudo -u mysite /srv/mysite/venv/bin/pip install -r requirements.txt gunicorn

Put the secret key and other settings in an environment file that only root and the app can read:

sudo install -o root -g mysite -m 640 /dev/null /etc/mysite.env
echo "DJANGO_SECRET_KEY=$(python3 -c 'import secrets; print(secrets.token_urlsafe(50))')" | sudo tee /etc/mysite.env > /dev/null

Then load it, prepare the database and static files, and run Django’s own security check:

cd /srv/mysite/app
sudo -u mysite bash -c 'set -a; . /etc/mysite.env; set +a
  /srv/mysite/venv/bin/python manage.py migrate
  /srv/mysite/venv/bin/python manage.py collectstatic --noinput
  /srv/mysite/venv/bin/python manage.py check --deploy'

check --deploy reports production settings that are missing or unsafe. Fix what it lists before going live.

Step 3: Run Django with Gunicorn

Django’s Gunicorn guide shows the basic command, run from the folder that holds manage.py:

gunicorn mysite.wsgi

On its own, that starts one process with one thread on 127.0.0.1:8000. For a real site, start more workers. Gunicorn’s design notes suggest (2 × CPU cores) + 1 as a starting point, adjusted under load, so 3 workers on a 1-core server and 5 on a 2-core server.

To start Gunicorn at boot and restart it when it stops, create a systemd service. This one is adapted from the example in Gunicorn’s deployment documentation, at /etc/systemd/system/gunicorn.service:

[Unit]
Description=Gunicorn for mysite
After=network.target

[Service]
Type=notify
User=mysite
Group=mysite
WorkingDirectory=/srv/mysite/app
EnvironmentFile=/etc/mysite.env
ExecStart=/srv/mysite/venv/bin/gunicorn mysite.wsgi --workers 3 --bind 127.0.0.1:8000
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Start it and check that Django answers locally:

sudo systemctl daemon-reload
sudo systemctl enable --now gunicorn
curl -I -H "Host: example.com" http://127.0.0.1:8000/

Binding to 127.0.0.1 keeps Gunicorn off the public internet. Visitors only reach it through the web server.

Step 4: Put Arka in front of Gunicorn

Gunicorn’s documentation “strongly” recommends running it behind a proxy server. With the default sync workers, the proxy must buffer slow clients, or Gunicorn becomes open to denial-of-service attacks. Django’s static files guide adds that web servers are better and faster at serving static files than Django. This guide uses Arka for that job.

Arka is a web server and reverse proxy written in Rust, made by VulnTech, with automatic HTTPS from Let’s Encrypt and a browser-based control panel. It is free to use, including commercially, and comes as a .deb package for 64-bit Ubuntu and Debian. Version 0.1.8 was current on October 28, 2026.

Install it as its installation guide describes (stop any other program using ports 80 and 443 first):

curl -fL -o arka.deb https://upgrades.vulntech.com/v1/arka/download/stable/0.1.8/x86_64-linux
sha256sum arka.deb    # compare with the checksum on the download page
sudo apt install ./arka.deb

Add your email address for certificate expiry warnings to /etc/arka/arka.toml, then restart Arka once with sudo systemctl restart arka:

[acme]
email = "you@example.com"

Then create one file for the site. This host file follows Arka’s Django recipe, with the HTTPS lines from its first-site guide, at /etc/arka/hosts/example.com.toml:

server_names = ["example.com", "www.example.com"]

[tls]
acme = true            # free Let's Encrypt certificate, renewed automatically
redirect_http = true   # send http:// visitors to https://
hsts = true

[[routes]]             # files gathered by collectstatic
path = "/static/"
handler = { type = "static", root = "/srv/mysite/app/staticfiles", strip_prefix = true, cache_control = "public, max-age=3600" }

[[routes]]             # user uploads
path = "/media/"
handler = { type = "static", root = "/srv/mysite/app/media", strip_prefix = true }

[[routes]]
path = "/"
[routes.handler]
type = "proxy"
upstreams = ["127.0.0.1:8000 max_conns=3"]   # match gunicorn --workers 3
queue = { size = 200, timeout = "30s" }      # wait in line when every worker is busy
preserve_host = true
buffering = {}                               # slow clients never hold a worker

Arka applies a saved host file within milliseconds, without a restart, and requests the certificate on its own. To check a file before it goes live, run sudo -u arka arka --config /etc/arka/arka.toml check. The arka user needs read access to the staticfiles and media folders.

Arka passes X-Forwarded-Proto, X-Forwarded-For and X-Forwarded-Host to Django and replaces any copies a visitor sends, which is what makes the SECURE_PROXY_SSL_HEADER setting from step 1 safe. With max_conns and queue, Arka holds extra requests in a first-come, first-served line instead of piling them onto Gunicorn’s workers, and answers 503 with Retry-After when the line is full.

Step 5: Check HTTPS and the live site

Once the certificate is issued, check the site from your own computer:

curl -I https://example.com/
curl -I http://example.com/    # should redirect to https://
curl -I https://example.com/static/admin/css/base.css

The first should answer 200 (or the redirect your home page uses), the second a redirect to HTTPS, and the third 200 from the web server. If you use the admin, open /admin/ and sign in to confirm that sessions and CSRF work over HTTPS. Run manage.py check --deploy again with the final settings.

How to deploy updates

Each new release follows the same short routine:

cd /srv/mysite/app
sudo -u mysite git pull
sudo -u mysite /srv/mysite/venv/bin/pip install -r requirements.txt
sudo -u mysite bash -c 'set -a; . /etc/mysite.env; set +a
  /srv/mysite/venv/bin/python manage.py migrate
  /srv/mysite/venv/bin/python manage.py collectstatic --noinput'
sudo systemctl reload gunicorn

reload sends Gunicorn a HUP signal, which starts new workers with the new code and shuts the old ones down gracefully. Back up the database before running migrations that change or remove data.

Why use Arka for Django

Arka’s documentation describes several features that matter for a Django deployment:

  • One file per site. HTTPS, static files, uploads and the proxy to Gunicorn live in a single host file, and saved changes apply in about 2 milliseconds without a restart or dropped connections.
  • Automatic HTTPS. Certificates from Let’s Encrypt (or ZeroSSL and other ACME providers) are requested and renewed on their own, with HSTS and modern TLS settings by default.
  • Protection for Gunicorn’s workers. Buffering keeps slow visitors from holding a worker, and max_conns with queue lines up requests when every worker is busy instead of overloading them.
  • Built for speed. Arka runs one event loop per CPU core, keeps small files in memory, compresses static files once and reuses the result, and supports HTTP/2 and HTTP/3.
  • Security built in. A web application firewall with Arka’s own rules or the OWASP Core Rule Set 4, automatic bans for repeat attackers, rate limits, and protection against request smuggling and slow clients.
  • A control panel. Live charts, logs, certificates and a form for every setting, with team accounts and an audit log.

Common Django deployment errors

SymptomLikely causeFix
400 Bad Request on every pageThe domain isn’t in ALLOWED_HOSTSAdd the exact host name, then sudo systemctl reload gunicorn
Pages load without CSSStatic files weren’t collected or the web server points at the wrong folderRun collectstatic and check the /static/ path matches STATIC_ROOT
403 CSRF verification failed on formsThe HTTPS origin isn’t trusted, or the proxy doesn’t pass the schemeAdd https:// origins to CSRF_TRUSTED_ORIGINS and set SECURE_PROXY_SSL_HEADER
502 Bad GatewayGunicorn isn’t running or listens on a different addressCheck systemctl status gunicorn and journalctl -u gunicorn
Redirect loop to HTTPSSECURE_SSL_REDIRECT is on but Django can’t see the original schemeSet SECURE_PROXY_SSL_HEADER, or let the web server do the redirect

Once the site is live, keep Django on a supported release. Django’s download page lists 5.2 LTS as supported until April 2028 and 6.1 until December 2027, so plan upgrades before those dates. Securing the server itself matters as much as the app: our SOC analyst roadmap explains how attacks on servers are spotted and investigated, and our guide to the best places to learn cyber security includes free labs on how web applications like yours are attacked.

Sources

  1. Deployment checklist (Django 6.1 documentation)
  2. How to use Django with Gunicorn (Django documentation)
  3. How to deploy static files (Django documentation)
  4. Django 6.1 release notes
  5. What Python version can I use with Django? (Django FAQ)
  6. Download Django and supported versions
  7. Deploying Gunicorn (Gunicorn documentation)
  8. Gunicorn design and worker count (Gunicorn documentation)
  9. Install Arka (Arka documentation)
  10. Your first sites: proxying an application (Arka documentation)
  11. Guides and recipes: Django, Flask and other Python apps (Arka documentation)
  12. Arka web server (VulnTech)