Trac and mod_python

Mod_python is an ​Apache module that embeds the Python interpreter within the server, so that web-based applications in Python will run many times faster than traditional CGI and will have the ability to retain database connections.
Trac supports ​mod_python, which speeds up Trac's response times considerably, especially compared to CGI, and permits use of many Apache features not possible with tracd/mod_proxy.

Simple configuration: single project

If you just installed mod_python, you may have to add a line to load the module in the Apache configuration:

LoadModule python_module modules/mod_python.so

Note: The exact path to the module depends on how the HTTPD installation is laid out.

On Debian using apt-get:

apt-get install libapache2-mod-python libapache2-mod-python-doc

Still on Debian, after you have installed mod_python, you must enable the modules in apache2, equivalent to the above Load Module directive:

a2enmod python

On Fedora use, using yum:

yum install mod_python

You can test your mod_python installation by adding the following to your httpd.conf. You should remove this when you are done testing for security reasons. Note: mod_python.testhandler is only available in mod_python 3.2+.

The option TracUriRoot may or may not be necessary in your setup. Try your configuration without it; if the URLs produced by Trac look wrong, if Trac does not seem to recognize URLs correctly, or you get an odd "No handler matched request to..." error, add the TracUriRoot option. You will notice that the Location and TracUriRoot have the same path.

The options available are:

# For a single projectPythonOption TracEnv /var/trac/myproject# For multiple projectsPythonOption TracEnvParentDir /var/trac/myprojects# For the index of multiple projectsPythonOption TracEnvIndexTemplate /srv/www/htdocs/trac/project_list_template.html# A space delimitted list, with a "," between key and value pairs.PythonOption TracTemplateVars key1,val1 key2,val2
# Useful to get the date in the wanted orderPythonOption TracLocale en_GB.UTF8
# See description above PythonOption TracUriRoot /projects/myproject

Python Egg Cache

Compressed Python eggs like Genshi are normally extracted into a directory named .python-eggs in the users home directory. Since Apache's home usually is not writeable, an alternate egg cache directory can be specified like this:

PythonOption PYTHON_EGG_CACHE /var/trac/myprojects/egg-cache

Or you can uncompress the Genshi egg to resolve problems extracting from it.

Configuring Authentication

Advanced Configuration

Setting the Python Egg Cache

If the Egg Cache isn't writeable by your Web server, you'll either have to change the permissions, or point Python to a location where Apache can write. This can manifest itself as a 500 internal server error and/or a complaint in the syslog.

When you request the /projects URL, you will get a listing of all subdirectories of the directory you set as TracEnvParentDir that look like Trac environment directories. Selecting any project in the list will bring you to the corresponding Trac environment.

If you don't want to have the subdirectory listing as your projects home page you can use a

<LocationMatch"/.+/">

This will instruct Apache to use mod_python for all locations different from root while having the possibility of placing a custom home page for root in your DocumentRoot folder.

You can also use the same authentication realm for all of the projects using a <LocationMatch> directive:

This does not seem to work in all cases. What you can do if it does not:

Try using <LocationMatch> instead of <Location>.

<Location /> may, in your server setup, refer to the complete host instead of simple the root of the server. This means that everything (including the login directory referenced below) will be sent to Python and authentication does not work, ie you get the infamous Authentication information missing error. If this is the case, try using a sub-directory for Trac instead of the root, ie /web/ and /web/login instead of / and /login.

Depending on apache's NameVirtualHost configuration, you may need to use <VirtualHost *:80> instead of <VirtualHost *>.

Note: DocumentRoot should not point to your Trac project env. As Asmodai wrote on #trac: "suppose there's a webserver bug that allows disclosure of DocumentRoot they could then leech the entire Trac environment".

Troubleshooting

If you get server error pages, you can either check the Apache error log, or enable the PythonDebug option:

<Location/projects/myproject>
...
PythonDebugon</Location>

For multiple projects, try restarting the server as well.

Login Not Working

If you've used <Location /> directive, it will override any other directives, as well as <Location /login>.
The workaround is to use negation expression as follows (for multi project setups):

Expat-related segmentation faults

