in Hacks

Bottle, Flup and lighttpd

This is just a short post because I spent a few hours yesterday to get it running although it’s pretty easy.

Bottle application

In case you didn’t know Bottle is a micro web-framework for Python. Here’s a simple application (which is totally insecure):

 

from bottle import route, run

@route('/app/<name>')
def hello_name(name):
    return "Hello " + name

run(host='localhost', port=8080)

If you want to run your bottle application with lighttpd and FastCGI you need to install flup.

Flup

What is flup? Flup is a random assortment of WSGI server and most importantly for us fcgi. You can install it with pip:

 

pip install flup

Now we can run our application with flup.

Run bottle with flup

 

from bottle import route, run

@route('/app/<name>')
def hello_name(name):
    return "Hello " + name

run(server="flup", host='localhost', port=8080)

As you can see you just add server="flup". That’s it. Also notice that I route to /app/. You will see in the next step why I do this. The next step is to install and configure lighttpd.

Get lighttpd running

Again if you’re running a Debian-based distribution it’s just:

sudo apt-get install lighttpd

Start by making a backup copy of the lighttpd.conf which is in /etc/lighttpd/lighttpd.conf:

cp /etc/lighttpd/lighttpd.conf .

Now we can edit it and connect it to our bottle application. First you need to add mod_fastcgi with:

server.modules += ("mod_fastcgi")

Now you can add the fastcgi handlers which look like this:

fastcgi.server = (
    "/app/" =>
        (
                (
                    "host" => "127.0.0.1",
                    "port" => 8080,
                    "check-local" => "disable"
                )
        )
)

In line 2 you can see that we define a directory (/app/). That means that every request to our server in /app/ will be handled by bottle. The host is line 5 is your bottle server which runs on localhost. The port in line 6 should be the same as in your bottle app and check-local just checks first if there’s an /app/ directory. You want bottle to handle these requests so disable it.

Get everything working together

Start lighttpd:

sudo /etc/init.d/lighttpd start

Next you want to start your bottle application:

python server.py

And that’s it. If you connect to your http server with http://localhost:80 you will see the standard lighttpd welcome page. If you now go into http://localhost/app/friend you will see bottle’s output.

 

Why did this take you so long?

Good question. The problem was that a lot of specifications don’t use fastcgi.server with the host:port setting but with bin-path and socket. In this case lighttpd will start your bottle server and connect to a UNIX socket. The problem is that in the latest version of bottle, bottle will try to bind to a host:port setting. It tried it out but it doesn’t seem that you can overwrite this option. So yeah, that’s the simple solution.

Write a Comment

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Just a remark, if you are already running PHP with lighttpd, the mod_fastcgi is already activated in :
    /etc/lighttpd/conf-enabled/10-fastcgi.conf

    And a declaration of fastcgi.server is probably already declared too in :
    /etc/lighttpd/conf-enabled/15-fastcgi-php.conf

    Which means, you can avoid activating “mod_fastcgi” and adapt your bottle fastcgi.server with a “+=” like so :
    fastcgi.server += ( “/app/” =>

  2. PS : I think a comma is missing after “flup” in :
    run(server=”flup” host=’localhost’, port=8080)

    Nice post nonetheless, helped me :)