Python working on Apache

This page gives some nice but advanced tricks for a moin Apache setup with php and .htaccess.  The directives
on this page assume that you have knowledge about Apache configuration, newbies should
stick to the basic setup. This requires the Apache module “mod_rewrite” for rewriting  (which should be standard).

mod_python is an Apache module that embeds the Python interpreter within the server. With mod_python you can 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.

Adding permission to serve the htdocs directory

In some (sane) Linux distributions (like SuSE 9.0) serving directories other than the
document-root “/srv/www/htdocs” with Apache is switched off by default for
security reasons in “/etc/httpd/httpd.conf” (or for Apache2 “/etc/apache2/httpd.conf”):

# forbid access to the entire filesystem by default
<Directory />
Options None
AllowOverride None
Order deny,allow
Deny from all
</Directory>

To allow Apache to serve directories outside of the document root you have to add these
lines to “/etc/httpd/httpd.conf” (in SuSE it is recommended to create a new “http.conf.local” and
include this file in “/etc/sysconfig/apache2”):

Alias /wiki/ "/usr/share/moin/htdocs/"
<Directory "/usr/share/moin/htdocs/">
Order deny,allow
Allow from all
</Directory>
ScriptAlias /mywiki "/usr/share/moin/mywiki/cgi-bin/moin.cgi"
<Directory "/usr/share/moin/mywiki/cgi-bin">
Order deny,allow
Allow from all
</Directory>

Voodoo is not for newbies!

An Apache root wiki is easy, but it has limitations. Since moin needs to access certain
static files (images, css, etc.), it intercepts a part of the possible WikiName namespace.

  1. Install moin as normal.

Add the following lines to your VirtualHost config:

RewriteEngine On
RewriteLogLevel 0

# Point to moin shared static files - DEPENDS ON MOIN VERSION!
RewriteRule ^/moin_static160/(.*)$ /prefix/share/moin/htdocs/$1 [last]

# Map everything else to moin cgi script
RewriteRule ^(.*)$ /path/to/moin.cgi$1 [type=application/x-httpd-cgi]

# Setting for FastCGI
##RewriteRule ^(.*)$ /path/to/moin.fcg$1 [type=application/x-httpd-fcgi]

The RewriteRule for FastCGI here does not work for me:

[Wed Jan 05 01:43:41 2005] [error] [client 10.0.0.1] File does not exist: /home/apache/moin/wiki/share/moin/pw/moin.fcg/RecentChanges, referer: http://moin.dahoam/StartSeite
duplo:/home/danielt# ls /home/apache/moin/wiki/share/moin/pw/moin.fcg
-rwxr-xr-x  1 root  www  1088 Jan  4 23:35 /home/apache/moin/wiki/share/moin/pw/moin.fcg*

As a workaround I use this:

RewriteRule ^/?(.*) /_Wikiscript_/moin.fcg/$1
RewriteRule ^/_Wikiscript_/(.*) /home/apache/moin/wiki/share/moin/pw/$1 [last]

Root wiki on Mac OSX

This configuration intercepts all userfolder URLs like http://127.0.0.1/~yourname/ and
offers a blank wiki page instead, so these instructions are offered as an exercise only.
There may be a way around this problem but I don’t know what it is.

Using the previous information on the moin OS X installation, you end up with a virtual
host block like this:

<VirtualHost *>
ServerName Gnarlodious
Alias /moin_static160/ /usr/local/share/moin/htdocs/
ScriptAlias /mywiki /usr/local/share/moin/mywiki/moin.cgi
RewriteEngine On
RewriteRule ^/moin_static160/(.*)$ /usr/local/share/moin/htdocs/$1 [last]
RewriteRule ^(.*)$ /usr/local/share/moin/mywiki/moin.cgi$1 [type=application/x-httpd-cgi]
</VirtualHost>