This problem will most certainly hit you on Unix when using Python 2.4.
In Python 2.4, some version of ​Expat (an XML parser library written in C) is used and if Apache is using another version, this results in segmentation faults.
As Trac 0.11 is using Genshi, which will indirectly use Expat, that problem can now hit you even if everything was working fine before with Trac 0.10. This problem has not been reported for Python 2.5+, so best to upgrade.

Form submission problems

If you're experiencing problems submitting some of the forms in Trac (a common problem is that you get redirected to the start page after submission), check whether your DocumentRoot contains a folder or file with the same path that you mapped the mod_python handler to. For some reason, mod_python gets confused when it is mapped to a location that also matches a static resource.

Problem with virtual host configuration

If the <Location /> directive is used, setting the DocumentRoot may result in a 403 (Forbidden) error. Either remove the DocumentRoot directive, or make sure that accessing the directory it points is allowed, in a corresponding <Directory> block.

Using <Location /> together with SetHandler resulted in having everything handled by mod_python, which leads to not being able to download any CSS or images/icons. Use <Location /trac> SetHandler None </Location> to circumvent the problem, though this may not be the most elegant solution.

Problem with zipped egg

It's possible that your version of mod_python will not import modules from zipped eggs. If you encounter an ImportError: No module named trac in your Apache logs but you think everything is where it should be, this might be your problem. Look in your site-packages directory; if the Trac module appears as a file rather than a directory, then this might be your problem. To rectify this, try installing Trac using the --always-unzip option:

easy_install --always-unzip Trac-0.12b1.zip

Using .htaccess

Although it may seem trivial to rewrite the above configuration as a directory in your document root with a .htaccess file, this does not work. Apache will append a "/" to any Trac URLs, which interferes with its correct operation.

It may be possible to work around this with mod_rewrite, but I failed to get this working. In all, it is more hassle than it is worth.

FreeBSD issues

The FreeBSD ports have both the new and old versions of mod_python and SQLite, but earlier versions of pysqlite and mod_python won't integrate:

pysqlite requires threaded support in Python

mod_python requires a threadless install.

Apache2 does not automatically support threads on FreeBSD. You could force thread support when running ./configure for Apache, using --enable-threads, but this isn´t recommended.
The best option ​seems to be adding to /usr/local/apache2/bin/ennvars the line:

export LD_PRELOAD=/usr/lib/libc_r.so

Fedora 7 Issues

Make sure you install the 'python-sqlite2' package as it seems to be required for TracModPython, but not for tracd.

Subversion issues

If you get the following Trac error Unsupported version control system "svn" only under mod_python, though it works well on the command-line and even with TracStandalone, chances are that you forgot to add the path to the Python bindings with the PythonPath directive. A better way is to add a link to the bindings in the Python site-packages directory, or create a .pth file in that directory.

If this is not the case, it's possible that you are using Subversion libraries that are binary incompatible with the Apache ones and an incompatibility of the apr libraries is usually the cause. In that case, you also won't be able to use the svn modules for Apache (mod_dav_svn).

You also need a recent version of mod_python in order to avoid a runtime error (argument number 2: a 'apr_pool_t *' is expected) due to the default usage of multiple sub-interpreters. Version 3.2.8 should work, though it's probably better to use the workaround described in ​#3371, in order to force the use of the main interpreter:

PythonInterpreter main_interpreter

This is also the recommended workaround for other issues seen when using the Python bindings for Subversion within mod_python (​#2611, ​#3455). See in particular Graham Dumpleton's comment in ​#3455 explaining the issue.

Page layout issues

If the formatting of the Trac pages look weird, chances are that the style sheets governing the page layout are not handled properly by the web server. Try adding the following lines to your Apache configuration:

Note: For the above configuration to have any effect it must be put after the configuration of your project root location, ie <Location /myproject />.

Note: Do not enable python optimizations using the directive PythonOptimize On. When optimizations are enabled the page header/footer and documentation for macros and plugins will be hidden. An error will be raised in Trac 1.0.11 and later when optimizations are enabled.

HTTPS issues

If you want to run Trac fully under https you might find that it tries to redirect to plain http. In this case just add the following line to your Apache configuration: