In spirit of writing this down so I don’t forget, I humbly submit this to [the Internets][funny-internet] in the vain hope that it helps someone else.
For the purposes of this I’m assuming that you have Python and Django installed. I use [Macports][macports] but feel free to use [Homebrew][homebrew] or any other [fine package manager][apt-get].
* Install apache2 and mod_wsgi
~ $ sudo port install apache2 mod_wsgi
* You’ll probably want to to add /opt/local/apache2/bin/ to your $PATH in ~/.profile
* Create an httpd.conf file
cd /opt/local/apache2/conf
sudo cp httpd.conf.sample httpd.conf
* Add the mod_wsgi module to httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
* Make sure the vhosts config file is loaded in httpd.conf
Include conf/extra/httpd-vhosts.conf
* Set up a vhost for your domain in /opt/local/apache2/conf/extra/httpd-vhosts.conf. Note the YOUR-USERNAME-HERE place holder.
NameVirtualHost *:80
ServerName local.yourdomain.com
ErrorLog "/private/var/log/apache2/local.yourdomain.com-error_log"
AllowOverride All
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
WSGIDaemonProcess local.yourdomain.com processes=1 threads=1 maximum-requests=1
WSGIProcessGroup local.yourdomain.com
WSGIScriptAlias / "/Users/YOUR-USERNAME-HERE/Code/wsgi_apps/local.yourdomain.com.wsgi"
**Note:** I amended the WSGIDaemonProcess line above so your code would get refreshed with each new request.
The ~/Code/wsgi_apps path is arbitrary, that’s just where I keep mine.
Many folks like to keep their .wsgi files in a /public directory inside their Django project. I like to work without a project folder, but either way, substitute the path to your wsgi file for /Users/YOUR-USERNAME-HERE/Code/wsgi_apps.
* Check your Apache conf before your wreck your apache conf
$ sudo apachectl configtest
Syntax OK
* Set up an alias for your domain in /etc/hosts
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 local.yourdomain.com #Local Django server
* Set up your actual wsgi file
import site
site.addsitedir('/Users/YOUR-USERNAME-HERE/.virtualenvs/YOUR-VIRTUALENV/lib/python2.4/site-packages')
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysettingsmodule.local'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I’m using [Ian Bicking’s virtualenv][venv] and [Doug Hellmann’s kick-ass virtualenvwrapper][vwrapper], and you should be too.
If you’re not __and__ you’re able to ignore the cries of all the puppies and bunnies you’re killing because you’re not, then you’ll want to remove the first two lines.
* Fire up Apache
$ sudo apachectl start
That should be it! Go to http://local.yourdomain.com in your favorite browser and you should see your Django project.
[funny-internet]: http://www.youtube.com/watch?v=iRmxXp62O8g&feature=related
[macports]: http://www.macports.org/
[homebrew]: http://github.com/mxcl/homebrew
[apt-get]: http://linux.die.net/man/8/apt-get
[venv]: http://pypi.python.org/pypi/virtualenv
[vwrapper]: http://www.doughellmann.com/projects/virtualenvwrapper/