Thursday, October 25, 2012

web2py and apache2/mod_python

Finally got it to work!

web2py is an open source Python based web application framework. I had the fun of setting one up on a SuSE ELS 11.1 server. Not wanting to go outside the distribution to alert the server administrative team, I stayed with what are available in the repository, which does not have the WSGI module. So I went with the Python module for Apache.

There is a file named modpythonhandler.py in the top folder of web2py installation, with sample configuration in it. But following that I get an "invalid request" message from accessing web2py. I searched on Google for setting up web2py with mod_python, but it still took me some time and trials to get it right. So here is what I have learned from this experience.

One thing is to setup access to the static files (CSS, JavaScripts, etc.):
AliasMatch ^/([^/]+)/static/(.+)$ /path/to/web2py/applications/$1/static/$2
That turns request for welcome/static/js/jquery.js to the actual path to that file, which is usually in the applications subfolder.

Next, setup the handler for web2py -- All client requests pass through the handler, except for the static files:


SetHandler python-program PythonHandler web2py_modpython PythonPath "sys.path+['/path/to/web2py']"
AllowOverride FileInfo Options -Indexes FollowSymLinks Order Allow,Deny Allow from all
Next, the static files:

SetHandler None Options -Indexes FollowSymLinks ExpiresActive On ExpiresDefault "access plus 1 hour" Order Allow,Deny Allow from all
Files in all the static/ subfolders should be handled by Apache directly.

The content of the web2py_modpython.py file (the Python handler):
from mod_python import apacheimport modpythonhandler
def handler(req):    req.subprocess_env['PATH_INFO'] = '/welcome/default/index'    return modpythonhandler.handler(req)
The end.

No comments:

Post a Comment