CGI deployment on Apache

Here are the simple steps needed to create and run an web.py application.

  • Install web.py and flups

  • Create the application as documented

  1. if __name__ == "__main__":
  2. web.run(urls, globals())

For our example, let it be named app.py, located in /www/app and we need it accessible as http://server/app/app.py.

  • Configure Apache (version 2.2 in this example)
  1. ScriptAlias /app "/www/app/"
  2. <Directory "/www/app/">
  3. Options +ExecCGI +FollowSymLinks
  4. Order allow,deny
  5. Allow from all
  6. </Directory>

That’s it. Your application is accessible via http://server/app/app.py/. Additional URLs handled by the application are added to the end of the URL, for examples http://server/app/app.py/myurl.

  • .htaccess configuration
  1. Options +ExecCGI
  2. AddHandler cgi-script .py
  3. DirectoryIndex index.py
  4. <IfModule mod_rewrite.c>
  5. RewriteEngine on
  6. RewriteBase /
  7. RewriteCond %{REQUEST_FILENAME} !-f
  8. RewriteCond %{REQUEST_FILENAME} !-d
  9. RewriteCond %{REQUEST_URI} !^/favicon.ico$
  10. RewriteCond %{REQUEST_URI} !^(/.*)+index.py/
  11. RewriteRule ^(.*)$ index.py/$1 [PT]
  12. </IfModule>

Here it is assumed that your application is called index.py. The above htaccess checks if some static file/directory exists failing which it routes the data to your index.py. Change the Rewrite Base to a sub-directory if needed.