Wednesday, June 17, 2009

Django, Apache 2.2, mod_ssl, and mod_wsgi (Part 2)

Part 1 | Part 2 | Part 3

I'm going to make an assumption. You have 1 domain and can create hosts under that domain. Fr example, say you own I'm assuming you have the ability to create "" with whomever hosts your DNS.

You'll want to setup 2 domains:

Since this process is virtually different for everyone I won't cover it. Contact your DNS provider for assistance here.

Setting up Apache
If you've never used Apache, you can pretty much control the entire webserver from 1 file: httpd.conf. By default it's located in {apache install direcotory}\conf\httpd.conf. I like to edit it in Wordpad becuase I'm a lightweight, but you're welcome to edit it in whatever hacker-rific editor you prefer.

Installing mod_wsgi
mod_wsgi is a module addon for Apache which is responsible for accepting the incoming HTTP connection, turning that connection into a python object, and then passing it to Django. In other words, it's really important.

  • Copy the .so file you downloaded into {apache install directory}\modules.
  • Open the httpd.conf file and find the section that has all the "LoadModule"s in it
  • Add this line:
    LoadModule wsgi_module modules/
  • This will load the wgi module whenever Apache runs
Configuring Virtual Hosts
Let's recall what our objectives for this project are:
  • Serve up Django with mod_wsgi by default from the site root
  • Serve up static media (images, css, js, etc.) from a separate virtual host
  • Offer the Django app in both HTTP and HTTPS
To achieve this, we'll need to setup at least 2 virtual hosts:
  1. to hold the Django application
  2. to hold all of our static media
We'll also need a way to configure Django to know where our static virtual host is.

In case you're wondering, there are a few reasons why this is an ideal setup:
  1. We want our Django application to worry about 1 thing: serving up python
  2. It would probably be a performance hit if python had to process all of our media files. It makes more sense to have them directly served by apache
  3. We can enable caching for everything on our static media virtual host, also improving performance
  4. It's uber leet cool (no, not really)
You can create virtual hosts in apache with the directive. Here's a sample of 2 virtual hosts:

NameVirtualHost *:80

<VirtualHost *:80>

<VirtualHost *:80>

This will give us 2 domains to serve from.

Still to come....
  • Configuring wsgi and django
  • Configuring django settings to use our virtual hosts
  • Adding https support
  • Forcing /admin to use https only


  © Blogger template 'Minimalist G' by 2008

Back to TOP