My ongoing experiences with Ubuntu, and later Mythbuntu, as a media center with MythTV. I'm also using the system for a virtual machine server, a mediawiki server and a general all around home infrastructure base.

Monday, June 29, 2009

Getting Django running under Apache

I heard a number of good things about Django and wanted to give it a try. After following the tutorial and playing around with it, I found it really cool. BTW, there is also good documentation to be found at the Django Book site.

After playing a little, I decided I wanted to try deploying Django into my existing Apache instance. And well, I found the documentation for doing this somewhat lacking (even in the book). But I forged ahead and catched what it took here. Hopefully it will either be helpful or someone can come along and tell me I missed something.

First, I started by installing Django on my Ubuntu server. I already had Apache installed and working btw.
sudo apt-get install python-django

Reading through the Django document I decided to go the modwsgi route since that seem to be the recommended course. I followed the directions for installing modwsgi:
sudo apt-get install libapache2-mod-wsgi

Then I created a Django site to run my applications in. Pick the name of your site carefully - as far as I can tell, it will appear in all your URLs (maybe I'm wrong on this as I haven't experimented too much):
cd /usr/local
sudo django-admin startproject django_site

Now, you could just go ahead and make your site owned by the Apache user (www-data), but I decided to make it owned by me so that I could easily edit things:
sudo chown -R vwelch django_site
Now create /usr/local/django_site/django.wgsi that looks like the following:
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'django_site.settings'
sys.path.append('/usr/local')
sys.path.append('/usr/local/django_site')

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

Now go ahead and edit /usr/local/django_site/settings.py and set up your database. I used SQLite3 since it was the simplest. You will need to put the database somewhere the Apache user (www-data) has write access. In my case I created a directory just for this purpose:
sudo mkdir /usr/local/django_db
sudo chown www-date /usr/local/django_db

So the relevant two lines of my settings.py file looked like:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/usr/local/django_db/database.db'

Now go ahead and create the database - you must do this as the Apache user:
sudo -u www-data ./manage.py syncdb


Time to proceed with Apache configuration. Create the file /etc/apache2/sites-available/django.conf (using 'sudo vi') so that it looks like the following. Note that I believe the first argument to WSGIScriptAlias "/django_site" must patch your site name. The second line lets the admin application find it's CSS (I haven't dealt with the case another application also wants CSS).
WSGIScriptAlias /django_site /usr/local/django_site/django.wsgi
Alias /media /var/lib/python-support/python2.6/django/contrib/admin/media

Now enable the site in Apache and restart it:
sudo a2ensite django.conf
sudo apache2ctl restart


Now visit you should be able to visit http://hostname/django_site and get a Django welcome page. If not, well, something is wrong :-/

At this point you can add applications under /usr/local/django_site as you normally would and have them appear at http://hostname/django_site/appname

Good luck. And as I mentioned I figured this out through mostly trial and error. If anyone can correct me or point me at better docs, I'd be grateful.

No comments: