Troubles with lighttpd

116 views
Skip to first unread message

Kevin Dodge

unread,
Aug 21, 2008, 10:58:03 PM8/21/08
to web...@googlegroups.com
I'm really excited to start developing with web2py, as it looks like an outstanding web framework. However, getting it to work well with lighttpd is no walk in the park. After many hours of research, I finally came up with a working solution. I'll post it below for the sake of others also trying to set up fastcgi with lighttpd. (skip to my actual questions?)

Firstly, in a lighttpd configuration file (preferably the one that's symlinked in conf-enabled):

    server.document-root = "/path/to/web2py/"
    server.error-handler-404 = "/web2py.fcgi" # Forwards all requests to web2py
    server.dir-listing = "disable" # Required, else / will just show a directory listing
    server.errorlog = "/tmp/error.log"
    server.modules += ( "mod_fastcgi" ) # The fastcgi module is required, of course, for fastcgi to work ;)
    fastcgi.server = ( ".fcgi" => ("localhost" => ("min-procs" => 1, "socket" => "/tmp/web2py.sock" )))

Because of the way lighttpd works, if there are no index files and dir-listing is disabled, it will return a 404 error rather than a traditional 400 Unauthorized (or similiar). And, since we told it to, all 404s are then mapped to the fastcgi script that forwards the request to Gluon. Long story made short: it forwards all requests to Gluon.

The only problem I've encountered so far is that if the following script is not running at the time, or if the server can't access the socket (in this case,
/tmp/web2py.sock), then it will return a 500 Internal Server Error. I've yet to find out how to make this output a friendly message, as server.error-handler-500 is apparently non-existent and thus far, I haven't been able to get any support regarding this. (Note that server.errorfile-prefix doesn't do the job, either, so it might just be a flaw in lighttpd).

Moving along, you'll need this or something similar in web2py.fcgi (which, by the way, must be in the path [sys.path]). Note that the bindAddress must be equal to the one in your lighttpd configuration, and both lighttpd and the script must have access to it (therefore they should be run as the same user).

    #!/usr/bin/env python
    import gluon.main
    import gluon.contrib.gateways.fcgi as fcgi
    application=gluon.main.wsgibase

    fcgi.WSGIServer(application,bindAddress='/tmp/web2py.sock').run()

You'll need to run web2py.fcgi at all times for web2py to work. Again, it should be run as the same user that lighttpd runs under (in my case, www-data).

----

Well, now that I've got it all setup, I just have a few questions. First of all, I frequently get this message:
    WARNING:root:no cache.disk
How do I go about fixing this? And, whenever I try to use the included admin interface (/admin), I get "admin disabled because unable to access password file." So, how do I go about specifying a password and/or password file?

Many thanks,
Kevin Dodge

mdipierro

unread,
Aug 22, 2008, 3:43:00 AM8/22/08
to web2py Web Framework
Thank you for this tutorial Kevin.

The cause of your problems is one of the following:
1) web2py is running in the wrong folder. Try add this to the handler:

import sys, os
sys.path.insert(0,'')
path=os.path.dirname(os.path.abspath(__file__))
if not path in sys.path: sys.path.append(path)
os.chdir(path)

2) the process does not have write access to the web2py folder. You
may need to chown.

Massimo

