Setup Django with Passenger Prefpane

I am loving Django for web development. I didn’t have it set up to serve my (development) projects automatically until just now.

I had installed the Passenger Prefpane, which greatly simplifies the management of VirtualHost-based serving of sites, at least for Rails and other ruby-based frameworks. With a little work, you can use the same setup to serve Django projects.

Rather than re-detail the setup, I’ll just point you to the mod_passenger setup, and the Passenger Prefpane setup pages.

Now, to set up a Django project: obviously you need a django project. Create one, and note where it is located. I stick all of mine in ~/Sites.

Add a file to the root of this project, called passenger_wsgi.py. It needs to contain the following data:

1 import os, sys
2 sys.path.append('/Users/matt/Sites') # Replace with your directory
3 os.environ['DJANGO_SETTINGS_MODULE'] = 'testing.settings' # replace with your projectname.
4 import django.core.handlers.wsgi
5 application = django.core.handlers.wsgi.WSGIHandler()

Now, add the site to the Passenger prefpane. My site is the testing.local site:

Now visit the address and ensure that it works. You should get the basic you need to set up django message.

To get the admin media served by the standard apache setup, I created a link inside the /Library/WebServer/Documents directory to /Library/Python/2.5/site-packages/django/contrib/admin/media. This can be done inside the Terminal:

sudo ln -s /Library/Python/2.5/site-packages/django/contrib/admin/media /Library/WebServer/Documents/admin-media/

Then, change the ADMIN_MEDIA setting in your django projects to http://localhost/admin-media/. This is probably the weakest point in the setup, as it will only work for pages served to your machine, not others on your network.

1 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
2 # trailing slash.
3 # Examples: "http://foo.com/media/", "/media/".
4 ADMIN_MEDIA_PREFIX = 'http://localhost/admin-media/'

I had some issues with mod_passenger serving the data for localhost (and arne, my actual machine’s name) from the first installed VirtualHost. To overcome this, I put in a new file into /etc/apache2/other/localhost.conf, which looks like:

1 <VirtualHost *:80>
2   DocumentRoot "/Library/WebServer/Documents"
3   <directory "/Library/WebServer/Documents">
4     Order allow,deny
5     Allow from all
6   </directory>
7 </VirtualHost>

This forces unnamed, or other sites to work as intended. Including the /User/*/Sites directories.