= Trac and mod_wsgi = [http://code.google.com/p/modwsgi/ 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 performances. [[PageOutline(2-3,Overview,inline)]] == The `trac.wsgi` script Trac can be run on top of mod_wsgi with the help of the following application script, which is just a Python file, though usually saved with a `.wsgi` extension). === A very basic script In its simplest form, the script could be: {{{#!python import os os.environ['TRAC_ENV'] = '/usr/local/trac/mysite' os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' import trac.web.main application = trac.web.main.dispatch_request }}} The `TRAC_ENV` variable should naturally be the directory for your Trac environment (if you have several Trac environments in a directory, you can also use `TRAC_ENV_PARENT_DIR` instead), while the `PYTHON_EGG_CACHE` should be a directory where Python can temporarily extract Python eggs. === A more elaborate script If you're 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. To solve this problem, use the following `.wsgi` file instead: {{{#!python import os os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' import trac.web.main def application(environ, start_response): environ['trac.env_path'] = '/usr/local/trac/mysite' return trac.web.main.dispatch_request(environ, start_response) }}} For clarity, you should give this file a `.wsgi` extension. You should probably put the file in its own directory, since you will expose it to Apache. If you have installed Trac and eggs in a path different from the standard one you should add that path by adding the following code at the top of the wsgi script: {{{#!python import site site.addsitedir('/usr/local/trac/lib/python2.4/site-packages') }}} Change it according to the path you installed the Trac libs at. === Recommended `trac.wsgi` script A somewhat robust and generic version of this file can be created using the `trac-admin deploy ` command which automatically substitutes the required paths (see TracInstall#cgi-bin). == Mapping requests to the script After you've done preparing your .wsgi script, add the following to your Apache configuration file (`httpd.conf` for example). {{{ WSGIScriptAlias /trac /usr/local/trac/mysite/apache/mysite.wsgi WSGIApplicationGroup %{GLOBAL} Order deny,allow Allow from all }}} Here, the script is in a subdirectory of the 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 as a result. 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): {{{#!python def application(environ, start_response): start_response('200 OK',[('Content-type','text/html')]) return ['Hello World!'] }}} For more information about using the mod_wsgi specific directives, see the [http://code.google.com/p/modwsgi/wiki/ mod_wsgi's wiki] and more specifically the [http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac IntegrationWithTrac] page. == Configuring Authentication === Example: Basic Authentication with Apache === The simplest way to enable authentication with Apache is to create a password file. Use the `htpasswd` program to create the password file: {{{ $ htpasswd -c /somewhere/trac.htpasswd admin New password: Re-type new password: Adding password for user admin }}} After the first user, you dont need the "-c" option anymore: {{{ $ htpasswd /somewhere/trac.htpasswd john New password: Re-type new password: Adding password for user john }}} ''See the man page for `htpasswd` for full documentation.'' After you've created the users, you can set their permissions using TracPermissions. Now, you'll need to enable authentication against the password file in the Apache configuration: {{{ AuthType Basic AuthName "Trac" AuthUserFile /somewhere/trac.htpasswd Require valid-user }}} If you're hosting multiple projects you can use the same password file for all of them: {{{ AuthType Basic AuthName "Trac" AuthUserFile /somewhere/trac.htpasswd Require valid-user }}} === Example: Digest Authentication with Apache === For better security, it is recommended that you either enable SSL or at least use the “digest” authentication scheme instead of “Basic”. Please read the [http://httpd.apache.org/docs/2.0/ Apache HTTPD documentation] to find out more. For example, on a Debian 4.0r1 (etch) system the relevant section in apache configuration can look like this: {{{ LoadModule auth_digest_module /usr/lib/apache2/modules/mod_auth_digest.so AuthType Digest AuthName "trac" AuthDigestDomain /trac AuthUserFile /somewhere/trac.htpasswd Require valid-user }}} and you'll have to create your .htpasswd file with htdigest instead of htpasswd as follows: {{{ # htdigest /somewhere/trac.htpasswd trac admin }}} where the "trac" parameter above is the same as !AuthName above ("Realm" in apache-docs). === Example: Apache Basic Authentication for Trac and mod_wsgi Per the mod_wsgi documentation linked to above, here is an example Apache configuration that a) serves the Trac instance from a virtualhost subdomain and b) uses Apache basic authentication for Trac authentication. If you want your Trac to be served from e.g. !http://trac.my-proj.my-site.org, then from the folder e.g. {{{/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: Create the htpasswd file: {{{ cd /home/trac-for-my-proj/the-env htpasswd -c htpasswd firstuser ### and add more users to it as needed: htpasswd htpasswd seconduser }}} (keep the file above your document root for security reasons) Create this file e.g. (ubuntu) {{{/etc/apache2/sites-enabled/trac.my-proj.my-site.org.conf}}} with the following contents: {{{ WSGIApplicationGroup %{GLOBAL} Order deny,allow Allow from all ServerName trac.my-proj.my-site.org DocumentRoot /home/trac-for-my-proj/the-env/htdocs/ WSGIScriptAlias / /home/trac-for-my-proj/the-deploy/cgi-bin/trac.wsgi AuthType Basic AuthName "Trac" AuthUserFile /home/trac-for-my-proj/the-env/htpasswd Require valid-user }}} Note: 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. See mod_wsgi tickets [http://code.google.com/p/modwsgi/issues/detail?id=100 #100] and [http://code.google.com/p/modwsgi/issues/detail?id=132 #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 [http://www.mail-archive.com/modwsgi@googlegroups.com/msg01917.html here]) solved this for me[[BR]]-- Graham Shanks'' === Getting Trac to work nicely with SSPI and 'Require Group' === If like me you've 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 its not working your usernames in trac are probably looking like 'DOMAIN\user' rather than 'user'. This WSGI script 'fixes' things, hope it helps: {{{#!python import os import trac.web.main os.environ['TRAC_ENV'] = '/usr/local/trac/mysite' os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' def application(environ, start_response): if "\\" in environ['REMOTE_USER']: environ['REMOTE_USER'] = environ['REMOTE_USER'].split("\\", 1)[1] return trac.web.main.dispatch_request(environ, start_response) }}} === Trac with PostgreSQL === When using the mod_wsgi adapter with multiple Trac instances and PostgreSQL (or MySQL?) as a database back-end, the server ''may'' create a lot of open database connections and thus PostgreSQL processes. A somewhat brutal workaround is to disabled connection pooling in Trac. This is done by setting `poolable = False` in `trac.db.postgres_backend` on the `PostgreSQLConnection` class. But it's not necessary to edit the source of Trac, the following lines in `trac.wsgi` will also work: {{{ import trac.db.postgres_backend trac.db.postgres_backend.PostgreSQLConnection.poolable = False }}} Now Trac drops the connection after serving a page and the connection count on the database will be kept minimal. //This is not a recommended approach though. See also the notes at the bottom of the [http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac mod_wsgi's IntegrationWithTrac] wiki page.// === Other resources For more troubleshooting tips, see also the [TracModPython#Troubleshooting mod_python troubleshooting] section, as most Apache-related issues are quite similar, plus discussion of potential [http://code.google.com/p/modwsgi/wiki/ApplicationIssues application issues] when using mod_wsgi. ---- See also: TracGuide, TracInstall, [wiki:TracFastCgi FastCGI], [wiki:TracModPython ModPython], [trac:TracNginxRecipe TracNginxRecipe]