Alias /moin_static160/ "/moinwiki/share/moin/htdocs/"
ScriptAlias /mywiki "/moinwiki/share/moin/mywiki/moin.cgi"

Root wiki with mod python recipe

Using the above example, I got this working like this:

  1. Install moin as usual and create instance
  2. Use this config in httpd.conf
NameVirtualHost 192.115.134.51:80
<VirtualHost 192.115.134.51:80>
ServerName wiki.nirs.dyndns.org
ServerAlias wiki

# Rewrite urls
RewriteEngine On
RewriteLogLevel 0
# map static files to htdocs
RewriteRule ^/moin_static160/(.*)$ /usr/share/moin/htdocs/$1 [last]
# map everything else to server script
RewriteRule ^(.*)$ /usr/share/moin/mywiki/moinmodpy.py$1

<Directory "/usr/share/moin/mywiki">
# These are copied from the default cgi-bin directory
AllowOverride None
Options None
Order allow,deny
Allow from all

# Modpy stuff
AddHandler python-program .py
# Add the path to the wiki directory, where moinmodpy.py and
# wikiconfig.py are located.
PythonPath "['/usr/share/moin/mywiki'] + sys.path"
PythonHandler moinmodpy
PythonDebug On
</Directory>
</VirtualHost>

mod_python embeds the python interpreter into
the apache server. This saves initialization time and the need of forking cgi scripts.
It doesn’t have the ability to run as different users. It will always run as the main
apache user and group. Be sure that your wiki data files are accessible and writable
by your apache server.

The basic configuration is suitable for mod_python 3.1.3 and later. If you use older
version, see the section “Older mod_python versions”

mod_python will cause your apache processes to increase their memory requirements considerably
– especially as apache runs many separate processes which will each need to have their
own copy of the python code and data in the process memory space. You may find that FastCGI,
as detailed in HelpOnInstalling/FastCgi is rather more efficient in this respect.

Basic configuring

  1. Install mod_python
  2. Set up a wiki instance
  3. Edit wikiconfig.py
  4. Changes to Apache httpd.conf
  5. Restart Apache

The sample configurations below are for a wiki instance called mywiki installed in a
directory /var/www/moin/mywiki with the main MoinMoin installation installed in python’s
default site library path. The wiki appears as URL /mywiki under the server – ie http://my.ser.ver/mywiki.
You will need to change these to reflect your installation.

Install mod_python

Most people will just add a mod_python package to their current operating system installation.
If you are building from source then you should consult the mod_python documentation.

The mod_python installation should have added some lines to the Apache configuration
file – either in the file itself or in an included configuration file (for example on
Red Hat or Fedora linux the mod_python configuration is in /etc/httpd/conf.d/python.conf).

Make sure you have this line in httpd.conf or mod_python will not work:

LoadModule python_module modules/mod_python.so

After this restart Apache and make sure that it starts successfully, and that the error
log has a line similar to this:-

[Sat Jan 01 15:40:49 2005] [notice] mod_python: Creating 4 session mutexes based on 150
max processes and 0 max threads.

You may need to change some environment variables on (eg) FreeBSD – this is detailed
in the port installation message.

Set up a wiki instance

This is done as shown in WikiInstanceCreation. Its recommended to first configure the
wiki with cgi and check that it works, then change the configuratin to use mod_python.
This allows you be sure that any problems are in the mod_python transition rather than
the basic MoinMoin installation.

  1. Copy moin.cgi into your wiki directory
  2. Configure httpd.conf as cgi first (the shown Alias is for moin version 1.6.0):
    • Alias /moin_static160/ "/usr/share/moin/htdocs/"
      ScriptAlias /mywiki "/var/www/moin/mywiki/moin.cgi"

Changes to Apache `httpd.conf`

After your wiki is running as cgi script, convert it to run with mod_python.

If you run your wiki as cgi as we recommended before, remove or comment the ScriptAlias
directive:

