Running Python through Apache
mapserver, python, web development
apache, gdal, mod_python, python, tilecache
I’m trying (and having) to start using Python more and more due to its ubiquity in the OpenSource GIS world. There are a number of add-ons and libraries for MapServer that require Python to function properly and/or quickly. It is also widely used for scripting both MapServer objects and for raster manipulation using GDAL. I initially set up Python under Apache to run the Tilecache tile server.
According to the ModPython site - “Python will run many times faster than traditional CGI and will have access to advanced features such as ability to retain database connections.”
|image0|Mod_python is an Apache module that embeds the Python interpreter within the server, and can be downloaded from their site. There are versions for both Unix and Windows, which makes it ideal for MapServer for Windows (MS4W). Ironically after saying that using Python for web applications is many times faster than CGI, the ModPython download page uses CGI..
There are a number of Windows versions available, depending on your version of the Apache server. Make sure you get the right one - at the time of writing I am using MS4W v2.3.1, which includes Apache HTTP Server version 2.2.11. Your version of Python also makes a difference - I’m using 2.5.4 so I downloaded mod_python-3.3.1.win32-py2.5-Apache2.2.exe. My reason for not yet having upgraded to Python 2.6 or 3 was because all the TileCache documentation refers to Python 2.5.
The Mod_python installer allows you to choose your Python and Apache directories which in a standard MS4W installation would be C:\Python25 and C:\ms4w\Apache respectively.
For the rest of the configuration steps follow the instructions here. If you are using MapServer for Windows then a brief summary of these steps is as follows:
1. Open your C:\ms4w\Apache\conf\httpd.conf and add the lines below. A C:\ms4w\Apache\modules\mod_python.so file should have been created during the installation of Mod_python.
LoadModule python_module modules/mod_python.so <Directory "C:/ms4w/Apache/htdocs/test"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory>
2. Add the following code to a mptest.py file in a C:\ms4w\Apache\htdocs\test folder (check the spaces/tabs for the script are correct using the Python editor IDLE):
from mod_python import apache def handler(req): req.content_type = 'text/plain' req.write("Hello World!") return apache.OK
3. Restart your Apache server service to apply the changes to the configuration. You can do this through the GUI or using:
C:\ms4w\Apache\bin\httpd.exe -k start -n "ApacheMS4WWebServer"
4. Go to http://geographika.azurewebsites.net/test/mptest.py in your browser (assuming a default HTTP port of 80) and you should see the familiar “Hello World” message. If not..see the troubleshooting page. Note that with this configuration all links ending in .py will be handled with by the same mptest.py script. So if you are editing your Python script and wondering why nothing changes (..said from experience), check your httpd.conf.
You should also be able to see a summary of your system at http://geographika.azurewebsites.net/mpinfo. Setting up TileServer is a blog post in itself and I hope to document that soon.
Finally, if in a fit of frustration with non-running Python scripts you uninstall everything mentioning the name Python from your machine, you are likely to find the error message below in your Apache log files:
[Mon Jan 18 12:28:02 2010] [error] make_obcallback: could not import mod_python.apache.\n ImportError: No module named mod_python.apache [Mon Jan 18 12:28:02 2010] [error] make_obcallback: Python path being used "['... [Mon Jan 18 12:28:02 2010] [error] [client 172.30.99.54] python_handler: Can't get/create interpreter.
A reinstall of the Windows Set Up package should fix this.