Embedding Python In Apache2 With mod_python (Debian Etch)
https://www.howtoforge.com/embedding-python-in-apache2-with-mod_python-debian-etch
Author: Falko Timme
Author: Falko Timme
This tutorial shows how to install and use mod_python on a Debian Etch server with Apache2. mod_python is an Apache module that embeds the Python interpreter within the server. It allows you to 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.
I do not issue any guarantee that this will work for you!
1 Preliminary Note
I have tested this on a Debian Etch server with the IP address 192.168.0.100 where Apache2 is already installed.
I'm using a virtual host with the document root /var/www in this example.
2 Installing mod_python
To install mod_python, we simply run:
apt-get install libapache2-mod-python
3 Configuring Apache
Now we must configure Apache so that it can handle Python files. There are two ways of doing so. The first (and default) one is to use the Publisher Handler. It allows you to write pure Python scripts with the extension .py that will be interpreted by Apache. The second way is the PSP Handler. PSP stands for Python Server Pages. It allows you to embed Python code directly in HTML code, similar to PHP. PSP files have the extension .psp.
3.1 The Publisher Handler
To enable the Publisher Handler, we must open our vhost configuration (I'm using the default vhost on Debian with the document root /var/www; the configuration for this vhost is located in /etc/apache2/sites-available/default) and add the lines AddHandler mod_python .py, PythonHandler mod_python.publisher, and PythonDebug On to it:
vi /etc/apache2/sites-available/default
[...] <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On </Directory> [...] |
Restart Apache afterwards:
/etc/init.d/apache2 restart
Now we create a little Python test script (e.g. /var/www/test.py) with pure Python code in it...
vi /var/www/test.py
def index(req): return "Test successful"; |
... and call it in a browser (e.g. http://192.168.0.100/test.py). If all goes well, it should display Test successfulin your browser.
3.2 The PSP Handler
To enable the Publisher Handler, we must open our vhost configuration (I'm using the default vhost on Debian with the document root /var/www; the configuration for this vhost is located in /etc/apache2/sites-available/default) and add the lines AddHandler mod_python .psp, PythonHandler mod_python.psp, and PythonDebug On to it:
vi /etc/apache2/sites-available/default
[...] <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all AddHandler mod_python .psp PythonHandler mod_python.psp PythonDebug On </Directory> [...] |
Restart Apache afterwards:
/etc/init.d/apache2 restart
Now we create a little PSP test script (e.g. /var/www/test.psp) with HTML and Python code in it...
vi /var/www/test.psp
<html> <body> <h1><% req.write("Hello!") %></h1> </body> </html> |
... and call it in a browser (e.g. http://192.168.0.100/test.psp). If all goes well, it should display Hello! in your browser.
4 Python Modules
If you need further Python modules, you can search for them like this:
apt-cache search python
Pick the ones you need and install them as follows:
apt-get install python-mysqldb python-xml
Restart Apache afterwards:
/etc/init.d/apache2 restart
Tidak ada komentar:
Posting Komentar