Embedding Python In Apache2 With mod_python (Debian/Ubuntu, Fedora/CentOS, Mandriva, OpenSUSE)
|
Submitted by falko (Contact Author) (Forums) on Sun, 2008-07-20 17:46. :: CentOS | Debian | Fedora | Mandriva | SuSE | Ubuntu | Apache
Embedding Python In Apache2 With mod_python (Debian/Ubuntu, Fedora/CentOS, Mandriva, OpenSUSE)Version 1.0 This tutorial shows how to install and use mod_python on various distributions (Debian/Ubuntu, Fedora/CentOS, Mandriva, OpenSUSE) 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 NoteI have tested this on systems with the IP address 192.168.0.100 where Apache2 is already installed.
2 Debian/UbuntuFor Debian/Ubuntu, please refer to this tutorial: Embedding Python In Apache2 With mod_python (Debian Etch).
For ISPConfig UsersIn that tutorial I enable mod_python in the default vhost which has no effect on other vhosts, so if you are using ISPConfig (from version 2.2.24 on) you can use that setup without any changes and still enable/disable mod_python on a per-website basis. ISPConfig uses the Publisher Handler - it does not support the PSP Handler.
3 Fedora 9/CentOS 5.23.1 Installing mod_pythonTo install mod_python, we simply run: yum install mod_python
3.2 Configuring ApacheNow 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.2.1 The Publisher HandlerTo enable the Publisher Handler, we must edit the mod_python configuration which you can find in /etc/httpd/conf.d/python.conf. This file contains many examples - we make a backup of it and create that file again from scratch. I'm using the default Fedora/CentOS document root /var/www/html here in the <Directory> directive - adjust this to your needs. The important lines are AddHandler mod_python .py and PythonHandler mod_python.publisher: cp /etc/httpd/conf.d/python.conf /etc/httpd/conf.d/python.conf_orig
Please note: if you use ISPConfig (from version 2.2.24 on) on the server, please leave out the <Directory ...>...</Directory> part in the above file since that would enable mod_python globally for the directory in question. In ISPConfig you can enable mod_python on a per-website basis instead which gives you more control whether a website can use mod_python or not. Restart Apache afterwards: /etc/init.d/httpd restart Now we create a little Python test script (e.g. /var/www/html/test.py) with pure Python code in it... vi /var/www/html/test.py
... and call it in a browser (e.g. http://192.168.0.100/test.py). If all goes well, it should display Test successful in your browser.
3.2.2 The PSP HandlerTo enable the PSP Handler, we must edit the mod_python configuration which you can find in /etc/httpd/conf.d/python.conf. This file contains many examples - we make a backup of it and create that file again from scratch. I'm using the default Fedora/CentOS document root /var/www/html here in the <Directory> directive - adjust this to your needs. The important lines are AddHandler mod_python .py and PythonHandler mod_python.psp: cp /etc/httpd/conf.d/python.conf /etc/httpd/conf.d/python.conf_orig
Please note: if you use ISPConfig (from version 2.2.24 on) on the server, please leave out the <Directory ...>...</Directory> part in the above file since that would enable mod_python globally for the directory in question. In ISPConfig you can enable mod_python on a per-website basis instead which gives you more control whether a website can use mod_python or not. Also note that ISPConfig does not support the PSP Handler - it uses the Publisher Handler. Restart Apache afterwards: /etc/init.d/httpd restart Now we create a little PSP test script (e.g. /var/www/html/test.psp) with HTML and Python code in it... vi /var/www/html/test.psp
... 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.
|





print: 
Recent comments
15 hours 17 min ago
18 hours 53 min ago
2 days 8 hours ago
3 days 2 hours ago
3 days 2 hours ago
3 days 5 hours ago
5 days 14 hours ago
5 days 18 hours ago
6 days 4 hours ago
6 days 4 hours ago