2007-11-26

web.py update 4 - apache cgi configuration

Finally I decided to put my web.py application on test server. I updated python to 2.5, and installed needed packages.

Then I decided to configure application as fast-CGI under existing Apache2 installation with bunch of other running applications. Short and good installation manual can be found at webpy page.

I tried to configure fast-CGI and then CGI setup, but something went wrong. All configuration examples are using url rewriting, and my Apache does not have proper module. I could of course install mod-rewrite for apache, but I realized that I don't need it yet. Simple web.py application should work even without mod-rewrite. It could be nice feature on constrained hosted systems.

After needed modifications my apache config file for CGI looks like that:

<VirtualHost *:80>
ServerName mywebpyapp.domain.com

Alias /static /var/www/mywebpyapp/static
ScriptAliasMatch ^/(.*) /var/www/mywebpyapp/run-cgi.py/$1

<Directory "/var/www/mywebpyapp">
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>


and my separate CGI script run-cgi.py:

#!/usr/local/bin/python2.5
import cgitb; cgitb.enable()
import sys
import web, app.controller

def cgidebugerror():
_wrappedstdout = sys.stdout
sys.stdout = web._oldstdout
cgitb.handler()
sys.stdout = _wrappedstdout

web.internalerror = cgidebugerror

#web.webapi.internalerror = web.debugerror
web.config.db_parameters = dict(dbn='postgres', user='user', pw='password', db='myappdb',host='localhost',port='5432')
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)

if __name__ == "__main__":
urls=app.controller.urls
web.run(urls, globals(), web.reloader)


So my application is working now behind apache and it is ready for production deployment.

2 comments:

Tomasz Worona said...

Quick fix - I've removed not working directive for fast-CGI, that I unintentionally left after my experiments . Of course this configuratio is working for CGI (with or without dead config code).

<Directory "/var/www/mywebpyapp">
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
--- REMOVED HERE ---
<Files controller.py>
SetHandler fastcgi-script
</Files>
--------------------

</Directory>
</VirtualHost>

Willi said...

Hi,

just wanted to send you a big "Thank You" - i have been developing with the internal webserver of webpy for some weeks now and have been searching for an easy integration into a working apache-configuration ever since.