Web.py

From DreamHost

Jump to: navigation, search
The instructions provided in this article or section require shell access unless otherwise stated.

You can use the PuTTY client on Windows, or SSH on UNIX and UNIX-like systems such as Linux or Mac OS X.
Your account must be configured for shell access in the Control Panel.
More information may be available on the article's talk page.

I've just prepared this step-by-step guide to get you going quickly with http://webpy.org/

Contents

Setting up web.py

CGI

Setting up CGI is the easiest thing to do, if you could make sure all the pieces are matched together correctly. In the example code, replace example.com with your domain name hosted at DreamHost.

0. Beginning

cd to your domain's www directory:

cd ~/example.com

1. Install web.py

Use the Subversion command-line client to grab the latest web.py code:

svn co http://webpy.org/svn/trunk/web/

Create index.cgi like this:

#!/usr/bin/env python2.4
import web
urls = ('/', 'index')
class index:
   def GET(self):
       print "Hi web.py, finally we meet!"
def runfcgi_apache(func):
    web.wsgi.runfcgi(func, None)
if __name__ == "__main__":
   web.wsgi.runwsgi = runfcgi_apache
   web.run(urls, globals())

And make index.cgi executable, since it's a CGI script:

chmod +x index.cgi

2. Install Flup

Use wget to grab the latest fcgi code:

wget http://svn.saddi.com/py-lib/trunk/fcgi.py

Modify web.py's web/wsgi.py file like so:

--- wsgi.py     (revision 130)
+++ wsgi.py     (working copy)
@@ -13,8 +13,8 @@
    
def runfcgi(func, addr=('localhost', 8000)):
     """Runs a WSGI function as a FastCGI server."""
-    import flup.server.fcgi as flups
-    return flups.WSGIServer(func, multiplexed=True, bindAddress=addr).run()
+    import fcgi as flups
+    return flups.WSGIServer(func, multiplexed=False, bindAddress=addr).run()

3. Serve it via Apache

Edit your Apache .htaccess to enable CGI:

vim .htaccess

Add:

Options +ExecCGI
AddHandler cgi-script .py
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.cgi/$1  [L]
</IfModule>

4. Check and trouble shooting

Your domain's www directory should look something like this:

$ ls -F ~/example.com
fcgi.py  index.cgi*  web/

Point your browser to http://example.com/ (or whatever domain you register with dreamhost) and you should be able to see the welcome greeting. Just in case you have 500 internal problems, check the error log at

tail -n 30 ~/logs/example.com/http/error.log

FCGI

  1. Set up your domain or subdomain with FastCGI. Assuming your domain is todo.dabase.com
  2. cd; rmdir ~/todo.dabase.com
  3. svn co http://svn.natalian.org/projects/todo/
  4. ln -s ~/todo ~/todo.dabase.com
  5. Tweak ~/todo with your mysql db (see config.py) and email stuff for errors

I can't recommend developing on your Dreamhost shell as once the FastCGI processes are running, they're painful to kill and restart them. Develop locally with web.reloader so your changes are immediately reflected in your Web application.

Benchmarking

ab is from the apache2-utils Debian package.

/usr/sbin/ab -c 4 -n 300 http://todo.dabase.com/

4 concurrent connections pushing out 300 requests. If you find it too slow, considering running lighttpd on a dedicated server.

Restarting FASTCGI processes

You probably need to do this also for new code to take effect.

This sometimes works:

killall python2.4

Improving stability and startup speed

  1. Use http://svn.saddi.com/py-lib/trunk/fcgi.py instead of the newer flup
  2. Modify wsgi.py like so:


--- wsgi.py     (revision 130)
+++ wsgi.py     (working copy)
@@ -13,8 +13,8 @@
    
def runfcgi(func, addr=('localhost', 8000)):
     """Runs a WSGI function as a FastCGI server."""
-    import flup.server.fcgi as flups
-    return flups.WSGIServer(func, multiplexed=True, bindAddress=addr).run()
+    import fcgi as flups
+    return flups.WSGIServer(func, multiplexed=False, bindAddress=addr).run()

See also

Chinese version of this article.

Personal tools