Edgewall Software

Changes between Version 28 and Version 29 of TracModWSGI


Ignore:
Timestamp:
Feb 27, 2011, 4:07:54 PM (13 years ago)
Author:
Christian Boos
Comment:

reorganized the page a bit

Legend:

Unmodified
Added
Removed
Modified
  • TracModWSGI

    v28 v29  
    11= Trac and mod_wsgi =
    22
    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].''
    43
    5 [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 significantly better performance compared to existing WSGI adapters for mod_python or CGI.
     4[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.
    65
    7 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). This file can be created using the '''trac-admin <env> deploy <dir>''' command which automatically substitutes the required paths.
     6[[PageOutline(2-3,Overview,inline)]]
    87
    9 {{{
    10 #!python
     8== The `trac.wsgi` script
     9
     10Trac 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).
     11
     12=== A very basic script
     13In its simplest form, the script could be:
     14
     15{{{#!python
    1116import os
    1217
     
    2025The `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.
    2126
    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:
     27=== A more elaborate script
    2328
    24 {{{
    25 #!python
     29If 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.
     30
     31To solve this problem, use the following `.wsgi` file instead:
     32{{{#!python
    2633import os
    2734
     
    3441}}}
    3542
    36 For clarity, you should give this file a `.wsgi` extension. You should probably put the file in it's own directory, since you will expose its directory to Apache. You can create a .wsgi file which handles all this for you by running the TracAdmin command `deploy`.
     43For 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.
    3744
    3845If 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:
    3946
    40 {{{
    41 #!python
     47{{{#!python
    4248import site
    4349site.addsitedir('/usr/local/trac/lib/python2.4/site-packages')
     
    4652Change it according to the path you installed the Trac libs at.
    4753
    48 After you've done preparing your wsgi-script, add the following to your Apache configuration file (`httpd.conf` for example).
     54=== Recommended `trac.wsgi` script
     55
     56A somewhat 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).
     57
     58
     59== Mapping requests to the script
     60
     61After you've done preparing your .wsgi script, add the following to your Apache configuration file (`httpd.conf` for example).
    4962
    5063{{{
     
    5871}}}
    5972
    60 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.
     73Here, 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.
    6174
    6275To 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):
    6376
    64 {{{
     77{{{#!python
    6578def application(environ, start_response):
    6679        start_response('200 OK',[('Content-type','text/html')])
     
    6881}}}
    6982
    70 See also the mod_wsgi [http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac installation instructions] for Trac.
     83For 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.
    7184
    72 For 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.
    7385
    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''
    7586
    76 == Apache Basic Authentication for Trac thru mod_wsgi ==
     87=== Example: Apache Basic Authentication for Trac and mod_wsgi
    7788
    7889Per 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.
     
    115126Note: for subdomains to work you would probably also need to alter `/etc/hosts` and add A-Records to your host's DNS.
    116127
    117 == Trac with PostgreSQL ==
    118128
    119 When using the mod_wsgi adapter with multiple Trac instances and PostgreSQL (or MySQL?) as a database back-end, the server can create a lot of open database connections. (and thus PostgreSQL processes)
     129== Troubleshooting
    120130
    121 A 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.
     131=== Use a recent version
    122132
    123 But it's not necessary to edit the source of Trac, the following lines in `trac.wsgi` will also work:
     133Please 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].
    124134
    125 {{{
    126 import trac.db.postgres_backend
    127 trac.db.postgres_backend.PostgreSQLConnection.poolable = False
    128 }}}
     135''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''
    129136
    130 Now Trac drops the connection after serving a page and the connection count on the database will be kept minimal.
    131 
    132 == Getting Trac to work nicely with SSPI and 'Require Group' ==
     137=== Getting Trac to work nicely with SSPI and 'Require Group' ===
    133138If 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'.
    134139
    135140This WSGI script 'fixes' things, hope it helps:
    136 {{{
     141{{{#!python
    137142import os
    138143import trac.web.main
     
    146151    return trac.web.main.dispatch_request(environ, start_response)
    147152}}}
     153
     154
     155=== Trac with PostgreSQL ===
     156
     157When 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.
     158
     159A 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.
     160
     161But it's not necessary to edit the source of Trac, the following lines in `trac.wsgi` will also work:
     162
     163{{{
     164import trac.db.postgres_backend
     165trac.db.postgres_backend.PostgreSQLConnection.poolable = False
     166}}}
     167
     168Now Trac drops the connection after serving a page and the connection count on the database will be kept minimal.
     169
     170//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.//
     171
     172=== Other resources
     173
     174For 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.
     175
     176
    148177----
    149178See also:  TracGuide, TracInstall, [wiki:TracFastCgi FastCGI], [wiki:TracModPython ModPython], [trac:TracNginxRecipe TracNginxRecipe]