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:

#!/usr/local/bin/python
import os, sys
sys.path.append('/home/')
sys.path.append('/home/marina/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'  # this is your settings.py 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>

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


Alias /media/ /home/marina/media/
ServerAdmin admin@djangofoo.com
ErrorLog "logs/marina.com-error_log"
CustomLog "logs/marina.com-access_log" 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 settings.py

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.