Installing Mod_wsgi and Python for Apache on Windows

Tutorial on how to install Mod_wsgi and Python for use with the Apache HTTP Server.

37878 views

Edited: 2018-02-25 04:08

Mod_wsgi and Apache, Windows

This tutorial deals with the installation of Mod_wsgi on Windows, which is a module to host python applications. To get started you first need to download the files corresponding to your version of Apache.

Windows Binary downloads are available from: http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi

Link to Mod_wsgi's GitHub page: https://github.com/GrahamDumpleton/mod_wsgi

Installing Mod_wsgi for Apache on Windows

Apache2.2 needs mod_wsgi-win32-ap22py27-3.3.so which you should rename to mod_wsgi.so and place into your apache modules directory. Of cause this depends on where you installed Apache, but as an example this could be:

C:\Program Files\Apache Software Foundation\Apache2.2\modules

For simplicity, create a new directory outside your htdocs, simply name the new directory wsgi_app for now, and place it in c:

The final path to your new directory should look like:

c:\wsgi_app

Configure Mod_wsgi in Apache on Windows

The nest step is to configure Apache to run a basic test application from the directory you just made. So you will have to find your http.conf file, located in:

C:\Program Files\Apache Software Foundation\Apache2.2\conf

Open the file with your favorite editor, and insert the below:

LoadModule wsgi_module modules/mod_wsgi.so

You can do a search for LoadModule, and place it directly below any other modules that you load. Otherwise, simply place it at the end of the file, so that you can easily find it later.

You should now create a WSGI script alias, this is used to direct requests to your new application. The below would direct requests for /wsgi to a file placed in your wsgi_app directory. But you should also create a directory block for your wsgi_app directory, both demonstrated below.

WSGIScriptAlias /wsgi "C:/wsgi_app/wsgi_app.py"
<Directory "C:/wsgi_app">
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>

Again, for simplicity, you may want to find the section in the file, where the other Directory settings are located, and then place the above below the <Directory "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"> block.

Your first Python web Application

Create a file named wsgi_app.py inside of your wsgi_app directory. Then open this file in your editor, and add the below code to the file.

def application(environ, start_response):
    status = '200 OK'
    output = 'This is my Website!'

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

    return [output]

All you need to do now is to restart your Apache web server, and try and type in either yourdomain.com/wsgi, or localhost/wsgi (depending on your setup), to see if things work.

Tell us what you think:

phapha

hey there, nice tutorial !
You should consider send it or ask to add a link in the official documentation & github, that is rather poor for windows environment.
ypou should maybe mention the pip install as well, since graham declared all other methods deprecated...

Trevor

Nice tutorial, well done. What I haven't been able to do is run more than one application on window due to the service daemon for wsgi not being available, is there a way to do this for 2 ssl apps with flask and apache?

Ray

Trevor, use the "WSGIApplicationGroup <group>" apache directive to specify different groups for different apps.