On Aug 21, 9:58 pm, Kevin Dodge <dodge.ke...@gmail.com> wrote:
> I'm really excited to start developing with web2py, as it looks like an outstanding web framework. However, getting it to work well with lighttpd is no walk in the park. After many hours of research, I finally came up with a working solution. I'll post it below for the sake of others also trying to set up fastcgi with lighttpd. (skip to my actual questions?)
> Firstly, in a lighttpd configuration file (preferably the one that's symlinked in conf-enabled): server.document-root = "/path/to/web2py/"
> server.error-handler-404 = "/web2py.fcgi" # Forwards all requests to web2py
> server.dir-listing = "disable" # Required, else / will just show a directory listing
> server.errorlog = "/tmp/error.log"
> server.modules += ( "mod_fastcgi" ) # The fastcgi module is required, of course, for fastcgi to work ;)
> fastcgi.server = ( ".fcgi" => ("localhost" => ("min-procs" => 1, "socket" => "/tmp/web2py.sock" )))Because of the way lighttpd works, if there are no index files and dir-listing is disabled, it will return a 404 error rather than a traditional 400 Unauthorized (or similiar). And, since we told it to, all 404s are then mapped to the fastcgi script that forwards the request to Gluon. Long story made short: it forwards all requests to Gluon.
> The only problem I've encountered so far is that if the following script is not running at the time, or if the server can't access the socket (in this case,/tmp/web2py.sock), then it will return a 500 Internal Server Error. I've yet to find out how to make this output a friendly message, asserver.error-handler-500is apparently non-existent and thus far, I haven't been able to get any support regarding this. (Note thatserver.errorfile-prefixdoesn't do the job, either, so it might just be a flaw in lighttpd).Moving along, you'll need this or something similar in web2py.fcgi (which, by the way, must be in the path [sys.path]). Note that the bindAddressmustbe equal to the one in your lighttpd configuration, andbothlighttpd and the script must have access to it (therefore they should be run as the same user). #!/usr/bin/env python
> import gluon.main
> import gluon.contrib.gateways.fcgi as fcgi
> application=gluon.main.wsgibase
> fcgi.WSGIServer(application,bindAddress='/tmp/web2py.sock').run()You'll need to run web2py.fcgi at all times for web2py to work. Again, it should be run as the same user that lighttpd runs under (in my case, www-data).
> ----
> Well, now that I've got it all setup, I just havea few questions. First of all, I frequently get this message: WARNING:root:no cache.diskHow do I go about fixing this? And, whenever I try to use the included admin interface (/admin), I get "admin disabled because unable to access password file." So,how do I go about specifying a password and/or password file?
> Many thanks,
> Kevin Dodge

Kevin Dodge

unread,
Aug 22, 2008, 9:15:39 AM8/22/08
to web...@googlegroups.com
Ah, thank you. As it turns out, I forgot to chown the files from my own
user to www-data. The no disk cache message has entirely disappeared
now, however, I'm still getting the no admin password error. How should
I specify the password or what source code do I need to change, because
it only seems to work when I use the included webserver (ie. not
lighttpd with fastcgi)?

Thanks again,
Kevin Dodge

mdipierro

unread,
Aug 22, 2008, 6:31:41 PM8/22/08
to web2py Web Framework
Hi Kevin,

about the admin there are two possibilities.
1) it is disabled because it cannot go unencrypted unless from
localhost
2) the password file is in the wring folder.

check if you have a file web2py/parameters_80.py
If not but you have web2py/parameters_8000.py, then

cp web2py/parameters_8000.py web2py/parameters_80.py

Massimo

Kevin Dodge

unread,
Aug 22, 2008, 8:30:09 PM8/22/08
to web...@googlegroups.com
Oh, thanks a lot. I can use the admin interface now with lighttpd :) A
simple symlink did the trick, between the two files you mentioned below.

Thanks,
Kevin

Kevin Dodge

unread,
Aug 23, 2008, 9:10:05 PM8/23/08
to web...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oh, and since I finally did get it work, I've created a small tutorial
similar to the one I posted in my first email message. I tried, of
course, to be as accurate as possible but if you find any mistakes
please email me so I can correct them. Also, feel free to post this
wherever you see fit so that others may benefit from it :)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iQEcBAEBAgAGBQJIsLTmAAoJEKCz398aPcB7ac0H/R70AGYaXwkswKXXmsk2BSHB
N6xX5YEQHAWURTiBGZ3c7SR7oUKENsnd56zisZtmwwJF0UqpMoJkvphKjVYUty4K
6OTgvugyGhkpbsk7RM+3mOZaA1rWkO6+RIVIKh+vO1NjEHgeoXLl/iGX5rggkImq
fhCpChohyrqzGAmuRC96t8wQJIlAqJpU9ZndqDC4AQLVVzqY3/M9hfIEHtiesUv2
M18ZirwVx7WHHxMfNSzn1ETuW3Si2md8hkUg3pasrz0yLhdicVzlKsE1XkyWbblt
/1C4JcGKduccNBhC1xOxBzH3sN1gttX/ARFjwcn1UoJpLr5mu2O2GvHiNjabCc4=
=7+BP
-----END PGP SIGNATURE-----

tutorial.htm

mdipierro

unread,
Aug 24, 2008, 4:11:47 AM8/24/08
to web2py Web Framework
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 ;)

Massimo Di Pierro

unread,
Aug 25, 2008, 6:37:18 PM8/25/08
to web...@googlegroups.com
Since this is not in Markdown syntax. Could you post it somewhere and
I will link it. Else, would you mind converting it to markdown?

Massimo

> <tutorial.htm>

fengfeng

unread,
Oct 9, 2008, 3:06:41 AM10/9/08
to web2py Web Framework
(plugin.c.165) dlopen() failed for: /usr/lib/lighttpd/mod_indexfile,
mod_fastcgi.so /usr/lib/lighttpd/mod_indexfile, mod_fastcgi.so: cannot
open shared object file: No such file or directory

fengfeng

unread,
Oct 9, 2008, 4:03:07 AM10/9/08
to web2py Web Framework
i always meet 404 - Not Found,
my web2py path is /home/web2py/,
and i put the fcgihandler.fcgi in the web2py path,is it right?
Reply all
Reply to author
Forward
0 new messages