Changes between Initial Version and Version 1 of TracModWSGI


Ignore:
Timestamp:
03/20/09 12:02:34 (16 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracModWSGI

    v1 v1  
     1= Trac and mod_wsgi =
     2
     3'''Important note:''' ''Please use either version 1.3 or 2.3 or later of `mod_wsgi`. Version 2.0 has problems with downloading attachments (see [trac:ticket:7205 #7205]).''
     4
     5[http://code.google.com/p/modwsgi/ mod_wsgi] is an Apache module for running WSGI-compatible Python applications directly on top of Apache:
     6
     7  The mod_wsgi adapter is an Apache module that provides a WSGI compliant interface for hosting Python based web applications within Apache. The adapter is written completely in C code against the Apache C runtime and for hosting WSGI applications within Apache provides significantly better performance than using existing WSGI adapters for mod_python or CGI.
     8
     9It is already possible to run Trac on top of mod_wsgi. This can be done by writing the following application script, which is just a Python file, though usually saved with a .wsgi extension).
     10
     11{{{
     12import os
     13
     14os.environ['TRAC_ENV'] = '/usr/local/trac/mysite'
     15os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs'
     16
     17import trac.web.main
     18application = trac.web.main.dispatch_request
     19}}}
     20
     21The {{{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. [[BR]]
     22For clarity, you should give this file a {{{.wsgi}}} extension. You should probably put the file in it's own directory, since you will open up its directory to Apache.
     23You can create a .wsgi files which handles all this for you by running the TracAdmin command {{{deploy}}}.
     24
     25If you have installed trac and eggs in a path different from the standard one you should add that path by adding the following code on top of the wsgi script:
     26{{{
     27import site
     28site.addsitedir('/usr/local/trac/lib/python2.4/site-packages')
     29}}}
     30Change it according to the path you installed the trac libs at.
     31
     32After you've done preparing your wsgi-script, add the following to your httpd.conf.
     33
     34{{{
     35WSGIScriptAlias /trac /usr/local/trac/mysite/apache/mysite.wsgi
     36
     37<Directory /usr/local/trac/mysite/apache>
     38    WSGIApplicationGroup %{GLOBAL}
     39    Order deny,allow
     40    Allow from all
     41</Directory>
     42}}}
     43
     44Here, 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 subinterpreters 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.
     45
     46To 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):
     47
     48{{{
     49def application(environ, start_response):
     50        start_response('200 OK',[('Content-type','text/html')])
     51        return ['<html><body>Hello World!</body></html>']
     52}}}
     53
     54See also the mod_wsgi [http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac installation instructions] for Trac.
     55
     56For troubleshooting tips, see 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.
     57
     58== Trac with PostgreSQL ==
     59
     60When using the mod_wsgi adapter with multiple Trac instances and PostgreSQL (or MySQL?) as a database back-end the server can get a lot of open database connections. (and thus PostgreSQL processes)
     61
     62A workable solution is to disabled connection pooling in Trac. This is done by setting poolable = False in trac.db.postgres_backend on the PostgreSQLConnection class.
     63
     64But it's not necessary to edit the source of trac, the following lines in trac.wsgi will also work:
     65
     66{{{
     67import trac.db.postgres_backend
     68trac.db.postgres_backend.PostgreSQLConnection.poolable = False
     69}}}
     70
     71Now Trac drops the connection after serving a page and the connection count on the database will be kept minimal.
     72
     73== Getting Trac to work nicely with SSPI and 'Require Group' ==
     74If 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'.
     75
     76This WSGI script 'fixes' things, hope it helps:
     77{{{
     78import os
     79import trac.web.main
     80
     81os.environ['TRAC_ENV'] = '/usr/local/trac/mysite'
     82os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs'
     83
     84def application(environ, start_response):
     85    if "\\" in environ['REMOTE_USER']:
     86        environ['REMOTE_USER'] = environ['REMOTE_USER'].split("\\", 1)[1]
     87    return trac.web.main.dispatch_request(environ, start_response)
     88}}}
     89----
     90See also:  TracGuide, TracInstall, [wiki:TracFastCgi FastCGI], [wiki:TracModPython ModPython], [trac:TracNginxRecipe TracNginxRecipe]