Web.py
From DreamHost
| 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. |
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. This might also be the best choice as DreamHost limits FCGI-processes to 100MB.
0. Beginning
cd to your domain's www directory:
cd ~/example.com
1. Install web.py
Download web.py with wget:
wget http://webpy.org/static/web.py-0.3.tar.gz
Create index.py like this:
#!/usr/bin/env python2.5
import web
urls = ("/.*", "hello")
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, world!'
if __name__ == "__main__":
app.run()
And make index.py executable, since it's a CGI script:
chmod +x index.py
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.py/$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.py* 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
See the instructions here: http://thefire.us/archives/261
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
- Use http://svn.saddi.com/py-lib/trunk/fcgi.py instead of the newer flup
- 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
An alternative set of instructions for fcgi
Chinese version of this article.

