Changes between Version 38 and Version 39 of TracModPython
- Timestamp:
- Sep 25, 2005, 3:26:28 PM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracModPython
v38 v39 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 If you just installed mod_python, you may have to add a line to load the module in the Apache conf: 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 11 LoadModule python_module modules/mod_python.so 29 12 }}} 30 13 31 Here's a typical Trac CGI/Apache setup: 32 33 {{{ 34 ScriptAlias /projects/myproject /path/to/python/share/trac/cgi-bin/trac.cgi 35 <Location /projects/myproject> 36 SetEnv TRAC_ENV /var/trac/myproject 37 </Location> 38 }}} 39 40 The equivalent mod_python setup is: 41 14 A simple setup of Trac on mod_python looks like this: 42 15 {{{ 43 16 <Location /projects/myproject> 44 17 SetHandler mod_python 45 18 PythonHandler trac.ModPythonHandler 46 PythonOption TracUriRoot "/projects/myproject"47 19 PythonOption TracEnv /var/trac/myproject 20 PythonOption TracUriRoot /projects/myproject 48 21 </Location> 49 22 }}} 50 23 51 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. When testing the setup I found that if a single directory (i.e. /myproject) is used the ''TracUriRoot'' line could be left out. When multiple directories (i.e. /projects/myproject) were used the option was needed.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. 52 25 53 Authentication works the same as for CGI: 54 26 Configuring authentication works the same as for [wiki:TracCgi#AddingAuthentication CGI]: 55 27 {{{ 56 28 <Location "/projects/myproject/login"> … … 62 34 }}} 63 35 64 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: 65 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: 66 37 {{{ 67 <Location "/projects/myproject"> 68 PythonPath "sys.path+['/path/to/trac']" 38 <Location /projects/myproject> 39 ... 40 PythonPath "sys.path + ['/path/to/trac']" 69 41 ... 70 42 </Location> 71 43 }}} 72 44 73 == Setting up a project on the root of the webserver ==74 75 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:76 77 {{{78 <VirtualHost trac.example.org>79 ServerName trac.example.org80 Alias /trac /var/www/trac.example.org/htdocs/trac81 <Location />82 SetHandler mod_python83 PythonHandler trac.ModPythonHandler84 85 86 PythonOption TracUriRoot "/"87 PythonOption TracEnv /var/trac/myproject88 </Location>89 <Location /login>90 AuthType Basic91 AuthName "My Project"92 AuthUserFile /var/trac/myproject/.htaccess93 Require valid-user94 </Location>95 <Location /trac>96 SetHandler None97 </Location>98 </VirtualHost>99 }}}100 101 102 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.103 104 45 == Setting up multiple projects == 105 46 106 The Trac mod_python handler handler supports a configuration option similar to Subversion's {{{SvnParentPath}}}, called {{{TracEnvParentDir}}}: 107 47 The Trac mod_python handler handler supports a configuration option similar to Subversion's `SvnParentPath`, called `TracEnvParentDir`: 108 48 {{{ 109 49 <Location /projects> 110 50 SetHandler mod_python 111 51 PythonHandler trac.ModPythonHandler 52 PythonOption TracEnvParentDir /var/trac 112 53 PythonOption TracUriRoot /projects 113 PythonOption TracEnvParentDir "/var/trac"114 54 </Location> 115 55 }}} 116 56 117 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. 118 58 119 59 If you don't want to have the subdirectory listing as your projects home page you can use a 120 121 60 {{{ 122 61 <LocationMatch "/.+/"> 123 62 }}} 124 63 125 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. 126 65 127 128 === Use different locations for htdocs and mod_python === 129 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. 130 131 == Setting up multiple projects as the root URL == 132 133 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: 134 135 {{{ 136 Alias /trac/ /usr/share/trac/htdocs/ 137 <Directory "/usr/share/trac/htdocs"> 138 Order allow,deny 139 Allow from all 140 </Directory> 141 142 <Location /> 143 SetHandler mod_python 144 PythonHandler trac.ModPythonHandler 145 PythonOption TracEnvParentDir "/var/trac" 146 PythonOption TracUriRoot / 147 </Location> 148 149 <Location /project1/login> 150 AuthType Basic 151 AuthName "Project1" 152 AuthUserFile /var/www/projects.yourdomain.com/security/users 153 AuthGroupFile /var/www/projects.yourdomain.com/security/groups 154 Require group project1-users 155 </Location> 156 157 <Location /project2/login> 158 AuthType Basic 159 AuthName "Project2" 160 AuthUserFile /var/www/projects.yourdomain.com/security/users 161 AuthGroupFile /var/www/projects.yourdomain.com/security/groups 162 Require group project2-users 163 </Location> 164 165 <Location /trac> 166 SetHandler none 167 </Location> 168 }}} 169 170 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}}}. 171 172 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. 173 174 You can also use the same authentication realm for all of the projects using a {{{<LocationMatch>}}} directive: 175 66 You can also use the same authentication realm for all of the projects using a `<LocationMatch>` directive: 176 67 {{{ 177 68 <LocationMatch "/[^/]+/login"> 178 ... 69 AuthType Basic 70 AuthName "Trac" 71 AuthUserFile /var/trac/.htaccess 72 Require valid-user 179 73 </LocationMatch> 180 74 }}} 181 75 182 76 == Troubleshooting == 183 184 === Setting up plugin cache directory ===185 186 {{{187 SetEnv PYTHON_EGG_CACHE /path/to/dir188 }}}189 77 190 78 === Form submission problems === … … 194 82 === Using .htaccess === 195 83 196 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. 197 85 198 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. :) 199 200 === mod_python does caching: beware ===201 202 When using mod_python you have to reload apache before changes to a trac.ini of a project or a projects templates take effect!203 204 ''Note: This doesn't apply to the current development version (0.9pre), where the configuration is reloaded automatically if it has changed.''205 87 206 88 === Win32 Issues === … … 213 95 === OS X issues === 214 96 215 There is a mod_python issue on OSX: Look at the end of its README. 216 You need to either define the environment variable DYLD_FORCE_FLAT_NAMESPACE before starting httpd 217 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]. 218 98 219 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:220 221 {{{222 --- src/mod_python.c Mon Feb 16 20:47:27 2004223 +++ /Usersjkp/mod_python.c Wed Mar 16 21:15:49 2005224 @@ -31,6 +31,10 @@225 * (In a Python dictionary) */226 static PyObject * interpreters = NULL;227 228 +#ifdef WITH_THREAD229 +static apr_thread_mutex_t* interpreters_lock = 0;230 +#endif231 +232 apr_pool_t *child_init_pool = NULL;233 234 /**235 @@ -124,6 +128,8 @@236 name = MAIN_INTERPRETER;237 238 #ifdef WITH_THREAD239 + apr_thread_mutex_lock(interpreters_lock);240 +241 PyEval_AcquireLock();242 #endif243 244 @@ -149,6 +155,8 @@245 246 #ifdef WITH_THREAD247 PyEval_ReleaseLock();248 +249 + apr_thread_mutex_unlock(interpreters_lock);250 #endif251 252 if (! idata) {253 @@ -469,6 +477,9 @@254 const char *userdata_key = "python_init";255 apr_status_t rc;256 257 + /* fudge for Mac OS X with Apache where Py_IsInitialized() broke */258 + static int initialized = 0;259 +260 apr_pool_userdata_get(&data, userdata_key, s->process->pool);261 if (!data) {262 apr_pool_userdata_set((const void *)1, userdata_key,263 @@ -490,13 +501,16 @@264 }265 266 /* initialize global Python interpreter if necessary */267 - if (! Py_IsInitialized())268 + if (initialized == 0 || ! Py_IsInitialized())269 {270 + initialized = 1;271 272 /* initialze the interpreter */273 Py_Initialize();274 275 #ifdef WITH_THREAD276 + apr_thread_mutex_create(&interpreters_lock,APR_THREAD_MUTEX_UNNESTED,p);277 +278 /* create and acquire the interpreter lock */279 PyEval_InitThreads();280 #endif281 }}}282 283 284 ''Mar. 25/05''285 286 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).287 99 ---- 288 See also TracGuide, TracInstall, Trac MultipleProjects100 See also TracGuide, TracInstall, TracCgi, TracFastCgi