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][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/

This entry was posted in Django, Programming, Python. Bookmark the permalink.

4 Responses to Setup Django with mod_wsgi on your Mac

  1. Iraê says:

    Very nice writeup! It helped me a lot!

    But for me one I had to append my project folder to the path, like so:

    import os, sys
    sys.path = [‘/Users/USERNAME_HERE/code/PROJECT_NAME/’] + sys.path
    os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘settings’

    Where you replace USERNAME_HERE and PROJECT_NAME by your values.

    Also in my settings.py file I did:

    PROJECT_PATH = os.path.abspath(os.path.dirname(file))
    TEMPLATE_DIRS = (
        PROJECT_PATH + ‘/templates/’,
    )

  2. Hobbes3 says:

    I followed most of your instructions but I am getting an error with MySQLdb.

    Here is the full error: http://stackoverflow.com/questions/9475626/django-apache-with-mod-wsgi-unable-to-load-mysqldb

  3. Pak Ivan G says:

    I have no /opt/ dir… Where i can find it

  4. OJ Tibi says:

    Looks like you’re installing Apache via Macports, too. Do you know of any way to install mod_wsgi and getting it to work with the bundled Apache on Mac?

Comments are closed.