Nat TaylorBlog, AI, Product Management & Tinkering

Deploying Django with mod_wsgi

Published on .

Today I wanted to deploy my Django application to a server running Apache. It took some fiddling, so here are my notes.

Here’s how I configured apache and mod_wsgi

WSGIScriptAlias / /mypath/wsgi.py process-group=ats
WSGIDaemonProcess ats user=ats group=ats python-home=/mypath/.venv python-path=/mypath/ats header-buffer-size=16384 threads=4
WSGIProcessGroup ats

There’s a lot in there.

  1. The first line makes the application accessible from the root directory
  2. The second line configures a daemon process named ats, under the user ats (which is important for accessing files and such) then defines the venv and the application path. header-buffer-size=16384 will resolve “Truncated or oversized response headers received from daemon process” errors. Threads adds concurrency.
  3. The third line is for stability and isolation

Keep in mind that your wsgi.py runs before an Apache directive SetEnv so you cannot use that.

Also LogLevel info is your friend, as this will help you debug any issues via the apache error log.

I had previously only run this application via the development server and hadn’t taken any care to handle static files, so my application raised 500 errors. I lazily fixed that by adding + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to urlpatterns in urls.py but this is not a good approach.

Remember you can also run python manage.py runserver to ensure everything is working.

Post Navigation

«
»