Saturday, June 22, 2013

The shortest path to serving a 'Hello World' using Apache and Python WSGI on Windows


So you want to create a website or service with some dynamic content, using templates, database and some code ? Get ready to choose form a gazillion choices in languages, frameworks and design patterns. And one of them is using Apache as the server, Python as the language and the Web Server Gateway Interface (WSGI)  to link them. All three are documented abundantly but for some reason I have not found a simple step by step guide to set it up on a Windows machine and just get 'Hello World' to show up in my browser. So here is how I did it.

Install Python. It's a standard Windows installer so that's just point and click. Then make sure the path to 'python.exe' is in your PATH environment variable

Install Apache using XAMPP.  This is also available as Windows installer and you can also install MySQL, FileZilla, Tomcat and Mercury. I'd advice to install at least MySQL. After all a database is one of the minimal requirements for most websites. In my example XAMPP is installed to D:/Xampp.
Using the XAMPP control panel, start Apache and make sure it's running: open a browser and type 'localhost' as the URL. If it works it will show the XAMPP configuration screen.

Now get the 'wsgi_mod.so' binary that matches your configuration. It must be the 32 bit or 64 bit version depending on system AND it must match the Apache version that is installed. Now XAMPP does not really show you which version it is using so the easiest way to find out is looking at the CHANGES.txt file in the apache folder. The current version of XAMPP (June 2013) contains version 2.4.3. This means that none of the 'wsgi_mod.so' files on the google.code page will work since they all target version 2.2 ! 
I Googled for 'mod_wsgi apache 2.4' and found it in the Apache Lounge .

Copy the 'wsgi_mod.so' to the 'apache/modules/' folder.
Add this line to 'httpd.conf' to load the mod_wsgi module:

LoadModule wsgi_module modules/mod_wsgi.so


Stop and start Apache to make sure it's still working.


Create a directory D:/Xampp/wsgi. Make sure you can run python scripts in this directory: open a command window, make sure you're in this directory and just type 'python'. If all is well it will show the installed python version and a command prompt. If not, check the environment variable 'PATH' and make sure the python folder is included.

Add the following lines to  'httpd.conf'. This will allow access to the wsgi directory, and make sure all wsgi requests are routed to the directory:

<Directory "D:/xampp/wsgi" >
    AllowOverride None
    Options None
    Require all granted
</Directory>

# Python WSGI interface module
<IfModule wsgi_module>

  WSGIPythonHome "D:/Python27"
  WSGIScriptAlias / D:/xampp/wsgi
</IfModule> 


Now create a text file with the following contents:

  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] 


Save it as 'hello_world.wsgi' in the 'wsgi' directory 
Open your browser and type 'localhost/hello_world.wsgi'. And yes: It will show 'Hello World' !


Note that strictly spoken the 'WSGIPythonHome "D:/Python27"'  is not required for this example, but if you want to add code that uses the 'import' statement Apache needs to know where to find your Python directory.

UPDATE : The 64 bit difference

After writing the initial post I changed computers to a 64 bit Windows 8 laptop. And then it suddenly does not work as advertised. Xampp installs without a problem, but it seems impossible to get Apache to load the mod_wsgi.so module. When I startup apache manually using the command prompt it stops on 'mod_wsgi is not a valid Win32 application'.  And even after trying several different versions of mod_wsgi.so it will not start. As I expect it has to do with the fact that this is 32 Bit Apache running on a 64 bit system it might be better to install a 64 bit version. Just download the installation from https://www.apachelounge.com/download/win64/ and extract the zip file. It's not really an installer, this will just unpack a complete apache folder with all binaries and required subfolders. If we now just replace the original xampp/apache folder with the one we just unpacked it will work just fine. Make sure you first save the httpd.conf file, or it will be overwritten and you'll have to apply all the changes for using the wsgi module.