Using AJP with flup
AJP (Apache JServ Protocol) is a web connector protocol originally designed to connect Apache Tomcat to a web server. It is a binary, packet-oriented protocol with support for persistent connections. Functionally, it serves the same purpose as other web connector protocols, like FastCGI (more specifically, "static" FastCGI), SCGI, and a host of others. That is, connecting an application server with the web server.
FastCGI documentation and HOWTOs are pretty ubiquitous. Unsurprisingly, AJP has almost no relevance outside of the Java/Tomcat world. So for a Python web application developer/deployer using flup, AJP might seem like an unappealing choice due to the lack of specific documentation. This mini-HOWTO aims to fix that.
But... why??
Firstly, AJP is a simpler protocol than FastCGI. At least, this is my impression after having implemented AJP, FastCGI and SCGI in Python. In this case, simplicitly implies speed and as far as request/response throughput is concerned. The AJP servers are faster than their FastCGI counterparts. (And interestingly, since SCGI is the simplest protocol of them all, its servers are faster than AJP.)
If you run Apache httpd 2.2, there are no additional modules to compile/install for AJP support. (This may change sooner than later as I hear there is a development version of mod_proxy_fastcgi module that works...)
Lastly, if you already run Tomcat/Jetty or any other similar Java servlet container behind your web server, you might already be using AJP and don't want to bother with another connector protocol. (In which case, you probably don't even need this guide.)
Personally, I'm an AJP fan because I fall under the last two points. i.e. I run a heterogenous Java/Python/Ruby/PHP environment and I'm too lazy to install another connector module. ;)
Resources
Some links that may or may not be useful. :)
- The Apache Tomcat Connector site - Includes links to mod_jk source and binaries.
- The protocol description
Apache httpd 2.2
The 2.2 line of Apache's HTTP server includes the mod_proxy_ajp module. If you built httpd with proxy support (TODO: show how to do this?), you should be able to enable AJP support by adding/uncommenting the following lines to httpd.conf:
LoadModule proxy_module libexec/apache22/mod_proxy.so LoadModule proxy_ajp_module libexec/apache22/mod_proxy_ajp.so
(The path to the actual module may be different on your system. See the other LoadModule directives.) Then it's just a simple matter of adding the appropriate ProxyPass directive to your server/virtual host configuration. For example, if you wanted to mount your application at http://example.com/test, within the configuration for example.com, add:
ProxyPass /test ajp://localhost:8009/test
The Python script to start your application would be something similar to the following:
from myapp import app # Get WSGI application object from flup.server.ajp import WSGIServer WSGIServer(app, scriptName='/test').run()
And that's it. By default, flup's AJP servers listen to localhost:8009. You can easily change this by supplying the bindAddress argument to WSGIServer:
from myapp import app # Get WSGI application object from flup.server.ajp import WSGIServer WSGIServer(app, scriptName='/test', bindAddress=('localhost', 8010)).run()
Apache httpd 1.3, 2.0
Uhh.. TBD. :)