21 | | The {{{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. [[BR]] |
22 | | For clarity, you should give this file a {{{.wsgi}}} extension. You should probably put the file in it's own directory, since you will open up its directory to Apache. |
23 | | You can create a .wsgi files which handles all this for you by running the TracAdmin command {{{deploy}}}. |
| 20 | The `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. |
| 21 | |
| 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: |
| 23 | |
| 24 | {{{ |
| 25 | #!python |
| 26 | import os |
| 27 | |
| 28 | os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' |
| 29 | |
| 30 | import trac.web.main |
| 31 | def application(environ, start_response): |
| 32 | environ['trac.env_path'] = '/usr/local/trac/mysite' |
| 33 | return trac.web.main.dispatch_request(environ, start_response) |
| 34 | }}} |
| 35 | |
| 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 open up its directory to Apache. You can create a .wsgi files which handles all this for you by running the TracAdmin command `deploy`. |
| 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'' |
| 75 | |
| 76 | == Apache Basic Authentication for Trac thru mod_wsgi == |
| 77 | |
| 78 | Per the mod_wsgi documentation linked to above, here is an example Apache configuration that a) serves the trac from a virtualhost subdomain and b) uses Apache basic authentication for Trac authentication. |
| 79 | |
| 80 | |
| 81 | If you want your trac to be served from e.g. !http://trac.my-proj.my-site.org, then from the folder e.g. {{{/home/trac-for-my-proj}}}, if you used the command {{{trac-admin the-env initenv}}} to create a folder {{{the-env}}}, and you used {{{trac-admin the-env deploy the-deploy}}} to create a folder {{{the-deploy}}}, then: |
| 82 | |
| 83 | create the htpasswd file: |
| 84 | {{{ |
| 85 | cd /home/trac-for-my-proj/the-env |
| 86 | htpasswd -c htpasswd firstuser |
| 87 | ### and add more users to it as needed: |
| 88 | htpasswd htpasswd seconduser |
| 89 | }}} |
| 90 | (for security keep the file above your document root) |
| 91 | |
| 92 | create this file e.g. (ubuntu) {{{/etc/apache2/sites-enabled/trac.my-proj.my-site.org.conf}}} with these contents: |
| 93 | |
| 94 | {{{ |
| 95 | <Directory /home/trac-for-my-proj/the-deploy/cgi-bin/trac.wsgi> |
| 96 | WSGIApplicationGroup %{GLOBAL} |
| 97 | Order deny,allow |
| 98 | Allow from all |
| 99 | </Directory> |
| 100 | |
| 101 | <VirtualHost *:80> |
| 102 | ServerName trac.my-proj.my-site.org |
| 103 | DocumentRoot /home/trac-for-my-proj/the-env/htdocs/ |
| 104 | WSGIScriptAlias / /home/trac-for-my-proj/the-deploy/cgi-bin/trac.wsgi |
| 105 | <Location '/'> |
| 106 | AuthType Basic |
| 107 | AuthName "Trac" |
| 108 | AuthUserFile /home/trac-for-my-proj/the-env/htpasswd |
| 109 | Require valid-user |
| 110 | </Location> |
| 111 | </VirtualHost> |
| 112 | |
| 113 | }}} |
| 114 | |
| 115 | |
| 116 | (for subdomains to work you would probably also need to alter /etc/hosts and add A-Records to your host's DNS.) |
| 117 | |
| 132 | |
| 133 | == Getting Trac to work nicely with SSPI and 'Require Group' == |
| 134 | If 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'. |
| 135 | |
| 136 | This WSGI script 'fixes' things, hope it helps: |
| 137 | {{{ |
| 138 | import os |
| 139 | import trac.web.main |
| 140 | |
| 141 | os.environ['TRAC_ENV'] = '/usr/local/trac/mysite' |
| 142 | os.environ['PYTHON_EGG_CACHE'] = '/usr/local/trac/mysite/eggs' |
| 143 | |
| 144 | def application(environ, start_response): |
| 145 | if "\\" in environ['REMOTE_USER']: |
| 146 | environ['REMOTE_USER'] = environ['REMOTE_USER'].split("\\", 1)[1] |
| 147 | return trac.web.main.dispatch_request(environ, start_response) |
| 148 | }}} |
| 149 | ---- |
| 150 | See also: TracGuide, TracInstall, [wiki:TracFastCgi FastCGI], [wiki:TracModPython ModPython], [trac:TracNginxRecipe TracNginxRecipe] |