Django and mod_wsgi deploy example

Posted: January 13th, 2010 | Author: | Filed under: Django | Tags: , , , , | Comments Off

So what we do next when we finished to develop(debug, debug… debug…!) our application? We go and deploy it of course!
Well, a common choice of deploying Django application is to use Apache httpd and mod_wsgi.

Let’s suppose the following scenario:

  • our application name is marina
  • our application is placed inside /home/ directory
  • our static files (javascript, images, css etc…) are inside /home/marina/media/

The first step is to create the WSGI file and place it inside /home/marina/
With the following content:

import os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'  # this is your file
os.environ['PYTHON_EGG_CACHE'] = '/tmp'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

The second step is to edit your httpd.conf and add the following lines.

<Directory "/home/marina/media">
   Order deny,allow
   Allow from all

<Directory "/home/marina">
    AllowOverride All
    Order deny,allow
    Allow from all

Alias /media/ /home/marina/media/
ErrorLog "logs/"
CustomLog "logs/" common
# mod_wsgi configuration is here
# we are running as user/group 'deamon', if you don't have those you need to change or create.
WSGIDaemonProcess marina user=daemon group=daemon processes=2 threads=25
WSGIProcessGroup marina
# this is our WSGI file.
WSGIScriptAlias / /home/marina/marina.wsgi

We need to be sure that our folder is readable by the ‘daemon‘ user

chown -R daemon /home/marina

NOTE: if you are using /media/ to store all the static files be sure you have this inside your

ADMIN_MEDIA_PREFIX = '/admin_media/' # by default this is /media/ which conflicts with our /media/ !!

And finally don’t forget to restart your apache.

If you still have issues to run a Django application its worth checking the error logs.

Comments are closed.