Setting up mod_wsgi with XAMPP on a Mac

September 17th, 2009 | Tagged as ,

For a project at work I needed to create a Python app, accessible via http. I’m using a Mac with XAMPP installed at work. Being used to do this sort of work mostly in PHP, I needed to learn a few things. Here is the summary of how to make it work on the localhost.

Download mod_wsgi

Python WSGI is a specification of interface between web servers and Python applications or application frameworks.  The mod_wsgi is an Apache module, using which it is possible to access Python applications (which adhere to WSGI speficiation) via http.

The mod_wsgi can be downloaded from http://code.google.com/p/modwsgi/.

Compile and install mod_wsgi

Versions used: Python 2.5.1, apache 2.2.11, mod_wsgi 2.3

Unpack the mod_wsgi and run the usual:

$ ./configure
$ make
$ sudo make install

On my Mac, the module was installed to /usr/libexec/apache2, I had to copy it manually to XAMPP location:

$ sudo cp /usr/libexec/apache2/mod_wsgi.so /Applications/xampp/xamppfiles/modules/
Write a sample Python WSGI application

The next step is to create a directory where the Python application will be stored. Unlike with PHP scripts, Apache server needs to be made aware of exact location of the app. We also have to tell Apache which particular script needs to be executed for any http request which points to the application directory. This is also very different from what you might be used to with PHP. To be able to do all the configuration changes in httpd.conf, we first need to create a directory and to create the main application script so we can write down all directory and file names required.

[ivk@ivk-mbpro /Applications/xampp/xamppfiles/htdocs]$ mkdir app-sample
[ivk@ivk-mbpro /Applications/xampp/xamppfiles/htdocs]$ cd app-sample
[ivk@ivk-mbpro /Applications/xampp/xamppfiles/htdocs/app-sample]$ vi main.py

main.py can be for instance a simple Hello World! app:

def application(env, start_response):
    start_response("200 OK", [])
    output = "<html>Hello World! Request: %s</html>"
    output %= env['PATH_INFO']
    return [output]
Put it all together

Modify the httpd.conf by adding the following:

LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias /app-sample "/Applications/xampp/xamppfiles/htdocs/app-sample/main.py"
<Directory "/Applications/xampp/xamppfiles/htdocs/app-sample">
    Order deny,allow
    Allow from all
</Directory>

WSGIScriptAlias directive makes sure that for all requests to /app-sample, the "/Applications/xampp/xamppfiles/htdocs/app-sample/main.py"will be executed by Python.

Modify mime.types by adding “py” as follows:

text/html        html htm py

After Apache restart you should be able to access your python script main.py via http:

python-sample-app-wsgi

2 Comments

yken.org » Blog Archive » Setting up mod_wsgi with XAMPP on a Mac Scripts Rss
September 17th, 2009 at 22:34

[...] posted here: yken.org » Blog Archive » Setting up mod_wsgi with XAMPP on a Mac By admin | category: directory script, scripts | tags: apache, application, [...]

Anonymous
November 25th, 2009 at 07:27

thank you

Dimitri
June 19th, 2010 at 02:02

Many thanks,
You are really cool!

Your comment

You can post anonymous comments. Some tags are allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>