The Swiss Army Knife For Python Web Developers
The best thing about WSGI is that there are so many gateways for productive usage. If you want to switch from Apache 2 with mod_wsgi to lighttpd with FastCGI, it’s a matter of a few minutes. If you want to distribute your application on an USB stick for presentation, you can bundle everything into an executable using py2exe and the built-in wsgiref module.
The following list shows the most often used server configurations and how you can serve your WSGI applications.
The following gateways are webserver agnostic and will work with any webserver out there that suppors the underlaying interface:
The following gateways are either written especially for the Apache webserver or work best with it.
For a long time, mod_python was the preferred way to deploy Python applications on the Apache webserver, and frameworks like Django still recommend this setup. However, some bugs in mod_python make it hard to deploy WSGI applications, especially the undefined behavior of SCRIPT_NAME makes routing hard.
If you want to use mod_python or have to use it, you can try the mod_python WSGI wrapper from this blog post about mod_python and WSGI.
Here is a small example configuration for Apache and mod_wsgi:
from yourapplicaiton import YourWSGIApplication # mod_wsgi just wants an object called `application` it will use # for dispatching. Pretty simple, huh? application = YourWSGIApplication(configuration='probably', goes='here')
Save it as yourapplication.wsgi and add this to your virtual host config:
WSGIScriptAlias / /path/to/yourapplication.wsgi/
(Note the trailing slash). Or, if you want to have the application in a subfolder:
WSGIScriptAlias /foo /path/to/yourapplication.wsgi
(Without the trailing slash). Detailed usage examples for the WSGI gateways usually come with the gateways or can be found in their wikis.
Selecting the best gateway is mainly a matter of what you have available on your server and how your application is written. Some applications might not be thread safe, others work better with threads etc.
The following IRC channels on freenode deal a lot with WSGI and you might be able to find some help there: