Not that this is particularly semwebby, but whatever.

The files below are a simple example of using web.py with fastCGI and Cheetah (which is how I'm using Redland on the daylily site.) Just put the files in your document root. You do need to have Cheetah Templating installed if you want to use web.render(). If you don't want templating, you can just 'print' the output, and it will go to the client.

File permissions are important! Set them as follows:

  • chmod 755 index.fcgi
  • chmod 644 .htaccess
  • chmod 644 templates/index.tmpl

/index.fcgi

#!/home/USERNAME/usr/bin/python2.4
import web

## Note that you need web.py and Cheetah Templating installed!
## Visit http://webpy.org and http://cheetahtemplate.org

urls = (
  '/(.*)', 'hello'
)

class hello:
  def GET(self, name):
    if not name: name="World"
    web.header('Content-Type', 'text/html')
    web.render("index.tmpl", terms={'name':name}) # Note - leave out templates/ from the path!

if __name__ == "__main__":
  web.runwsgi = web.runfcgi # force to run as Fast CGI
  web.run(urls)

/templates/index.tmpl

<html>
<head>
<title>My Test Page</title>
</head>
<body>
Hello $name!
</body>
</html>

/.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_URI} !^/static
  RewriteCond %{REQUEST_URI} !^/favicon.ico$
  RewriteCond %{REQUEST_URI} !^(/.*)+index.fcgi/
  RewriteRule ^(.*)$ index.fcgi/$1 [PT]
</IfModule>

As the daylily site is built entirely on semantic web technologies (RDF/SPARQL), I wanted Redland (aka librdf) so that I could perform SPARQL queries without having each request take five minutes. The PHP solutions all just seemed too slow. It took me quite a while to figure out how to install redland, even after realizing I would need to install my own version of Python in my home directory.

Here's a description of how I setup Python, Redland, and a few other useful packages on my dreamhost shared hosting account. I'm glossing over some of the details (like how to unzip a tarball) because really, you should probably be comfortable with the basics if you're going to go this route. I've replaced version numbers with "-xxx" in some places, you should substitute whatever version you are using. I used Python 2.4.3 and Redland 1.0.4.

Anyplace you see 'USERNAME', you should replace it with your dreamhost username.

First, get the redland, redland-bindings, and python-2.4 source. Save them all in /home/USERNAME/usr/src/, and unzip each tarball.

Redland

To build + install redland, first change into the redland-1.0.x directory and then:

  1. Configure redland with: ./configure --prefix=/home/USERNAME/usr/
  2. Make redland with: make
  3. Install redland with: make install
Note the comments about libraries (repeated below). I think I address this issue when I link with Python.

Python

To build + install Python, first change into the Python-xxx directory and then:

  1. export LD_RUN_PATH='/home/USERNAME/usr/lib/'
  2. export LDFLAGS="-L/home/USERNAME/usr/lib/ -Wl,-rpath /home/USERNAME/usr/lib/"
  3. Configure python with: ./configure --prefix=/home/USERNAME/usr/
  4. Make python with: make
  5. Install python with: make install

Redland-bindings

To build + install Python, first move the unzipped redland-bindings directory into the redland-1.0.x folder. If you don't do this, redland bindings will not find the Redland source as you compile it.

Next, change into the redland-1.0.x/redland-bindings-xxx/ directory and:

  1. Configure with: ./configure --prefix=/home/USERNAME/usr --with-python=/home/USERNAME/usr/bin/python2.4
  2. Make with: make
  3. install with: make install

Misc. Python packages with setup.py

I also needed a few other python packages - specifically flup, so that I could use fastcgi, and Cheetah for templating. To install those packages, just unzip them into ~/usr/src and run: 'python setup.py install --prefix=/home/USERNAME/usr' in each folder.

Redland's comments after install

After doing "make install", redland will output this warning message:

* If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

I'm relatively certain that I address that when I compile python with the LD_RUN_PATH variable, and the linker flags.

Afterthoughts

After you've done all this, you should have python ready to go. By default, if you type "python" at the prompt you will receive the dreamhost python. To fix this, you can update your PATH variable to include /home/USERNAME/usr/bin/ before the current PATH. You can also just point the 'shebang' line at the top of your python file at /home/USERNAME/usr/bin/python2.4.

Please send corrections / comments to chrismurf at gmail.