Changes between Version 1 and Version 2 of TracModPython


Ignore:
Timestamp:
09/16/05 00:03:52 (21 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracModPython

    v1 v2  
    22
    33Trac 0.7.1 and later supports [http://www.modpython.org/ mod_python], which speeds up Trac's response times considerably and permits use of many Apache features not possible with tracd/mod_proxy.
     4
     5Be sure to grab mod_python 3.1.3 and later for ''SetHandler'' ''mod_python'' directive to work.  Also, older versions may generate an internal error.  [http://projects.edgewall.com/trac/ticket/1090 #1090]
     6
     7== Trac Trunk Configuration ==
     8
     9The examples below are written for Trac 0.8.x.  If you are running Trac from the trunk source you will need to make a modification to the configuration.
     10
     11The mod_python handler class is changed in trunk revision [1287] and later, so you'll need to substitute the following line:
     12
     13{{{
     14   PythonHandler trac.ModPythonHandler
     15}}}
     16
     17with this one:
     18
     19{{{
     20   PythonHandler trac.web.modpython_frontend
     21}}}
    422
    523== Simple configuration ==
     
    2543}}}
    2644
    27 Note that the option ''TracUriRoot'' may or may not be necessary in your setup. Try without first, and if the URLs produced by Trac look wrong, add the ''TracUriRoot'' option.
     45Note that the option ''TracUriRoot'' may or may not be necessary in your setup. Try without first, and if the URLs produced by Trac look wrong or if Trac does not seem to recognize the URLs correctly, add the ''TracUriRoot'' option.
     46
     47Authentication works the same as for CGI:
     48
     49{{{
     50<Location "/projects/myproject/login">
     51  AuthType Basic
     52  AuthName "myproject"
     53  AuthUserFile /var/trac/myproject/.htaccess
     54  Require valid-user
     55</Location>
     56}}}
     57
     58If your trac installation isn't installed in your Python path, you'll have to tell Apache where to find the {{{trac.ModPythonHandler}}} module by adding this line to the {{{<Location>}}} section:
     59
     60{{{
     61<Location "/projects/myproject">
     62  PythonPath "sys.path+['/path/to/trac']"
     63  ...
     64</Location>
     65}}}
     66
     67== Setting up a project on the root of the webserver ==
     68
     69To install Trac on the root of the webserver (in a virtual host context for example) and make it available at the ''!http://some-hostname/'' URL, use the following:
     70
     71{{{
     72<VirtualHost trac.example.org>
     73  ServerName trac.example.org
     74  Alias /trac /var/www/trac.example.org/htdocs/trac
     75  <Location />
     76    SetHandler mod_python
     77    PythonHandler trac.ModPythonHandler
     78
     79
     80    PythonOption TracUriRoot "/"
     81    PythonOption TracEnv /var/trac/myproject
     82  </Location>
     83  <Location /login>
     84    AuthType Basic
     85    AuthName "My Project"
     86    AuthUserFile /var/trac/myproject/.htaccess
     87    Require valid-user
     88  </Location>
     89  <Location /trac>
     90    SetHandler None
     91  </Location>
     92</VirtualHost>
     93}}}
     94
     95
     96The path in the last {{{<Location>}}} block should match your {{{htdocs_location}}}. The directive "{{{SetHandler None}}}" allows us to escape mod_python and have Apache serve the static files (located at {{{/var/www/trac.example.org/htdocs/trac/}}} on the filesystem in this example). Any other URLs will be handled by mod_python.
    2897
    2998== Setting up multiple projects ==
     
    37106  PythonOption TracUriRoot /projects
    38107  PythonOption TracEnvParentDir "/var/trac"
     108</Location>
     109}}}
     110
     111When you request the {{{/projects}}} URL, you will get a (currently very simple) listing of all subdirectories of the directory you set as {{{TracEnvParentDir}}}. Selecting any project in the list will bring you to the corresponding Trac instance. You should make sure that the configured directory only contains Trac environment directories that match the currently installed Trac version, because that is not checked prior to the generation of the project list.
     112
     113If you don't want to have the subdirectory listing as your projects home page you can use a
     114
     115{{{
     116<LocationMatch "/.+/">
     117}}}
     118
     119This will instruct Apache to use mod_python for all locations different from root while having the possibility of placing a custom home page for root in yuor !DocumentRoot folder.
     120
     121
     122=== Use different locations for htdocs and mod_python ===
     123The mod_python location must be different than the trac htdocs location. For example, if you map Trac's htdocs location to {{{/trac}}} and then map your mod_python handler to {{{/trac}}} as well, you will encounter strange problems.  Configuration for the location of the htdocs is in the environment's ini file.
     124
     125== Setting up multiple projects as the root URL ==
     126
     127Let's say you want something like http://projects.yourdomain.com which has a list of all of the projects hosted on it.  However, you don't want to do manual configuration every time a new project is added.  Or, you'd like to minimize it.  Here's a quick sample:
     128
     129{{{
     130Alias /trac/ /usr/share/trac/htdocs/
     131<Directory "/usr/share/trac/htdocs">
     132   Order allow,deny
     133   Allow from all
     134</Directory>
     135
     136<Location />
     137   SetHandler mod_python
     138   PythonHandler trac.ModPythonHandler
     139   PythonOption TracEnvParentDir "/var/trac"
     140   PythonOption TracUriRoot /
     141</Location>
     142
     143<Location /project1/login>
     144   AuthType Basic
     145   AuthName "Project1"
     146   AuthUserFile /var/www/projects.yourdomain.com/security/users
     147   AuthGroupFile /var/www/projects.yourdomain.com/security/groups
     148   Require group project1-users
     149</Location>
     150
     151<Location /project2/login>
     152   AuthType Basic
     153   AuthName "Project2"
     154   AuthUserFile /var/www/projects.yourdomain.com/security/users
     155   AuthGroupFile /var/www/projects.yourdomain.com/security/groups
     156   Require group project2-users
     157</Location>
     158
     159<Location /trac>
     160   SetHandler none
     161</Location>
     162}}}
     163
     164A few things to note about this example:  all of the users are stored in one file, {{{/var/www/projects.yourdomain.com/security/users}}}.  Groups for these users are defined in the groups file, {{{/var/www/projects.yourdomain.com/security/groups}}}.  The Trac projects are all stored under {{{/var/trac}}}.
     165
     166To add a new project, you'll have to create a new user in the user file.  Then, create a new group for the project in the group file.  Finally, create a new <Location> block with a new {{{Require group}}} directive.  That's about it.
     167
     168You can also use the same authentication realm for all of the projects using a {{{<LocationMatch>}}} directive:
     169
     170{{{
     171<LocationMatch "/[^/]+/login">
     172   ...
    39173</LocationMatch>
    40174}}}
    41175
    42 When you request the {{{/projects}}} URL, you will get a (currently very simple) listing of all subdirectories of the directory you set as {{{TracEnvParentDir}}}. Selecting any project in the list will bring you to the corresponding Trac instance. You should make sure that the configured directory only contains Trac environment directories that match the currently installed Trac version, because that is not checked prior the the generation of the project list.
    43 
    44 
    45 === Adding authentication ===
    46 
    47 Adding authentication is straightforward in both cases. For example:
    48 
    49 {{{
    50 <LocationMatch /projects/[[:alnum:]]+/login>
    51   AuthType Basic
    52   AuthName "Trac"
    53   AuthUserFile /var/www/passwd
    54   Require valid-user
    55 </LocationMatch>
    56 }}}
     176== Troubleshooting ==
     177
     178=== Setting up plugin cache directory ===
     179
     180{{{
     181SetEnv PYTHON_EGG_CACHE /path/to/dir
     182}}}
     183
     184=== Form submission problems ===
     185
     186If you're experiencing problems submitting some of the forms in Trac (a common problem is that you get redirected to the start page after submission), check whether your {{{DocumentRoot}}} contains a folder or file with the same path that you mapped the mod_python handler to. For some reason, mod_python gets confused when it is mapped to a location that also matches a static resource.
     187
     188=== Using .htaccess ===
     189
     190Although it may seem trivial to rewrite the above configuration as a directory in your document root with a {{{.htaccess}}} file, this does not work. Apache will append a "/" to any Trac URLs, which interferes with its correct operation.
     191
     192It may be possible to work around this with mod_rewrite, but I failed to get this working. In all, it is more hassle than it is worth. Stick to the provided instructions. :)
     193
     194=== mod_python does caching: beware ===
     195
     196When using mod_python you have to reload apache before changes to a trac.ini of a project or a projects templates take effect!
     197
     198 ''Note: This doesn't apply to the current development version (0.9pre), where the configuration is reloaded automatically if it has changed.''
    57199
    58200=== Win32 Issues ===
     
    63205ticket [http://projects.edgewall.com/trac/ticket/554 #554].
    64206
    65 
     207=== OS X issues ===
     208
     209There is a mod_python issue on OSX: Look at the end of its README.
     210You need to either define the environment variable DYLD_FORCE_FLAT_NAMESPACE before starting httpd
     211or apply [http://www.dscpl.com.au/projects/vampire/PATCHES this patch] to mod_python.
     212
     213Also note that there is an error in the module when you build it from source.  Basically an unpatched version will not respond correctly to the 'apachectl restart' command.  If you issue this command on an unpatched module your client will receive a 500 error from apache.  The patch needed to fix this problem is included below:
     214
     215{{{
     216--- src/mod_python.c    Mon Feb 16 20:47:27 2004
     217+++ /Usersjkp/mod_python.c      Wed Mar 16 21:15:49 2005
     218@@ -31,6 +31,10 @@
     219  * (In a Python dictionary) */
     220 static PyObject * interpreters = NULL;
     221 
     222+#ifdef WITH_THREAD
     223+static apr_thread_mutex_t* interpreters_lock = 0;
     224+#endif
     225+
     226 apr_pool_t *child_init_pool = NULL;
     227 
     228 /**
     229@@ -124,6 +128,8 @@
     230         name = MAIN_INTERPRETER;
     231 
     232 #ifdef WITH_THREAD
     233+    apr_thread_mutex_lock(interpreters_lock);
     234+
     235     PyEval_AcquireLock();
     236 #endif
     237 
     238@@ -149,6 +155,8 @@
     239 
     240 #ifdef WITH_THREAD
     241     PyEval_ReleaseLock();
     242+
     243+    apr_thread_mutex_unlock(interpreters_lock);
     244 #endif
     245 
     246     if (! idata) {
     247@@ -469,6 +477,9 @@
     248     const char *userdata_key = "python_init";
     249     apr_status_t rc;
     250 
     251+    /* fudge for Mac OS X with Apache where Py_IsInitialized() broke */
     252+    static int initialized = 0;
     253+
     254     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
     255     if (!data) {
     256         apr_pool_userdata_set((const void *)1, userdata_key,
     257@@ -490,13 +501,16 @@
     258     }
     259 
     260     /* initialize global Python interpreter if necessary */
     261-    if (! Py_IsInitialized())
     262+    if (initialized == 0 || ! Py_IsInitialized())
     263     {
     264+        initialized = 1;
     265 
     266         /* initialze the interpreter */
     267         Py_Initialize();
     268 
     269 #ifdef WITH_THREAD
     270+        apr_thread_mutex_create(&interpreters_lock,APR_THREAD_MUTEX_UNNESTED,p);
     271+
     272         /* create and acquire the interpreter lock */
     273         PyEval_InitThreads();
     274 #endif
     275}}}
     276
     277
     278''Mar. 25/05''
     279
     280The patch listed above causes complaints when attempting to apply it; also, the URL for the OS X patch is broken. I found that patch as well as an additional one for mod_python for use with a multithreaded MPM [http://www.dscpl.com.au/projects/vampire/patches.html here] (same site; pages have been shuffled around).
    66281----
    67282See also TracGuide, TracInstall, TracMultipleProjects