Integrating Python with Apache

Installing the software

Before we install any packages we should ensure that the correct use-flags are configured so that all required functionality is made available and unnecessary functionality is not included. The www-apache/mod_python package and its dependencies provide a variety of use-flags only some of which will be discussed further here. As usual feel free to add and remove use-flags at will although the minimum set which are required for using this guide in its entirety are shown below.

lisa emerge -pv mod_python
 
These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild      ] www-apache/mod_python-3.3.1-r1

Once you are confident that the correct use-flags are set for the www-apache/mod_python package, and any dependencies it may require, you can proceed with the installation by issuing the emerge command shown below.

lisa emerge mod_python

Integrating Python with Apache

When the Python application has been successfully installed support for scripts written in the Python language can be easily added to an existing Apache installation by modifying the main configuration file as shown below.

/etc/conf.d/apache2
APACHE2_OPTS="-D DEFAULT_VHOST -D SSL -D SSL_DEFAULT_VHOST"
APACHE2_OPTS="-D DEFAULT_VHOST -D SSL -D SSL_DEFAULT_VHOST -D PYTHON"

Although the Python application has been connected to the apache2 daemon with the above configuration entry no default handlers are specified so some will either need to be specified globally in the /etc/apache2/modules.d/16_mod_python.conf configuration file or specified locally on a per-host basis as shown below.

/etc/apache2/vhosts.d/default_vhost.include
<Directory /var/www/hacking/website>
AddHandler python-program .py
PythonHandler mod_python.publisher
</Directory>

As always when installing a new module the apache2 daemon will require restarting for the changes to take effect. This can be easily accomplished with the command shown below.

lisa /etc/init.d/apache2 restart