How to setup Python with Lighttpd and FastCGI (like PHP)

Posted by johndir on Server Fault See other posts from Server Fault or by johndir
Published on 2011-12-06T15:58:56Z Indexed on 2012/12/13 11:07 UTC
Read the original article Hit count: 314

Filed under:
|
|
|

Running Lighttpd on Linux, I would like to be able to execute Python scripts just the way I execute PHP scripts.

The goal is to be able to execute arbitrary script files stored in the WWW directory, e.g. http://www.example.com/*.py.


  • I would not like to spawn a new Python instance (interpreter) for every request (like done in regular CGI, if I'm not mistaken), which is why I'm using FastCGI.

  • Following Lighttpd's documentation, the following is the FastCGI part of my config file. The problem is that it always runs the /usr/local/bin/python-fcgi script for every *.py file, regardless of the content of that file:

    http://www.example.com/script.py [output=>] "python-fcgi: test"

    (regardless of the content of script.py)

  • I'm not interested in using any framework, but simply executing individual [web] scripts.

How can I make it act like PHP, executing any script in the WWW directory by requesting it's path?

/etc/lighttpd/conf.d/fastcgi.conf:

server.modules += ( "mod_fastcgi" )

index-file.names += ( "index.php" )

fastcgi.server = (
    ".php" => (
        "localhost" => ( 
            "bin-path" => "/usr/bin/php-cgi",
            "socket" => "/var/run/lighttpd/php-fastcgi.sock",
            "max-procs" => 4, # default value
            "bin-environment" => (
                "PHP_FCGI_CHILDREN" => "1", # default value
            ),
            "broken-scriptfilename" => "enable"
          )
    ),
    ".py" =>
    (
        "python-fcgi" =>
        (
         "socket" => "/var/run/lighttpd/fastcgi.python.socket",
         "bin-path" => "/usr/local/bin/python-fcgi",
         "check-local" => "disable",
         "max-procs" => 1,
        )
    )
)

/usr/local/bin/python-fcgi:

#!/usr/bin/python2

def myapp(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['python-fcgi: test\n']

if __name__ == '__main__':
    from flup.server.fcgi import WSGIServer
    WSGIServer(myapp).run()

© Server Fault or respective owner

Related posts about webserver

Related posts about python