Setting up the Apache WSGI module on OSX 10.9

Last month I posted a blog article called Running a Flask application under the Apache WSGI module. I detailed all the steps when running on a typical Ubuntu/Debian machine. Today however, I thought I’d try and do the same setup on my mac. There were a few gotchas, which is why I’m writing this. As with most things Mac, I can’t promise this as a guide for everyone, just it worked for me and hopefully it will for you.

Ok, so before I get started, I’ve only performed this setup on OS X 10.9.4, one of the problems I had, was not a problem in versions prior to OS X 10.8. However if you’re that out of date, you might want to upgrade anyway.

For the most part, setting up a Flask application is the same as on Ubuntu/Debian. You create virtualhosts in different locations on OS X (/etc/apache2/extra/httpd-vhosts.conf), the virtualhost itself is also marginally different (more on this below). The part that is majorly different is getting the WSGI module installed. On linux it was as simple as running:

# Install WSGI module
$ sudo aptitude install libapache2-mod-wsgi

# Restart Apache
$ /etc/init.d/apache2 restart

As you probably know, aptitude doesn’t exist on OS X, instead there are multiple package managers you can use instead like Homebrew or Macports. In this article I’m going to use Homebrew. So if you haven’t got that installed yet, you can find a simple command on their website which you can run to install Homebrew. Currently that command is:

$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Next we need to install the mod_wsgi Apache module, using our new Homebrew tool. Before we can do that, we have to fix a bug in OS X by symlinking our xCode toolchain (on OSX 10.8 or higher ONLY). Don’t worry about this to much, it’s just Apple being crap again. Copy, paste, forget, continue.

# Change into the toolchain directory
$ cd /Applications/Xcode.app/Contents/Developer/Toolchains/

# Create the symlink
$ sudo ln -s XcodeDefault.xctoolchain OSX10.9.xctoolchain

Now we can install the mod_wsgi Apache module using Homebrew

$ brew install mod_wsgi

Next we need to load the mod_wsgi module into Apache using our httpd.conf file (usually located at /etc/apache2/httpd.conf) Add this line next to the other LoadModule calls.

$ LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/3.4/libexec/mod_wsgi.so

Restart Apache

$ sudo apachectl restart

Virtualhost differences

Although the virtual host follows almost the exact same forgot. Apple does have its quirks, like having to wrap every in quotes. We also do not need to set the home argument on WSGIDaemonProcess. Here are the example of the Linux and OS X virtualhosts for comparison

# Linux

<virtualhost *:80>
    ServerName my.webtool

    WSGIDaemonProcess webtool user=flask group=www-data threads=5 home=/var/www/prod-webtool/
    WSGIScriptAlias / /var/www/prod-webtool/webtool.wsgi

    <directory /var/www/prod-flask>
        WSGIProcessGroup webtool
        WSGIApplicationGroup %{GLOBAL}
        WSGIScriptReloading On
        Order deny,allow
        Allow from all
    </directory>
</virtualhost>
# OS X

<virtualhost *:80>
    ServerName my.webtool

    WSGIDaemonProcess webtool user=flask group=www-data threads=5
    WSGIScriptAlias "/" "/var/www/prod-webtool/webtool.wsgi"

    <directory "/var/www/prod-flask">
        WSGIProcessGroup webtool
        WSGIApplicationGroup %{GLOBAL}
        WSGIScriptReloading On
        Order deny,allow
        Allow from all
    </directory>
</virtualhost>

Final thoughts

Ok, so that was all pretty simple right? If this doesn’t work for you, sorry, leave a comment and I’ll try to help. As new versions of OS X are released I’ll try to update this blog post.

The rest of the setup is exactly the same as for Ubuntu/Debian, so read that article for information on configuring WSGI for Apache and your application.

6 Love This

7 Comments Setting up the Apache WSGI module on OSX 10.9

  1. Graham DumpLETON

    You are using an old mod_wsgi version. More recent versions should not require you to fiddle around in the compiler toolchain directory.

    Reply
  2. Simon

    Hey, I knew I had seen your name somewhere, Google confirmed where, so thanks for the reply. What is your recommended way of installing mod_wsgi? As far as I can tell Homebrew doesn’t allowed upgrading mod_wsgi, beyond version 3.4. Using a newer version would be great and I could then update this blog. Thanks.

    Reply
  3. rpcarn

    Thanks Simon, this helped me. I’m new to CGI so a couple of things weren’t clear to me. Here are a couple of tips for other people. I am not using Flask.

    1. The script must end in .wsgi (not .py or .cgi)

    2. wsgi looks for a function called ‘application’. Here’s a sample file that worked for me (without the and tags of course, if those get published)


    #!/usr/bin/python

    def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

    Reply
    1. Simon

      Hi pal, thanks for sharing your knowledge. There is no doubt in my mind, the majority of problems with WSGI are caused by tinniest of mix-up.

      Regarding your 2nd point. Maybe I didn’t make it super clear. I wrote an article previous to this one, on running flask on a Debianesque OS, which covers exactly what you’re talking about. Maybe I should have put the content in both articles – http://www.jakowicz.com/flask-apache-wsgi/

      Reply

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.