After playing for a while with python, easy and neat programming language, I was curious how easy (or hard) would it be to develop web application in it. Looking around google, there is a huge list of web frameworks that is ready for use, but most of them are way too big for this purpose.
Until I found web.py, small, simple and powerful python based web framework with not so great documentation. After playing around for a while, it was time to deploy this puppy onto my VPS with preinstalled CentOS 5.x. Here is a short and easy guide how to setup fully functional web.py enviroment with lighttpd, mysql and flup for fastcgi.
web.py & flup
To install web.py, we will use easy_install.
# wget http://peak.telecommunity.com/dist/ez_setup.py # python ez_setup.py # easy_install web.py
Flup
# wget http://www.saddi.com/software/flup/dist/flup-1.0.tar.gz # tar xvzf flup-1.0.tar.gz ; cd flup-1.0 # python setup.py install
Install lighttpd & fastcgi
First, we need to setup rpmforge repository which contains lighttpd and fastcgi rpms. Then we install them with yum.
# rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm # yum -y install lighttpd lighttpd-fastcgi mysql-server # chkconfig --levels 235 lighttpd on # chkconfig --levels 235 mysqld on # /etc/init.d/lighttpd start # /etc/init.d/mysqld start
Setup home directory for our web.py site
We will create folder called static where all static files such as images and css files will be stored.
# mkdir /home/oursite # mkdir /home/oursite/static # vi /etc/lighttpd/lighttpd.conf
In lighttpd.conf uncomment mod_fastcgi then go to the last line of the file (G in vi) and append:
include "oursite.conf"
Now let’s create oursite.conf
# vi /etc/lighttpd/conf/oursite.conf
oursite.conf:
$HTTP["host"] =~ "oursite\.com" {
server.document-root = "/home/oursite/"
fastcgi.server = ( "/website.py" =>
((
"socket" => "/tmp/fastcgi.socket",
"bin-path" => "/home/oursite/website.py",
"max-procs" => 1,
"bin-environment" => (
"REAL_SCRIPT_NAME" => ""
),
"check-local" => "disable"
))
)
url.rewrite-once = (
"^/favicon.ico$" => "/static/favicon.ico",
"^/static/(.*)$" => "/static/$1",
"^/(.*)$" => "/website.py/$1",
)
}Create our web.py application
As you noticed, in our lighttpd configuration file, we told lighttpd to access website.py file which will contain code for our web.py website. Here is hello world example from official web.py website.
website.py:
#!/usr/bin/env python import web urls = ( '/(.*)', 'hello' ) class hello: def GET(self, name): i = web.input(times=1) if not name: name = 'world' for c in range(int(i.times)): print 'Hello,', name+'!' if __name__ == "__main__": web.run(urls, globals())
Save this file in /home/oursite/website.py
Restart lighttpd
# /etc/init.d/lighttpd restart
Now go to oursite.com (your real domain) and see your web.py powered website in action.
web.py website + documentation
lighttpd website + documentation
Posted in System Administration tagged centos, lighttpd, linux, python, web.py with 2 comments





