Tuesday, December 11, 2007

Using mod_python apache module on Windows

Installation (Python 2.5.1 + mod_python 3.3.1 + Apache 2.2.6)

1. Install Python 2.5.1

2. Install Apache 2.2.6

We choose to install the Apache server as a system service.

At the end of installation process, the installer will create default configuration files, the firewall software may block that action, we’d better to shutdown the firewall application before installing Apache

3. Install mod_python 3.3.1

At the end of installation process, the installer will ask you the Apache directory, then it will install mod_python.so to the APACHE_ROOT/modules directory

Configuring & Testing

We will use the default document root directory (APACHE_ROOT/htdocs)

1. Following the direction of mod_python manual, add following line to Apache configuration file (APACHE_ROOT/conf/httpd.conf)

LoadModule python_module libexec/mod_python.so

2. Add the following Apache directives,

AddHandler mod_python .py

PythonHandler mptest

PythonDebug On

3. This redirects all requests for URLs ending in .py to the mod_python handler

4. Restart Apache in order for the changes to take effect

Troubleshooting: Failed to restart apache server

Use Event Viewr to find out the problem:

In System Event say:

The Apache2.2 service terminated with service-specific error 1 (0x1).

In Application Event say:

Apache/conf/httpd.conf: Cannot load

Apache/modules/mod_python.so into server: The specified module could not be found.

From the python interpreter, we can import mod_python successfully, why? mod_python.so use python25.dll, and our user path variable already contain the Python directory; We start the Apache server as a system service, it will use the system path variable to find the import dll files; So we should add the Python directory to the system PATH variable

Edit mptest.py file in the APACHE_ROOT/htdocs directory so that is has the following lines

from mod_python import apache

def handler(req):

req.content_type = 'text/plain'

req.write("Hello World!")

return apache.OK

5. Point your browser to the URL referring to the mptest.py(for example, http://localhost/mptest.py); you should see "Hello World!".

Troubleshooting: No “Hello World!”

From APACHE_ROOT/log/error.log say:

[error] make_obcallback: could not import mod_python.apache.\n

[error] make_obcallback: Python path being used "['C:\\\\Python\\\\python25.zip', '.\\\\DLLs', '.\\\\lib', '.\\\\lib\\\\plat-win', '.\\\\lib\\\\lib-tk', 'C:\\\\Apache\\\\bin']".

[error] get_interpreter: no interpreter callback found.

[error] [client 127.0.0.1] python_handler: Can't get/create interpreter.

Apache server cann’t import mod_python.apache, we should add a PYTHONPATH system variable to contain the python modules path, for example, we can set it as following

C:\Python;C:\Python\DLLs;C:\Python\Lib;

C:\Python\Lib\lib-tk;C:\Python\Lib\site-packages

No comments: