Context Navigation

Trac and mod_wsgi

​mod_wsgi is an Apache module for running WSGI-compatible Python applications directly on top of the Apache webserver. The mod_wsgi adapter is written completely in C and provides very good performance.

The trac.wsgi script

Trac can be run on top of mod_wsgi with the help of an application script, which is a Python file saved with a .wsgi extension.

A robust and generic version of this file can be created using the trac-admin <env> deploy <dir> command which automatically substitutes the required paths, see TracInstall#cgi-bin. The script should be sufficient for most installations and users not wanting more information can proceed to configuring Apache.

If you are using Trac with multiple projects, you can specify their common parent directory using the TRAC_ENV_PARENT_DIR in trac.wsgi:

defapplication(environ, start_request):# Add this to config when you have multiple projects
environ.setdefault('trac.env_parent_dir','/usr/share/trac/projects')..

The TRAC_ENV variable should naturally be the directory for your Trac environment, and the PYTHON_EGG_CACHE should be a directory where Python can temporarily extract Python eggs. If you have several Trac environments in a directory, you can also use TRAC_ENV_PARENT_DIR instead of TRAC_ENV.

On Windows:

If run under the user's session, the Python Egg cache can be found in %AppData%\Roaming, for example:

If run under a Window service, you should create a directory for Python Egg cache:

os.environ['PYTHON_EGG_CACHE']=r'C:\Trac-Python-Eggs'

A more elaborate script

If you are using multiple .wsgi files (for example one per Trac environment) you must not use os.environ['TRAC_ENV'] to set the path to the Trac environment. Using this method may lead to Trac delivering the content of another Trac environment, as the variable may be filled with the path of a previously viewed Trac environment.

In order to let Apache run the script, access to the directory in which the script resides is opened up to all of Apache. Additionally, the WSGIApplicationGroup directive ensures that Trac is always run in the first Python interpreter created by mod_wsgi. This is necessary because the Subversion Python bindings, which are used by Trac, don't always work in other sub-interpreters and may cause requests to hang or cause Apache to crash. After adding this configuration, restart Apache, and then it should work.

To test the setup of Apache, mod_wsgi and Python itself (ie without involving Trac and dependencies), this simple wsgi application can be used to make sure that requests gets served (use as only content in your .wsgi script):

For multiple environments, you can use the same LocationMatch as described with the previous method.

Note: Location cannot be used inside .htaccess files, but must instead live within the main httpd.conf file. If you are on a shared server, you therefore will not be able to provide this level of granularity.

Don't forget to activate the mod_auth_digest. For example, on a Debian 4.0r1 (etch) system:

You will also need to provide an account for Apache to use when checking credentials. As this password will be listed in plain text in the configuration, you need to use an account specifically for this task:

Note 1: This is the case where the LDAP search will get around the multiple OUs, conecting to the Global Catalog Server portion of AD. Note the port is 3268, not the normal LDAP 389. The GCS is basically a "flattened" tree which allows searching for a user without knowing to which OU they belong.

Note 2: You can also require the user be a member of a certain LDAP group, instead of just having a valid login:

Using the above, usernames in Trac will be of the form DOMAIN\username, so you may have to re-add permissions and such. If you do not want the domain to be part of the username, set SSPIOmitDomain On instead.

Using CA SiteMinder Authentication

Setup CA SiteMinder to protect your Trac login URL, for example /trac/login. Also, make sure the policy is set to include the HTTP_REMOTE_USER variable. If your site allows it, you can set this in LocalConfig.conf:

RemoteUserVar="WHATEVER_IT_SHOULD_BE"SetRemoteUser="YES"

The specific variable is site-dependent. Ask your site administrator. If your site does not allow the use of LocalConfig.conf for security reasons, have your site administrator set the policy on the server to set REMOTE_USER.

Also add a LogOffUri parameter to the agent configuration, for example /trac/logout.

Then modify the trac.wsgi script generated using trac-admin <env> deploy <dir> to add the following lines, which extract the HTTP_REMOTE_USER variable and set it to REMOTE_USER:

defapplication(environ, start_request):# Set authenticated username on CA SiteMinder to REMOTE_USER variable # strip() is used to remove any spaces on the end of the stringif'HTTP_SM_USER'in environ:
environ['REMOTE_USER']= environ['HTTP_REMOTE_USER'].strip()...

You do not need any Apache "Location" directives.

Example: Apache/mod_wsgi with Basic Authentication, Trac being at the root of a virtual host

Per the mod_wsgi documentation linked to above, here is an example Apache configuration that:

serves the Trac instance from a virtualhost subdomain

uses Apache basic authentication for Trac authentication.

If you want your Trac to be served from eg http://trac.my-proj.my-site.org, then from the folder eg /home/trac-for-my-proj, if you used the command trac-admin the-env initenv to create a folder the-env, and you used trac-admin the-env deploy the-deploy to create a folder the-deploy, then first:

For subdomains to work you would probably also need to alter /etc/hosts and add A-Records to your host's DNS.

Troubleshooting

Use a recent version

Please use either version 1.6, 2.4 or later of mod_wsgi. Versions prior to 2.4 in the 2.X branch have problems with some Apache configurations that use WSGI file wrapper extension. This extension is used in Trac to serve up attachments and static media files such as style sheets. If you are affected by this problem, attachments will appear to be empty and formatting of HTML pages will appear not to work due to style sheet files not loading properly. Another frequent symptom is that binary attachment downloads are truncated. See mod_wsgi tickets ​#100 and ​#132.

Note: using mod_wsgi 2.5 and Python 2.6.1 gave an Internal Server Error on my system (Apache 2.2.11 and Trac 0.11.2.1). Upgrading to Python 2.6.2 (as suggested ​here) solved this for me-- Graham Shanks

If you plan to use mod_wsgi in embedded mode on Windows or with the MPM worker on Linux, then you will need version 3.4 or greater. See ​#10675 for details.

Getting Trac to work nicely with SSPI and 'Require Group'

If you have set Trac up on Apache, Win32 and configured SSPI, but added a 'Require group' option to your Apache configuration, then the SSPIOmitDomain option is probably not working. If it is not working, your usernames in Trac probably look like 'DOMAIN\user' rather than 'user'.