Configuring mod_wsgi and multiple vhosts


This is a continuation of my write up on installing Silva 3.0 on a Debian squeeze system. Specifically, note that python2.7 is required and the libapache2-mod-wsgi package is installed from the testing (wheezy) repository. (Links are provided in the installation write up.)

Other than the Debian-specific package install and config locations, all the information contained in this document should apply to any Linux system running apache2 and Silva 3.0. Configuring mod_wsgi

If you'd like to run Silva in a production environment, it is advised to run it as a WSGI application. In Apache, this is easy to do with mod_wsgi. $ sudo apt-get install apache2 libpython2.6 $ sudo dpkg -i libapache2-mod-wsgi_3.3-4_i386.deb

I won't go into detail on configuring Apache; there are better resources available elsewhere. Here's the steps needed to get mod_wsgi serving Silva:

By default, mod_wsgi will load the library compiled for python2.6. However, this will cause Apache to crash when we try to start Silva. We need to change its configuration to load the python2.7 version. Open /etc/apache2/mods-available/wsgi.load with your favorite editor and change the single line to "LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so-2.7" (sans quotes).
Then you just have to tell Apache how to run the WSGI application. Here are the relevant snippets from a virtual host configuration file for one virtual host which will serve both files and directories from the local file system and Silva in a transparent manner, preferring files on the file system to those in Silva:
WSGIDaemonProcess site_name user=username group=groupname threads=4 maximum-requests=1000000 python-eggs=/tmp/python-eggs

<virtualhost *:80>

  <directory /path/to/buildout/silva-3.0/parts/mod_wsgi_app>
    Order deny,allow
    Allow from all
  </directory>

  WSGIScriptAlias /wsgi /path/to/buildout/silva-3.0/parts/mod_wsgi_app/wsgi

  RewriteEngine On

  #Prefer files and directories from the file system over those in Silva.
  #If the RewriteCond fails, there is no file on the file system with the same path and filename,
  #so send the request through to Silva. Silva will take care of returning the appropriate errors
  #if the file is still not found.
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
  RewriteRule (.*) /wsgi$1 [QSA,PT,L]

  WSGIProcessGroup site_name
  WSGIPassAuthorization On
</virtualhost>
Now when you start Apache, the Silva instance will be automatically started as well. It's important to remember that when Silva is running as a WSGI application, you shouldn't manually start Silva using paster.
One gotcha to be aware of is that Apache's automatic directory listings handler (ie, Options +Indexes) has lower precedence than the WSGI handler. Thus, if you want to use the auto index feature, you need to add another RewriteCond to the above configuration. Here's how to allow the auto indexing of a specific folder:
<Directory /var/www/dir/>
  Options +Indexes
</Directory>

...
RewriteCond %{REQUEST_FILENAME} !^/dir/
RewriteRule (.*) /wsgi$1 [QSA,PT,L]

Running more than one virtual host

Once mod_wsgi is properly configured, it is very easy to configure Silva to serve more than one virtual host. I personally run a handful of virtual hosts through Silva, including my main website and the Mellyrn project site. Silva configuration

Coming soon. Apache configuration

Here is the needed Apache configuration for two virtual hosts, one.example.com and two.example.com, respectively. These virtual hosts are identical to the one above, with two exceptions: notice the WSGIApplicationGroup on the second line and that each virtual host has a RequestHeader set command: WSGIDaemonProcess site_name user=username group=groupname threads=4 maximum-requests=1000000 python-eggs=/tmp/python-eggs WSGIApplicationGroup %{GLOBAL}

ServerName one.example.com

Order deny,allow Allow from all

WSGIScriptAlias /wsgi /path/to/buildout/silva-3.0/parts/mod_wsgi_app/wsgi

RewriteEngine On

RequestHeader set X-VHM-URL http://one.example.com

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d RewriteRule (.*) /wsgi$1 [QSA,PT,L]

WSGIProcessGroup site_name WSGIPassAuthorization On

ServerName two.example.com

Order deny,allow Allow from all

WSGIScriptAlias /wsgi /path/to/buildout/silva-3.0/parts/mod_wsgi_app/wsgi

RewriteEngine On

RequestHeader set X-VHM-URL http://two.example.com

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d RewriteRule (.*) /wsgi$1 [QSA,PT,L]

WSGIProcessGroup site_name WSGIPassAuthorization On

It should be intuitive that the value of the X-VMH-URL header will inform Silva which virtual host rule(s) to apply when serving content and rewriting URLs.

You can add as many virtual hosts as you wish using this method, and each can look and act radically different thanks to the power of Silva.

Now just restart Apache and visit you new virtual hosts. Issues with silva.app.forest

There are one or two things to be aware of when using silva.app.forest to provide the virtual hosting service. The first is that it is not as forgiving as the Zope Virtual Host Monster. If a rule is not defined for a specific path within a virtual host, an exception will be raised. Revision History

2013-02-12: Initial publication