[mod_python] running multiple mod_python apps under one apache

Jorey Bump list at joreybump.com
Tue Jul 19 14:28:44 EDT 2005


Kevin J. Smith wrote:

> I have looked into the linux vserver project but I thought I saw on this 
> list a more contained approach.  What are folk's experience with hosting 
> multiple mod_python applications on the same box or virtual hosting of 
> mod_python.

I use mod_python.publisher in a standard name-based virtual host 
environment. My preferred setup is to use the SetHandler directive on a 
single subdirectory within the DocumentRoot (you can also use an Alias 
directory if you prefer the separation), and prefix the PythonPath with 
another directory outside of the DocumentRoot where I store my custom 
modules and packages. I also normally create a data directory outside of 
the DocumentRoot that is writable by the apache user to create, store, 
or manipulate files that should not be accessible by browsers:

/var/www/vhost/example.com/
   - python/
   - data/
   - website/
       - apps/

With this approach, you are less likely to create namespace collisions 
and have an infrastructure that lends itself to better security. Setting 
aside a specific directory for your mod_python apps may resemble CGI, 
but you still get the performance boost of the embedded interpreter. In 
a virtual host container, you would configure mod_python this way:

PythonPath "['/var/www/vhosts/example.com/python'] + sys.path"
<Directory /var/www/vhosts/example.com/website/apps>
    SetHandler python-program
    PythonHandler mod_python.publisher
    PythonDebug On
</Directory>

If you want to scatter your apps around your site, you could use 
AddHandler for your entire DocumentRoot directory, but this requires you 
to use the .py extension in the URL. One trick I have used involves 
using SetHandler in a FilesMatch directive and "registering" each 
*uniquely* named application:

PythonPath "['/var/www/vhost/example.com/python'] + sys.path"
<Directory /var/www/vhost/example.com/website>
   # use FilesMatch to register mod_python applications
   # these are regex strings, so no other file should contain them!!!
   <FilesMatch "whackamole|tron|mpwebmail">
     SetHandler python-program
     PythonHandler mod_python.publisher
     PythonDebug On
   </FilesMatch>
</Directory>

It must be strongly noted that no other resource of any type can share 
the regex strings used in the FilesMatch directive, so the names must be 
sufficiently unique (electronic.html would match the tron entry in this 
example, and get processed by mod_python). While it's tempting to list 
the entire module name (whackamole.py), you'll lose the ability to drop 
the .py extension in URLs. In a variation on this scheme, you invent a 
sufficiently unique string to include only in the names of your 
mod_python apps (xyz in xyzapp.py, appxyz.py, etc.). As an overall 
strategy, this leaves much to be desired, but it does demonstrate that 
finetuning is possible. I *rarely* adopt this technique.

In a name-based virtual hosting environment, a new interpreter is 
created for each virtual host. Therefore, it's best to define the 
PythonPath directive *once* for each virtual host. One virtual host's 
PythonPath will not clobber another's, however, so the structure I 
outlined earlier provides fairly good support for mod_python in a 
name-based virtual host environment.

The remaining issue is the need to restart apache after editing. There 
are no solutions to this as of yet in mod_python.publisher, but an 
alternative is to touch all imported modules. Since that isn't normally 
practical, either, it's very useful to have a staging environment and 
transfer your well-tested code to the production server when it is ready.





More information about the Mod_python mailing list