Htaccess

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”):

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”):

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:

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

Install mod_python

Set up a wiki instance

Edit wikiconfig.py

Changes to Apache httpd.conf

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:-

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.

Copy moin.cgi into your wiki directory

Configure httpd.conf as cgi first (the shown Alias is for moin version 1.6.0):

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:

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):-

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 athttp://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:

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.).

Environment Access

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.