How to set up Python scripting for Apache
In this post i will show you how to set up Python scripting for Apache. Note that I assume that you have already set up and configured the standard LAMP server stack. If you haven’t, refer to any of the following links, depending on your distribution. (Open Suse,Mandriva,Arch Linux, Debian,Sientific Linux)
If you have some experience with python and want to create dynamic websites with it, then look further.
Dynamic websites doesn`t serve html files stored on the server, the content is, instead, dynamically generated by a program running on the server. That is where python, php, perl, ruby etc. comes in.
As most servers are not written in those languages, an interface is needed for the server and the interpreter to communicate. The Common Gateway Interface is one such interface, which is widely supported. Another option is to install Apache’s mod_python, which puts a python interpreter within the server itself, thus removing the overhead of the interpreters startup time for each request, in lieu of using more memory.
Setting up cgi is quite simple as Apache generally installs mod_cgi by default.
1.Create a folder called “cgi-bin” in the DocumentRoot folder of the server (typically in “/var/www” or “/srv/http”).
2. In that folder, put a script or program, with executable permission set.
For more information on cgi programming and troubleshooting, look here: http://httpd.apache.org/docs/2.0/howto/cgi.html
Here is a simplistic “Hello world” program written in python:
# -*- coding: UTF-8 -*-
# enable debugging
print ("Content-Type: text/plain;charset=utf-8n")
print ("Hello World!")
Mod python, on the other hand, is touted as faster and more flexible than using the cgi.
To install mod_python in Ubuntu, install the packages “libapache2-mod-python” and “python-mysqldb”, the last one is for handling databases.
The package “mod_python” is to be installed for Fedora. It is also available in the AUR for Arch Linux.
Next, you have to set up a handler so that the server process knows how to handle python programs. The two commonly used ones are the Publisher handler and Python Server Pages, the former catering to standalone python scripts and the later allowing embedded python code within html documents.
With the default virtual hosting setting in Ubuntu, you have to add the following code in the configuration file located in “/etc/apache2/sites-available/default”
<Directory /var/www/> AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On </Directory>
for the publisher handler. The equivalent for the psp handler is mod_python.psp as a handler for .psp .
Quick introductions, on how to use them, can be found
Once you are familiar with the above it is a good idea to install a Web framework like Django, which abstracts away many of the boilerplate tasks needed for a good Web Application.
Like us on Facebook
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- Setup DNS Server On openSUSE 13.1 : What is DNS Server? As you might know, DNS, Domain Name System, is a system that resolves Domain ...0 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint : Tor, The Onion Router, is a network of Virtual Tunnels that allows users to communicate securely and...1 comment |
- Twelve Vulnerabilities Have Been Fixed In Ubuntu 12.04, Time To Update : Canonical has released security updates for Ubuntu 12.04 LTS and advices to update your system as so...1 comment |
- Setup DNS Server On openSUSE 13.1
- TeamViewer 9 Final Has Been Released!
- Twelve Vulnerabilities Have Been Fixed In Ubuntu 12.04, Time To Update
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
- Selene Media Encoder: Convert Audio, Video Files To Most Popular Formats
- Linux Kernel 3.12.2 Is Available For Download! Install / Upgrade Instructions
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .