Changes between Initial Version and Version 1 of TracModWSGI


Ignore:
Timestamp:
05/30/11 20:20:19 (13 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.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].'' 
     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. The mod_wsgi adapter is written completely in C and provides significantly better performance than using existing WSGI adapters for mod_python or CGI. 
     6 
     7Trac 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). This file can be created using '''trac-admin <env> deploy <dir>''' command which automatically substitutes required paths. 
     8 
     9{{{ 
     10#!python 
     11import os 
     12 
     13os.environ['TRAC_ENV'] = '/usr/local/trac/mysite' 
     14os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' 
     15 
     16import trac.web.main 
     17application = trac.web.main.dispatch_request 
     18}}} 
     19 
     20The `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. 
     21 
     22'''Important note:''' 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. (The variable may be filled with the path of a previously viewed Trac environment.) To solve this problem, use the following `.wsgi` file instead: 
     23 
     24{{{ 
     25#!python 
     26import os 
     27 
     28os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' 
     29 
     30import trac.web.main 
     31def application(environ, start_response): 
     32  environ['trac.env_path'] = '/usr/local/trac/mysite'  
     33  return trac.web.main.dispatch_request(environ, start_response) 
     34}}} 
     35 
     36For 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. You can create a .wsgi files which handles all this for you by running the TracAdmin command `deploy`. 
     37 
     38If 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: 
     39 
     40{{{ 
     41#!python 
     42import site 
     43site.addsitedir('/usr/local/trac/lib/python2.4/site-packages') 
     44}}} 
     45 
     46Change it according to the path you installed the trac libs at. 
     47 
     48After you've done preparing your wsgi-script, add the following to your httpd.conf. 
     49 
     50{{{ 
     51WSGIScriptAlias /trac /usr/local/trac/mysite/apache/mysite.wsgi 
     52 
     53<Directory /usr/local/trac/mysite/apache> 
     54    WSGIApplicationGroup %{GLOBAL} 
     55    Order deny,allow 
     56    Allow from all 
     57</Directory> 
     58}}} 
     59 
     60Here, 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. 
     61 
     62To 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): 
     63 
     64{{{ 
     65def application(environ, start_response): 
     66        start_response('200 OK',[('Content-type','text/html')]) 
     67        return ['<html><body>Hello World!</body></html>'] 
     68}}} 
     69 
     70See also the mod_wsgi [http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac installation instructions] for Trac. 
     71 
     72For 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. 
     73 
     74''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'' 
     75 
     76== Apache Basic Authentication for Trac thru mod_wsgi == 
     77 
     78Per the mod_wsgi documentation linked to above, here is an example Apache configuration that a) serves the trac from a virtualhost subdomain and b) uses Apache basic authentication for Trac authentication. 
     79 
     80 
     81If 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: 
     82 
     83create the htpasswd file: 
     84{{{ 
     85cd /home/trac-for-my-proj/the-env 
     86htpasswd -c htpasswd firstuser 
     87### and add more users to it as needed: 
     88htpasswd htpasswd seconduser 
     89}}} 
     90(for security keep the file above your document root) 
     91 
     92create this file e.g. (ubuntu) {{{/etc/apache2/sites-enabled/trac.my-proj.my-site.org.conf}}} with these contents: 
     93 
     94{{{ 
     95<Directory /home/trac-for-my-proj/the-deploy/cgi-bin/trac.wsgi> 
     96  WSGIApplicationGroup %{GLOBAL} 
     97  Order deny,allow 
     98  Allow from all 
     99</Directory> 
     100 
     101<VirtualHost *:80> 
     102  ServerName trac.my-proj.my-site.org 
     103  DocumentRoot /home/trac-for-my-proj/the-env/htdocs/ 
     104  WSGIScriptAlias / /home/trac-for-my-proj/the-deploy/cgi-bin/trac.wsgi 
     105  <Location '/'> 
     106    AuthType Basic 
     107    AuthName "Trac" 
     108    AuthUserFile /home/trac-for-my-proj/the-env/htpasswd 
     109    Require valid-user 
     110  </Location> 
     111</VirtualHost> 
     112 
     113}}} 
     114 
     115 
     116(for subdomains to work you would probably also need to alter /etc/hosts and add A-Records to your host's DNS.) 
     117 
     118== Trac with PostgreSQL == 
     119 
     120When 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) 
     121 
     122A 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. 
     123 
     124But it's not necessary to edit the source of trac, the following lines in trac.wsgi will also work: 
     125 
     126{{{ 
     127import trac.db.postgres_backend 
     128trac.db.postgres_backend.PostgreSQLConnection.poolable = False 
     129}}} 
     130 
     131Now Trac drops the connection after serving a page and the connection count on the database will be kept minimal. 
     132 
     133== Getting Trac to work nicely with SSPI and 'Require Group' == 
     134If 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'. 
     135 
     136This WSGI script 'fixes' things, hope it helps: 
     137{{{ 
     138import os 
     139import trac.web.main 
     140 
     141os.environ['TRAC_ENV'] = '/usr/local/trac/mysite' 
     142os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' 
     143 
     144def application(environ, start_response): 
     145    if "\\" in environ['REMOTE_USER']: 
     146        environ['REMOTE_USER'] = environ['REMOTE_USER'].split("\\", 1)[1] 
     147    return trac.web.main.dispatch_request(environ, start_response) 
     148}}} 
     149---- 
     150See also:  TracGuide, TracInstall, [wiki:TracFastCgi FastCGI], [wiki:TracModPython ModPython], [trac:TracNginxRecipe TracNginxRecipe]