#ScriptAlias /mywiki "/var/www/moin/mywiki/moin.cgi"
Add a Location directive:
<Location /mywiki>
SetHandler python-program     # Add the path of your wiki directory
PythonPath "['/var/www/moin/mywiki'] + sys.path"
PythonHandler MoinMoin.request.request_modpython::Request.run
</Location>

If you have multiple MoinMoin instances then add one location directive for each one
(changing the paths as appropriate) and add a line with the directive PythonInterpreter
mywiki to each Location section. With this directive different subinterpreters with completely
separate namespaces will be used for each wiki (see here for details).

If you did not install MoinMoin in the default location, you will have to add the path
to MoinMoin to the system path:

PythonPath "['/var/www/moin/mywiki', '/prefix/lib/python2.x/site-packages'] + sys.path"

Solving problems for non-root-mounted wikis

On some installations, mod_python hands MoinMoin incorrect script_name and path_info.
It usually happens when using the Apache Location directive, with a wiki in an arbitary
path:

<Location /farm/mywiki> … </Location>

This will not work, because its not clear what is the script name, since with location
setup, there is no real script.

To solve this problem, use a PythonOption directive:

<Location /farm/mywiki># Location value must match the Apache Location value!
PythonOption Location /farm/mywiki ... 

</Location>

Configuring root wiki

You may wish to have your wiki appearing at the root of your webserver – for example
so that http://wiki.example.com/ will map to your wiki rather than having to use http://wiki.example.com/mywiki/.
This requires a slightly different form of configuration using mod_rewrite – this is
a standard module of recent Apache distributions, and is often enabled by default.

You need to set up your wiki instance as described above, and also copy moinmodpy.py
from the Moin installation directory to the wiki instance directory (/var/www/moin/mywiki
in these examples).

The Apache configuration needs mod_rewrite enabled – so the line

LoadModule rewrite_module modules/mod_rewrite.so

should appear in the first part of the httpd.conf configuration file.

The wiki and virtual host configuration should look like this (Alias is for moin version
1.6.0):-

<VirtualHost *:80>
ServerAdmin postmaster@example.com
DocumentRoot /var/www/html
ServerName wiki.example.com
Alias /moin_static160/ "/usr/share/moin/htdocs/"

# Rewrite urls
RewriteEngine On
RewriteLogLevel 0
# map /wiki static files to Moin htdocs
RewriteRule ^/moin_static160/(.*)$ /usr/share/moin/htdocs/$1 [last]
RewriteRule ^/robots.txt$ /usr/share/moin/htdocs/robots.txt [last]
RewriteRule ^/favicon.ico$ /usr/share/moin/htdocs/favicon.ico [last]
# map everything else to server script
RewriteRule ^(.*)$ /var/www/moin/mywiki/moinmodpy.py$1

<Directory "/var/www/moin/testwiki">
# Modpy stuff
AddHandler python-program .py
# Add the path to the wiki directory, where
# moinmodpy.py and wikiconfig.py are located.
PythonPath "['/var/www/moin/mywiki'] + sys.path"
PythonHandler MoinMoin.request.request_modpython::Request.run
</Directory>
</VirtualHost>

Integrating with Apache 2

Standalone or Twisted setup

If you want to use Twisted, edit mointwisted.py and set these options: port = 8000 interfaces
= [‘localhost’] If you want to use Standalone, edit standalone.py and set these options:
port = 8000 interface = ‘localhost’ Now run your moin server and check that it works at
http://localhost:8000 .

Apache 2 setup

This setup requires mod_proxy and mod_headers. Make sure they are installed and loaded
before you continue. Edit your Apache configuration file, either httpd.conf or an included
configuration file:

# Can be off for reverse proxying, more secure
ProxyRequests Off

Alias /moin_static160/ "/usr/share/moin/htdocs/"

<Location /exwiki/>
    ProxyPass http://localhost:8000/
    ProxyPassReverse http://localhost:8000/
    RequestHeader set X-Moin-Location /exwiki/
