Edgewall Software

Version 3 (modified by Ryan J Ollos, 9 years ago) ( diff )

Page cleanup.

Alternative frontends for development

Alternative frontend: mod_python

Virtualenv can be used with Apache and mod_python, but as mod_python will be a system install it will not be aware of the virtualenv without some configuration. For development this is useful as one Apache can serve various development versions in a more permanent way. This method will use the system Python, but will add the libraries from the virtualenv.

  1. Make a new frontend script that for instance can be stored in the virtualenv bin directory.
    #myvirtualtrac.py
    
    import os
    import site
    site.addsitedir('/path/to/my/virtualenv/lib/python2.4/site-packages')
    
    from trac.web.modpython_frontend import handler
    
  2. Update Apache config to use this script. Add or update options according to the default mod_python setup like for instance found in TracModPython.
    # Extend the path so Apache will find your script on path
    PythonPath "['/path/to/my/virtualenv/bin'] + sys.path"
    
    # Make mod_python use new frontend instead of trac.web.modpython_frontend
    PythonHandler myvirtualtrac
    

Alternative frontend: mod_wsgi

Using Apache, mod_wsgi is a very good alternative to mod_python. Setting this up follows the same pattern, but for development especially there is one major advantage: It can quite easily be set up to auto-reload on code changes.

  1. Update a default TracModWsgi setup to make a daemonised process, and as of mod_wsgi 2.0 (this setup depends on ≥ 2.0) there is built-in support for custom paths and similar. Example of a full VirtualHost configuration:
    <VirtualHost *:80>
        ServerName virtualtrac.local
    
        # Update user and group to be whatever on your system is intended to run the deamon
        # Update the paths to point to virtualenv site-packages (for trac+++) and bin (for script)
        WSGIDaemonProcess virtualtrac user=www group=www threads=25 python-path=/path/to/my/virtualenv/lib/python2.4/site-packages:/path/to/my/virtualenv/bin
    
        WSGIScriptAlias / /path/to/my/virtualenv/bin/virtualtrac.wsgi
    
        <Location />
          WSGIReloadMechanism Process
          WSGIProcessGroup virtualtrac
          # GLOBAL should generally be used instead of SERVER, see http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac
          WSGIApplicationGroup %{GLOBAL}
          Order deny,allow
          Allow from all
        </Location>
    
        # Authentication
        <LocationMatch (/[^/.]+/login)>
          # Note: Change settings with regards to auth method, paths and domain
          AuthType Digest
          AuthName "virtualtrac"
          AuthDigestDomain /trac http://virtualtrac.local
          AuthDigestProvider file
          AuthUserFile /path/to/access.htdigest
          Require valid-user
        </LocationMatch>
    
    </VirtualHost>
    
  2. Go to ReloadingSourceCode and save the Python script as /path/to/my/virtualenv/bin/monitor.py
  3. A basic WSGI frontend script, save as /path/to/my/virtualenv/bin/virtualtrac.wsgi
    import sys
    sys.stdout = sys.stderr
    
    import os
    os.environ['TRAC_ENV_DIR'] = '/path/to/trac/project'
    # or, alternatively for multiple projects
    #os.environ['TRAC_ENV_PARENT_DIR'] = "/parent/path/to/many/projects"
    os.environ['PYTHON_EGG_CACHE'] = '/path/to/a/temp/to/cache/eggs'
    
    import trac.web.main
    
    import monitor
    monitor.start(interval=1.0)
    # Additionally monitor easy-install.pth to restart whenever installs are done
    monitor.track('/path/to/my/virtualenv/lib/python2.4/site-packages/easy-install.pth')
    
    application = trac.web.main.dispatch_request
    
Note: See TracWiki for help on using the wiki.