You need to install the following software:
a] libapache2-mod-python : Mod_python is a module that embeds the Python language interpreter within the server, allowing Apache handlers to be written in Python. Mod_python brings together the versatility of Python and the power of the Apache Web server for a considerable boost in flexibility and performance over the traditional CGI approach.
b] Apache2 webserver.
Install mod_python
Open terminal and type the following command:$ sudo apt-get update
$ sudo apt-get install libapache2-mod-python libapache2-mod-python-docConfigure mod_python
You need to create directory to host your python scripts. Type the following command:$ sudo mkdir /var/www/pyGive your account permission to access the scripts:
$ sudo chown yourname:www-data /var/www/pyNow, open /etc/apache2/sites-available/default, enter:
$ sudo -s
# vi /etc/apache2/sites-available/defaultAdd the following config code:
Save and close the file. Restart Apache2 server:
<Directory /var/www/py>
AddHandler mod_python .py
PythonHandler hello
PythonDebug On
</Directory>
# /etc/init.d/apache2 restart
# tail -f /var/log/apache2/error.logYou should see mod_python/3.3.1 Python/2.5.2 loaded:
[Tue Mar 17 02:42:18 2009] [notice] FastCGI: process manager initialized (pid 15572)
[Tue Mar 17 02:42:18 2009] [notice] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads.
[Tue Mar 17 02:42:18 2009] [notice] mod_python: using mutex_directory /tmp
[Tue Mar 17 02:42:18 2009] [notice] Apache/2.2.9 (Ubuntu) mod_fastcgi/2.4.6 PHP/5.2.6-2ubuntu4.1 with Suhosin-Patch mod_python/3.3.1 Python/2.5.2 mod_perl/2.0.4 Perl/v5.10.0 configured -- resuming normal operations
How do I test mod_python?
Simply create the script as follows:$ vi /var/www/py/hello.pyThe following simple mod_python program serves as a good test:
from mod_python import apacheSave and close the file. Now fire a webbrowser and type the url:
def handler(req):
req.log_error('handler')
req.content_type = 'text/html'
req.send_http_header()
req.write('<html><head><title>Testing mod_python</title></head><body>')
req.write('Hello World!')
req.write('</body></html>')
return apache.OK
http://localhost/py/hello.pyYou hould see "Hello World!" on screen.
No comments:
Post a Comment