How WSGI, CGI, and the frameworks are all connected ?
Apache listens on port 80. It gets an HTTP request. It parses the request to find a way to respond. Apache has a LOT of choices for responding. One way to respond is to use CGI to run a script. Another way to respond is to simply serve a file.
In the case of CGI, Apache prepares an environment and invokes the script through the CGI protocol. This is a standard Linux Fork/Exec situation -- the CGI subprocess inherits an OS environment including the socket and stdout. The CGI subprocess writes a response, which goes back to Apache; Apache sends this respond to the browser.
CGI is primitive and annoying. Mostly because it forks a subprocess for every request.
WSGI is an interface that is based on the CGI design pattern. It is not necessarily CGI -- it does not have to fork a subprocess for each request. It can be CGI, but it doesn't have to be.
WSGI adds to the CGI design pattern in several important ways. It parses the HTML headers for you and adds these to the environment. It supplies any POST-oriented input as a file-like object in the environment. It also provides you a function that will formulate the response, saving your from a lot of formatting details.
What do I need to know / install / do if I want to run a web framework (say web.py or cherrypy) on my basic CGI configuration ?
Recall that forking a subprocess is expensive. There are two ways to work around this.
Embedded mod_wsgi
or mod_python
embeds Python inside Apache; no process is forked. Apache runs the Django application directly.
Daemon mod_wsgi
or mod_fastcgi
allows Apache to interact with a separate daemon (or "long-running process"), using the WSGI protocol. You start your long-running Django process, you configure Apache's mod_fastcgi to communicate with this process.
Note that mod_wsgi
can work in either mode: embedded or daemon.
When you read up on mod_fastcgi, you'll see that Django uses on flup to create a WSGI-compatible interface from the information provided by mod_fastcgi. The pipeline works like this.
Apache -> mod_fastcgi -> FLUP (via CGI protocol) -> Django (via WSGI protocol)
Django has several "django.core.handlers" for the various interfaces.
For mod_fastcgi, Django provides a manage.py runfcgi
that integrates FLUP and the handler.
For mod_wsgi, there's a core handler for this.
How to install WSGI support ?
Follow these instructions.
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
For background see this
http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-index