Progetto

Generale

Profilo

TracModWSGI » Cronologia » Versione 2

Anonimo, 10-11-2008 15:31

1 1
2 2
h1. Trac and mod_wsgi
3 1
4
5 2
*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:#7205 #7205])._
6
7
"mod_wsgi":http://code.google.com/p/modwsgi/ is an Apache module for running WSGI-compatible Python applications directly on top of Apache:
8
9 1
  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.
10
11
It 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).
12
13 2
<pre>
14 1
import os
15
16
os.environ['TRAC_ENV'] = '/usr/local/trac/mysite'
17
os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs'
18
19
import trac.web.main
20
application = trac.web.main.dispatch_request
21 2
</pre>
22 1
23 2
<pre>
24 1
25 2
<pre>
26
<pre>
27
28 1
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 on top of the wsgi script:
29 2
<pre>
30 1
import site
31
site.addsitedir('/usr/local/trac/lib/python2.4/site-packages')
32 2
</pre>
33 1
Change it according to the path you installed the trac libs at.
34
35
After you've done preparing your wsgi-script, add the following to your httpd.conf.
36
37 2
<pre>
38 1
WSGIScriptAlias /trac /usr/local/trac/mysite/apache/mysite.wsgi
39
40
<Directory /usr/local/trac/mysite/apache>
41
    WSGIApplicationGroup %{GLOBAL}
42
    Order deny,allow
43
    Allow from all
44
</Directory>
45 2
</pre>
46 1
47 2
<pre>
48 1
49
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):
50
51 2
<pre>
52 1
def application(environ, start_response):
53
        start_response('200 OK',[('Content-type','text/html')])
54
        return ['<html><body>Hello World!</body></html>']
55 2
</pre>
56 1
57 2
See also the mod_wsgi "installation instructions":http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac for Trac.
58 1
59 2
For troubleshooting tips, see the [TracModPython#Troubleshooting mod_python troubleshooting] section, as most Apache-related issues are quite similar, plus discussion of potential "application issues":http://code.google.com/p/modwsgi/wiki/ApplicationIssues when using mod_wsgi.
60 1
61
62 2
h2. Trac with [[PostgreSQL]]
63 1
64
65 2
When 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)
66
67
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.
68
69 1
But it's not necessary to edit the source of trac, the following lines in trac.wsgi will also work:
70
71 2
<pre>
72 1
import trac.db.postgres_backend
73
trac.db.postgres_backend.PostgreSQLConnection.poolable = False
74 2
</pre>
75 1
76
Now Trac drops the connection after serving a page and the connection count on the database will be kept minimal.