</Location>

Integrating with Apache 1

Standalone or Twisted setup

If you want to use Twisted, edit mointwisted.py and set these options:

properties = {'script_name': '/mywiki', 'http_host': 'yourdomain'}
port = 8000
interfaces = ['localhost']

If you want to use Standalone, edit standalone.py and set these options:

properties = {'script_name': '/mywiki', 'http_host': 'yourdomain'}
port = 8000
interface = 'localhost'
  • Another solution may be to use url_mapping config option.

Apache 1 setup

This setup requires mod_proxy. Make sure it’s installed and loaded before you continue.

Edit your Apache configuration file, either httpd.conf or an included configuration
file:

# Can be off for reverse proxying, more secure
ProxyRequests Off

Alias /moin_static160/ "/usr/share/moin/htdocs/"

<Location /exwiki>
ProxyPass http://localhost:8000/
ProxyPassReverse http://localhost:8000/
</Location>

Now restart Apache and test your wiki at http://www.example.com/exwiki/

Detailed Information

  1. HelpOnConfiguration/ApacheVoodoo
  2. HelpOnConfiguration/IntegratingWithApache
  3. HelpOnInstalling/ApacheOnLinux
  4. HelpOnInstalling/ApacheOnLinuxFtp (explore.py)
  5. HelpOnInstalling/ApacheOnLinuxFtp
  6. HelpOnInstalling/ApacheOnMacOsx
  7. HelpOnInstalling/ApacheOnWin32
  8. HelpOnInstalling/ApacheOnWin32withDomainAuthentication
  9. HelpOnInstalling/ApacheOnWin32withFastCgi
  10. HelpOnInstalling/ApacheWithModPython
  11. Modpython Tutorial – An introduction to modpython’s high level handlers: Publisher and PSP.

Mod_Python

Deployment Platforms

Linux and Windows. RPMs available.

Suitability

mod_python can be used to develop fast and small web tools and applications, and as part of larger custom application frameworks. It provides an object publishing mechanism similar to (but much simpler than) Zope, and a very clean mapping from URLs to functions. This makes it especially suitable for building web-accessible libraries using HTTP GET/POST as function call serialization.

ModPython Compatible Web Application Frameworks

  • ModPython now has built-in support for cookies and sessions, PSP templates, etc., and a CGI handler for compatibility with regular CGI python scripts.

Web application frameworks that support multiple kinds of deployments including ModPython:

  • WebStack aims to provide a common API regardless of the underlying server or framework environment (be it ModPython, CGI, Webware, etc.).

  • Quixote is a web application framework that has a ModPython handler.

  • Albatross applications can be deployed to ModPython or CGI.

  • Karrigell is meant primarily for running small standalone web applications, but now has a ModPython handler as well.

  • Aquarium is a MVC web application framework using Cheetah for templating with some resemblance to Struts and a ModPython WSAdaptor.

Frameworks designed specifically for ModPython:

  • Pylets is a new web framework with aspect-oriented MetaClasses.

  • PyWork is a Model-View-Controller framework with similarities to Struts.

  • JOTWeb also separates the view and the model in part by using TAL templates.

  • Django runs in stand-alone mode for development purpose, but relies on ModPython for production.

Environment Access

mod_python provides a similar API to mod_perl, allowing low-level access to Apache’s request handling mechanisms.

Session, Identification and Authentication

mod_python 3.1.3 and later provides support for server-side sessions with memory or dbm-based storage. It also supports session locking. For earlier versions, JonsPythonModules implement session management as well as templating and database pooling. mod_python has a very straightforward and flexible system for basic authentication, allowing access rules to be configured at the module and function level.

Persistence Support

Scripts remain in memory between page loads. The Python interpreter can be configured to run per-site or per-directory, allowing the scope of the persistence to be limited as necessary.

Advertisements

One thought on “Python working on Apache

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s