Changes between Version 2 and Version 3 of TracModPython
- Timestamp:
- 09/27/05 22:35:02 (21 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracModPython
v2 v3 1 1 = Trac and mod_python = 2 2 3 Trac 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.3 Trac 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 [wiki:TracStandalone tracd]/mod_proxy. 4 4 5 Be 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 9 The 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 11 The 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 17 with this one: 18 19 {{{ 20 PythonHandler trac.web.modpython_frontend 21 }}} 5 Be 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 (see [http://projects.edgewall.com/trac/ticket/1090 #1090]) 22 6 23 7 == Simple configuration == 24 8 25 Here's a typical Trac CGI/Apache setup: 26 9 If you just installed mod_python, you may have to add a line to load the module in the Apache configuration: 27 10 {{{ 28 ScriptAlias /projects/myproject /path/to/python/share/trac/cgi-bin/trac.cgi 29 <Location /projects/myproject> 30 SetEnv TRAC_ENV /var/trac/myproject 31 </Location> 11 LoadModule python_module modules/mod_python.so 32 12 }}} 33 13 34 The equivalent mod_python setup is: 35 14 A simple setup of Trac on mod_python looks like this: 36 15 {{{ 37 16 <Location /projects/myproject> 38 17 SetHandler mod_python 39 18 PythonHandler trac.ModPythonHandler 40 PythonOption TracUriRoot "/projects/myproject"41 19 PythonOption TracEnv /var/trac/myproject 20 PythonOption TracUriRoot /projects/myproject 42 21 </Location> 43 22 }}} 44 23 45 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 or if Trac does not seem to recognize the URLs correctly, add the ''TracUriRoot''option.24 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 or if Trac does not seem to recognize the URLs correctly, add the `TracUriRoot` option. 46 25 47 Authentication works the same as for CGI: 48 26 Configuring authentication works the same as for [wiki:TracCgi#AddingAuthentication CGI]: 49 27 {{{ 50 28 <Location "/projects/myproject/login"> … … 56 34 }}} 57 35 58 If 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 36 If the Trac installation isn't installed in your Python path, you'll have to tell Apache where to find the Trac mod_python handler using the `PythonPath` directive: 60 37 {{{ 61 <Location "/projects/myproject"> 62 PythonPath "sys.path+['/path/to/trac']" 38 <Location /projects/myproject> 39 ... 40 PythonPath "sys.path + ['/path/to/trac']" 63 41 ... 64 42 </Location> 65 43 }}} 66 44 67 == Setting up a project on the root of the webserver ==68 69 To 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.org74 Alias /trac /var/www/trac.example.org/htdocs/trac75 <Location />76 SetHandler mod_python77 PythonHandler trac.ModPythonHandler78 79 80 PythonOption TracUriRoot "/"81 PythonOption TracEnv /var/trac/myproject82 </Location>83 <Location /login>84 AuthType Basic85 AuthName "My Project"86 AuthUserFile /var/trac/myproject/.htaccess87 Require valid-user88 </Location>89 <Location /trac>90 SetHandler None91 </Location>92 </VirtualHost>93 }}}94 95 96 The 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.97 98 45 == Setting up multiple projects == 99 46 100 The Trac mod_python handler handler supports a configuration option similar to Subversion's {{{SvnParentPath}}}, called {{{TracEnvParentDir}}}: 101 47 The Trac mod_python handler handler supports a configuration option similar to Subversion's `SvnParentPath`, called `TracEnvParentDir`: 102 48 {{{ 103 49 <Location /projects> 104 50 SetHandler mod_python 105 51 PythonHandler trac.ModPythonHandler 52 PythonOption TracEnvParentDir /var/trac 106 53 PythonOption TracUriRoot /projects 107 PythonOption TracEnvParentDir "/var/trac"108 54 </Location> 109 55 }}} 110 56 111 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 to the generation of the project list.57 When you request the `/projects` URL, you will get a listing of all subdirectories of the directory you set as `TracEnvParentDir`. Selecting any project in the list will bring you to the corresponding Trac environment. 112 58 113 59 If you don't want to have the subdirectory listing as your projects home page you can use a 114 115 60 {{{ 116 61 <LocationMatch "/.+/"> 117 62 }}} 118 63 119 This 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 y uor !DocumentRoot folder.64 This 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 your !DocumentRoot folder. 120 65 121 122 === Use different locations for htdocs and mod_python === 123 The 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 127 Let'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 {{{ 130 Alias /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 164 A 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 166 To 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 168 You can also use the same authentication realm for all of the projects using a {{{<LocationMatch>}}} directive: 169 66 You can also use the same authentication realm for all of the projects using a `<LocationMatch>` directive: 170 67 {{{ 171 68 <LocationMatch "/[^/]+/login"> 172 ... 69 AuthType Basic 70 AuthName "Trac" 71 AuthUserFile /var/trac/.htaccess 72 Require valid-user 173 73 </LocationMatch> 174 74 }}} 175 75 176 76 == Troubleshooting == 177 178 === Setting up plugin cache directory ===179 180 {{{181 SetEnv PYTHON_EGG_CACHE /path/to/dir182 }}}183 77 184 78 === Form submission problems === … … 188 82 === Using .htaccess === 189 83 190 Although 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.84 Although 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 85 192 86 It 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 196 When 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.''199 87 200 88 === Win32 Issues === … … 207 95 === OS X issues === 208 96 209 There is a mod_python issue on OSX: Look at the end of its README. 210 You need to either define the environment variable DYLD_FORCE_FLAT_NAMESPACE before starting httpd 211 or apply [http://www.dscpl.com.au/projects/vampire/PATCHES this patch] to mod_python. 97 When using mod_python on OS X you will not be able to restart Apache using `apachectl restart`. This is apparently fixed in mod_python 3.2, but there's also a patch available for earlier versions [http://www.dscpl.com.au/projects/vampire/patches.html here]. 212 98 213 Also 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 2004217 +++ /Usersjkp/mod_python.c Wed Mar 16 21:15:49 2005218 @@ -31,6 +31,10 @@219 * (In a Python dictionary) */220 static PyObject * interpreters = NULL;221 222 +#ifdef WITH_THREAD223 +static apr_thread_mutex_t* interpreters_lock = 0;224 +#endif225 +226 apr_pool_t *child_init_pool = NULL;227 228 /**229 @@ -124,6 +128,8 @@230 name = MAIN_INTERPRETER;231 232 #ifdef WITH_THREAD233 + apr_thread_mutex_lock(interpreters_lock);234 +235 PyEval_AcquireLock();236 #endif237 238 @@ -149,6 +155,8 @@239 240 #ifdef WITH_THREAD241 PyEval_ReleaseLock();242 +243 + apr_thread_mutex_unlock(interpreters_lock);244 #endif245 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_THREAD270 + apr_thread_mutex_create(&interpreters_lock,APR_THREAD_MUTEX_UNNESTED,p);271 +272 /* create and acquire the interpreter lock */273 PyEval_InitThreads();274 #endif275 }}}276 277 278 ''Mar. 25/05''279 280 The 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).281 99 ---- 282 See also TracGuide, TracInstall, Trac MultipleProjects100 See also TracGuide, TracInstall, TracCgi, TracFastCgi
