Thank you! I will post it on AlterEgo
Massimo
quote:
> [tutorial.htm]IntroductionHi there! This is a short tutorial to help you set up web2py with lighttpd. I will assume that you have a working lighttpd environment already and have full access to its configuration. First of all, you'll need to grab
thesourcefromweb2py.com, and extract it to your target directory. This directory must be writable by lighttpd (which usually runs under www-data) as well as yourself. You'll also needPython 2.5, which you can grab from the Python website or from your favourite package manager (eg. apt, pacman, etc.). Finally, you'll need various libraries depending on what database backend you want to use (reccomended: MySQL or SQLite for testing). I won't explain how to get these, but if you have access to a package mananger, you can probably just search for them.
> Moving along, we need to setup web2py. Run the Python fileweb2py.py, and start the server. You can shut it down whenever you like (if you want to test it out, then feel free to), but once you do shutdown the server, rename the fileparameters_8000.py(assuming you used the default port8000) toparameters_80.py. This specifies a password for the admin interface, so that you can use it with lighttpd rather than the included server.
> We're done with web2py for now, but we still have some files to modify, so don't close that window (or terminal, whichever you prefer).Preparing the FastCGI serverOkay, the first real step to getting FastCGI to work with web2py and lighttpd is to modify the includedfcgihandler.py. It should look similar to this:#!/usr/bin/env python
> import sys,os
> path=os.path.dirname(os.path.abspath(__file__))
> if not path in sys.path: sys.path.append(path)
> import gluon.main
> import gluon.contrib.gateways.fcgi as fcgi
> application=gluon.main.wsgibase
> fcgi.WSGIServer(application,bindAddress='/tmp/web2py.sock').run()In fact, you might as well just copy and paste that entire code snippet intofcgihandler.py. Please pay special attention to theboldedfilename:/tmp/web2py.sock. This filemustbe writable by both lighttpd and web2py, so don't put it in a folder that would make it inaccessible. /tmp/ should work just fine, however.
> Next, renamefcgihandler.pytofcgihandler.fcgi. The filename itself is arbitrary but the extension is important.
> Finally, since the socket file mentioned earlier must be writable by both web2py and lighttpd, you may wish to run both under the same user. A shell script may come in handy:#!/bin/sh
> sudo -u www-data python fcgihandler.fcgiOr,#!/bin/sh
> python fcgihandler.fcgi & disown
> chmod 666 /tmp/web2py.sockOf course, these are only examples, and they are not necessary. Anyway, you should now run your shell script or simplypython fcgihandler.fcgi. It must be running at all times if you want web2py to work.Configuring lighttpdAgain, I'm assuming that you have a working lighttpd configuration already. Since the configuration scheme may vary (on Debian, config files are in/etc/lighttpd/), it's your responsibility to find your lighttpd configuration and edit it.url.access-deny += ('.py') #Cannot be added to a conditional block
> server.modules += ('mod_fastcgi') #Should not be added to a conditional block
> server.document-root = "/path/to/web2py"
> server.dir-listing = "disable"
> server.error-handler-404 = "/fcgihandler.fcgi"
> fastcgi.server = (".fcgi" => ("localhost" => ("min-procs" => 1, "socket" => "/tmp/web2py.sock")))That's about it! Making sure that you don't have any of those configuration derivatives already set (duplicates will make lighttpd angry), you can pretty much copy and paste that into your configuration and enjoy web2py!
> You should, however, pay special attention toboldattributes. These may be different depending on your setup, so be sure to modify them accordingly. You might also want to test your configuration in a conditional block, like such:$HTTP["host"] =~ "^localhost/web2py" {
> <config here>
> }Of course, you'll need to modify the conditional statement (which, by the way, is aregular expression), but you get the idea.Finishing UpNow that lighttpd is configured, as well as web2py, you should be ready to go!
> Reload lighttpd (if you're on Debian,sudo /etc/init.d/lighttpd reload), and try outlocalhost/web2pyin your browser (or whatever your configuration may be). There should be no problems, but if you're getting errors, it may be related to a few things:lighttpd and/or web2py can't access the socket fileweb2py isn't runningIf you're gettingWARNING:root:no cache.disk, then web2py doesn't have proper write permissionsIf you're gettingadmin disabled because unable to access password file, then you don't have aparameters_port.pyor web2py can't find itIf you're getting a 500 Internal Server Error, lighttpd is having problems communicating with web2py (it probably can't access the socket)If you're getting an Invalid Request, then web2py is having problems processing your request. Try running it on the included server, and remember that applications must be added from the admin interface.If you're getting a 404 Not Found, then lighttpd couldn't find the .fcgi file, or it can't access it.Most likely, you can resolve your problem by fixing one or more of the above things. However, if you're still having troubles, check theFAQor try themailing list. If you're sure your problem is related to lighttpd and/or its configuration, try asking #lighttpd on Freenode. Finally, you're also more than welcome toemail me.
> I hope that you've enjoyed this tutorial, or at least found it useful. If you found any mistakes or things that can be improved upon, please feel free todrop me an email.Closing NotesI think it's somewhat important to note that if web2py (the .fcgi file) is not running, lighttpd will return a 500 Internal Server Error message when trying to access anything that web2py would normally handle. I'm still trying to figure out how you can reroute HTTP 500s with lighty, but so far, to no avail. If there is a way (server.errorfile-prefixdoesnotget the job done), then I will add it to this tutorial, since a friendly page noting that web2py isn't running is a lot nicer than an Internal Server Error page that makes no attempt to comfort users or explain what is wrong.
> Once more, I'm available for contact viaemail, so please don't hesitate to send me a message if you could help improve this tutorial. I wrote it for the sake of others like me, struggling to get web2py to work with lighttpd with little documentation provided on both ends. Remember than in a production environment, lighttpd will be stable and fast, making it an excellent choice, even if you are just testing on localhost. Plus, it's good to know how this stuff works, in case you do ever go live ;)