Find file
Fetching contributors…
Cannot retrieve contributors at this time
232 lines (161 sloc) 10.4 KB
author description keywords license alias modified modified_by published title deprecated
name email
Use the Python framework to develop powerful and innovative web applications on Debian 5 (Lenny).,web applications,python,web frameworks
Monday, October 8th, 2012
Wednesday, April 14th, 2010 on Debian 5 (Lenny)
true is a web application framework that stresses minimalism, flexibility, rapid application development, and straight forward deployment. Originally developed to power the popular news and link aggregation site "Reddit," is a powerful option for developing systems for the web.

This guide assumes that have you followed the getting started guide prior to beginning. If you are new to Linux server administration, you may be interested in our introduction to Linux concepts guide, beginner's guide and administration basics guide. Furthermore a background in Python programing will be useful as you begin to develop applications with

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).


Install Prerequisites

Before beginning with, we must issue the following commands to ensure your system's package repositories are up to date and the latest versions of all software are installed:

apt-get update
apt-get upgrade

Issue the following command to install all prerequisite software:

apt-get install apache2 python2.5

The application you develop using may require additional dependencies that you can discover and install using your system's package management tool. The following command will install the PostgreSQL database and appropriate database drivers:

apt-get install python-psycopg2 postgresql

For more information about installing and using the PostgreSQL database, consider our documentation. Conversely, if you only need a simple embedded relational database, consider using SQLite, which you can install with the following command:

apt-get install python-pysqlite2 sqlite3


There are a number of different ways to install and deploy the framework. The Debian software repositories include a version of in the 0.2x series, packaged as python-webpy. However, for the purpose of this guide we will install the framework from source. Issue the following commands to download and install the latest released version:

cd /opt/
tar -xzvf
cd /opt/
python install

Make sure you're downloading the latest release by checking the site. Following a new release of the framework, follow the same procedure as above, but substitute the new version number from the project's website. Alternately, issue the following sequence of commands to use git to download the latest development version of

apt-get install git-core
cd /opt/
git clone git://
cd /opt/webpy/
python install

When you want to upgrade to the latest development version, issue the following sequence of commands:

cd /opt/webpy/
git clean -f 
git pull
python install

This method will ensure that you are always running the most up-to-date version of the code, but does not allow you to take advantage of any release testing that the developers may provide.

Create a Basic Application with

There are a number of examples of basic applications developed using the framework. The "main" application file is typically called "". Consider the following, "Hello World" application:

{: .file } : ~~~ python import web

urls = (
    '/(.*)', 'hello'
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":

Save this file at /srv/www/ or the equivalent path depending on your virtual hosting deployment, and proceed with the deployment of the application.

Deploy Applications provides a number of different possibilities for deploying and hosting applications. This document will describe a deployment using the Apache web server and the mod_wsgi process. However, you may choose to deploy your application using whatever web server and application interface method you are most comfortable with: nginx, lighttpd, or Cherokee with FastCGI, CGI, or embedded python interpreters are all viable options.

WSGI is an evolution of the CGI standard, and has performance comparable to FastCGI and embedded interpreter application deployments. Install the required Apache module with the following command:

apt-get install libapache2-mod-wsgi

Issue the following command to ensure that the required modules are enabled within Apache:

a2enmod rewrite

WSGI requires a slight modification to your application. Add the following lines to the end of the file:

{: .file-excerpt } : ~~~ python app = web.application(urls, globals(), autoreload=False) application = app.wsgifunc() ~~~

Consider the following Apache VirtualHost configuration for a mod_wsgi powered application:

{: .file-excerpt } Apache VirtualHost Configuration : ~~~ apache ServerAdmin
ServerName ServerAlias DocumentRoot /srv/www/ ErrorLog /srv/www/ CustomLog /srv/www/ combined

    WSGIScriptAlias / /srv/www/
    Alias /static /srv/www/

    <Directory /srv/www/>
      SetHandler wsgi-script
      Options ExecCGI

    AddType text/html .py

    <Location />
      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_URI} !^/static
      RewriteCond %{REQUEST_URI} !^(/.*)
      RewriteRule ^(.*)$$1 [PT]

Ensure that this virtual host has been enabled, and issue the following command to restart the server:

/etc/init.d/apache2 restart 

In the above example, requests for the domain will be handled by WSGI, with the application files located in /srv/www/ All static files can be stored in /srv/www/ and served directly by Apache. Furthermore, the rewrite rules convert requests so that paths beneath are handled by the application without including in the URL. For example, the request for would be processed as but requests for would not be rewritten and content would be served from /srv/www/

Build a Database Driven Application with

The "Hello World" application above is functional, but isn't able to store or access persistent data in a database system. The following example is simple but inserts and retrieves data from a database system. Consider the following code:

{: .file } : ~~~ python import web

urls = (
    '/(.*)', 'hello'
app = web.application(urls, globals())

db = web.database(dbn='postgres', db='webpy', user='webpy', pw='webweb')

class hello:
    def GET(self, notetext):
        notetext = dict(notes="a note")
        notes ='notes', notetext, what='notes')
        if notes:
            notes = 'a note is found'
            notes = 'no notes are found'
        return notes

if __name__ == "__main__":

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

This program connects to the PostgreSQL database "webpy" and looks in the table "notes" for a note that matches the text "a note." If the note is found, the program returns the text "a note is found"; otherwise, the page will return "no notes are found." Make sure there is a role, or user, in your PostgreSQL database called "webpy" with the credentials specified on the db line of this example.

At the PosgreSQL prompt, issue the following commands to the PostgreSQL shell statement to create the required database and tables. The "webpy" user for PostgreSQL must already exist:

GRANT ALL ON notes TO webpy;
\c webpy
CREATE TABLE notes (note_id int, notes varchar);

While the application is running you can issue the following SQL statements to PostgreSQL to modify the output of the above script:

INSERT INTO notes VALUES (1, 'a note');

DELETE FROM notes WHERE note_id=1;

Congratulations on the development of your new application!

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.