Search:  
Gentoo Wiki

Apache_Modules_mod_python

Apache2 Series


Main


Modules


Addons & Tunnels


Tips


Configuring


Other

edit

Introduction

Mod_python is an Apache module that embeds the Python interpreter within the server. With mod_python you can write web-based applications in Python that will run many times faster than traditional CGI and will have access to advanced features such as ability to retain database connections and other data between hits and access to Apache internals. A more detailed description of what mod_python can do is available in this O'Reilly article.

Installation

# emerge mod_python

Edit config by appending "-D PYTHON" to the APACHE2_OPTS global variable.

File: /etc/conf.d/apache2
APACHE2_OPTS="-D PHP4 -D SSL -D DOC -D FTPD -D PYTHON"

Restart apache

# /etc/init.d/apache2 restart

Testing

Add the following Apache directives

File: /etc/apache2/modules.d/16_mod_python.conf
<Directory /var/www/localhost/htdocs> 
   AddHandler mod_python .py
   PythonHandler mod_python.publisher 
   PythonDebug On 
</Directory>


Create simple "Hello World" application

File: /var/www/localhost/htdocs/mptest.py
from mod_python import apache

def handler(req):
	req.content_type = 'text/plain'
	req.send_http_header()
	req.write("Hello World!")
	return apache.OK

Note: Be careful when cutting and pasting from your browser, you may end up with incorrect indentation and a syntax error.

Restart apache

# /etc/init.d/apache2 restart

Open the url http://localhost/mptest.py/handler.

See Also

Retrieved from "http://www.gentoo-wiki.info/Apache_Modules_mod_python"

Last modified: Wed, 06 Aug 2008 22:29:00 +0000 Hits: 16,420