Django and mod_wsgi deploy example

Django and mod_wsgi deploy example | Django foo

Django and mod_wsgi deploy example

Posted: January 13th, 2010 | Author: Davo | Filed under: Django | Tags: , , , , | No Comments »

So what do 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:

01 #!/usr/local/bin/python
02 import os, sys
03 sys.path.append('/home/')
04 sys.path.append('/home/marina/')
05 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'  # this is your settings.py file
06 os.environ['PYTHON_EGG_CACHE'] = '/tmp'
07  
08 import django.core.handlers.wsgi
09  
10 application = django.core.handlers.wsgi.WSGIHandler()

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

01 <Directory "/home/marina/media">
02    Order deny,allow
03    Allow from all
04 </Directory>
05  
06 <Directory "/home/marina">
07     AllowOverride All
08     Order deny,allow
09     Allow from all
10 </Directory>
11  
12 Alias /media/ /home/marina/media/
13 ServerAdmin [email protected]
14 ErrorLog "logs/marina.com-error_log"
15 CustomLog "logs/marina.com-access_log" common
16 # mod_wsgi configuration is here
17 # we are running as user/group 'deamon', if you don't have those you need to change or create.
18 WSGIDaemonProcess marina user=daemon group=daemon processes=2 threads=25
19 WSGIProcessGroup marina
20 # this is our WSGI file.
21 WSGIScriptAlias / /home/marina/marina.wsgi

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

1 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

1 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.

你可能感兴趣的:(example)