Reusable solution for deploying Django in Apache
Django plays with Apache very well. Using mod_wsgi is recommended
to host Django apps in Apache and by my (short) experience it works fine.
There are few things I did not like on all the deployment docs/manuals
and it was the way how they handle the paths to application.
I was OK with entering fixed, absolute path to my django app in host config of Apache, but it made no sense for me to insert the path to application in django.wsgi or settings.py files.
Here is the solution which worked fine.
Configuration of VirtualHost in Apache:
<VirtualHost *> ServerName www.mysite.com WSGIDaemonProcess mysite processes=2 maximum-requests=500 threads=1 WSGIProcessGroup mysite WSGIScriptAlias / /var/www/mysite/django.wsgi Alias /site-media "/var/www/mysite/site-media" Alias /admin-media /usr/lib/python2.6/site-packages/django/contrib/admin/media <Directory /var/www/mysite> Order deny,allow Allow from all </Directory> </VirtualHost>
This is the only place you need to set fixed path to django app.
As you can see, WSGIScriptAlias directive points to django.wsgi file in project directory,
which does handle requests and forward them to django app.
Here is how django.wsgi look like:
import os, sys sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..') sys.path.append(os.path.dirname(os.path.abspath(__file__))) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' os.environ['PYTHON_EGG_CACHE'] = '/tmp/python-eggs' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
As you can see, django.wsgi handler will append path of project directory into PythonPath automatically.
Now we want to do something similar in settings.py for MEDIA_ROOT.
Relevant fragments of settings.py:
import os BASE = os.path.dirname(__file__) PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) MEDIA_ROOT = os.path.join(BASE, 'site-media') MEDIA_URL = '/site-media/' ADMIN_MEDIA_PREFIX = '/admin-media/'
That’s all folks. You can now reload apache with command ‘apache2ctl graceful’ run as root and your great app should be up&running. Oh yes, don’t forget to install mod_wsgi for Apache first
- BROWSE / IN TIMELINE
- « Secrets of log4net – TelnetAppender
- » Install Chrome/Chromium in Ubuntu – quick & easy
- BROWSE / IN django
- « Digg Style Pagination In Django Revisited Once Again
- » Django 1.2 is coming
COMMENTS / 4 COMMENTS
SPEAK / ADD YOUR COMMENT
Comments are moderated.