MoinMoin Wiki

Last modified by Sebastian Marsching on 2022/04/03 22:10

Running MoinMoin with Apache’s mod_proxy_fcgi

While MoinMoin works great with mod_fcgid, it does not work with mod_proxy_fcgi out of the box. Luckily, adapting it for mod_proxy_fcgi is not very hard. The moin.cgi script included in the MoinMoin distribution is a good starting point. The following script has been derived from the version of this script that is included in MoinMoin 1.9.8.

#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - CGI/FCGI Driver script

    @copyright: 2000-2005 by Juergen Hermann <jh@web.de>,
                2008 by MoinMoin:ThomasWaldmann,
                2008 by MoinMoin:FlorianKrupicka
                2017 by Sebastian Marsching
    @license: GNU GPL, see COPYING for details.
"""


import sys, os

# a) Configuration of Python's code search path
#    If you already have set up the PYTHONPATH environment variable for the
#    stuff you see below, you don't need to do a1) and a2).

# a1) Path of the directory where the MoinMoin code package is located.
#     Needed if you installed with --prefix=PREFIX or you didn't use setup.py.
#sys.path.insert(0, 'PREFIX/lib/python2.3/site-packages')

# a2) Path of the directory where wikiconfig.py / farmconfig.py is located.
#     See wiki/config/... for some sample config files.
#sys.path.insert(0, '/etc/moin')

# b) Configuration of moin's logging
#    If you have set up MOINLOGGINGCONF environment variable, you don't need this!
#    You also don't need this if you are happy with the builtin defaults.
#    See wiki/config/logging/... for some sample config files.
#from MoinMoin import log
#log.load_config('/path/to/logging_configuration_file')

# Is fixing the script name and path info needed?
# Use '' if your url looks like http://domain/
# Use '/mywiki' if your url looks like  http://domain/mywiki/
url_prefix = '/mywiki'  # <-- adapt here as needed

from MoinMoin.web.flup_frontend import CGIFrontEnd

class PathFixingCGIFrontEnd(CGIFrontEnd):
   def run_server(self, application, options):
       from MoinMoin.wikiutil import url_unquote
       def prefix_fixer(env, start):
           if 'PATH_INFO' not in env:
               return
            path_info = env['PATH_INFO']
           if path_info.startswith(url_prefix):
                path_info = path_info[len(url_prefix):]
            path_info = url_unquote(path_info).encode('utf-8')
            env['PATH_INFO'] = path_info
           if 'SCRIPT_NAME' in env:
                env['SCRIPT_NAME'] = url_prefix
           # Neither REQUEST_URI nor SCRIPT_FILENAME seem to be used.
           return application(env, start)
       super(PathFixingCGIFrontEnd, self).run_server(prefix_fixer, options)

PathFixingCGIFrontEnd().run()

If this script is placed in the same directory as wikiconfig.py, you should not have to change any paths. Otherwise, you have to add a sys.path.insert(...) line (see the corresponding commented section in the script).

Assuming that you saved the script as moin-fcgi-server, you can run start the server using one of the following lines:

moin-fcgi-server -i 127.0.0.1 -p 8999
moin-fcgi-server -i /path/to/fcgi.sock

The first variant makes the server listen on TCP port 8991 on the loopback interface. Assuming that the wiki is available at http://example.com/mywiki/, the following line should be added to the respective VirtualHost section of the Apache configuration:

ProxyPass "/mywiki" "fcgi://localhost:8999"

When using the second variant, the server listens on a UNIX domain socket and the Apache configuration is slightly different:

ProxyPass "/mywiki" "unix:/path/to/fcgi.sock|fcgi://localhost"

The flup FastCGI server resets the permissions of the socket to match the umask of the process, so permission or ownership changes of the socket are not preserved across restarts. Unfortunately, there is no way to change this without making changes to the MoinMoin code. For this reason, using Unix domain sockets is only feasible when the MoinMoin FastCGI server is running as the same user as the Apache webserver.