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/

* 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

    ErrorLog "/private/var/log/apache2/"

        AllowOverride All
        Options Indexes FollowSymLinks
        Order allow,deny
        Allow from all

    WSGIDaemonProcess processes=1 threads=1 maximum-requests=1

    WSGIScriptAlias / "/Users/YOUR-USERNAME-HERE/Code/wsgi_apps/"

**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       localhost broadcasthost
::1             localhost 
fe80::1%lo0     localhost #Local Django server

* Set up your actual wsgi file

import site

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 in your favorite browser and you should see your Django project.


About Chris

Python developer, Agile practitioner trying desperately not to be a pointy haired boss.
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 file I did:

    PROJECT_PATH = os.path.abspath(os.path.dirname(file))
        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:

  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?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s