September 25, 2009
Setup Django with mod_wsgi on your Mac
In spirit of writing this down so I don’t forget, I humbly submit this to the Internets 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 but feel free to use Homebrew or any other fine package manager.
- 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 <VirtualHost *:80> ServerName local.yourdomain.com ErrorLog "/private/var/log/apache2/local.yourdomain.com-error_log" <Directory /Users/YOUR-USERNAME-HERE/Code/wsgi_apps> AllowOverride All Options Indexes FollowSymLinks Order allow,deny Allow from all </Directory> 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" </VirtualHost>
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 OKSet 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 and Doug Hellmann’s kick-ass virtualenvwrapper, 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 startThat should be it! Go to http://local.yourdomain.com in your favorite browser and you should see your Django project.
Filed under: Django,Programming,Python
Next: Django continuous integration with Hudson and Nose
Previous: Memcached and WordPress MU
Related
- For Google, Rails on Tiger not reading database.yml
- A short introduction to Django and Gearman
- Django cache keys: making them safer
-
Iraê