You are not logged in.

#1 2009-03-27 09:23:54

xelados
Member
Registered: 2007-06-02
Posts: 314
Website

How do I get Python working with lighttpd?

I've been wanting to learn Python lately, and my favorite place to develop is the web, so I figured I could see what I could do by doing a few of my PHP projects in Python instead, to see if Python's the right tool for me.

I've installed lighttpd since it's small and simple. From what I can tell I have fastcgi enabled, since PHP 5 works on my server. I've tried checking the lighttpd docs, their wiki, Arch's wiki, and even searched the forums here, but haven't found any concrete way to get vanilla Python working. I've seen some howtos for various frameworks, but I don't want to learn a framework; I prefer building my stuff from scratch.

The only progress I've managed to make is getting a "403 - Forbidden" error when I try to access a .py file. Here are the contents of my /etc/lighttpd/lighttpd.conf: http://pastebin.com/m41f5831c

The highlighted lines (226 and 227) are what I have used for my attempt. I point my browser to "localhost:90/spork.py", or "localhost:90/sporkpy", and I get the 403 error. I've chmodded the file to be accessible by the webserver, and it's executable. The contents of the file are `print "Hello World!"`, so there's no reason it shouldn't supply output.

Any ideas? I'd really like to get this working quickly, and since I couldn't find other people who have issues with it I assume it's simple.

Offline

#2 2009-03-27 14:47:26

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 833
Website

Re: How do I get Python working with lighttpd?

Last time I checked it was possible only with web.py http://webpy.infogami.com/

Config would be something like this:

# Python support trough fastcgi with web.py
# http://webpy.infogami.com/
fastcgi.server = ( "/code.py" => (( 
   "socket" => "/tmp/py.socket",
   "bin-path" => "/var/www/website.tld/htdocs/code.py",
   "max-procs" => 1,
   "bin-environment" => (
     "REAL_SCRIPT_NAME" => ""
   ),
   "check-local" => "disable"
)))

You need to install an RTFM interface.

Offline

#3 2009-03-27 15:37:35

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,549
Website

Re: How do I get Python working with lighttpd?

There are two ways to get python working on the web.
I'm a web developer and I must say it is one of my preferred languages for the web.
You can use python on cgi mode or you can use mod_python module. Now, there are advantages and "disadvantages" to both. Some people will say that cgi mode is slow and blah, blah,blah ... other will tell you that mod_python is the way to go but too much work to learn.

I have used both and find them both "likable" wink ... but I prefer mod_python because it gives you soooo much power and control and because the way you code feels like you are coding in python. There is a way to code python on the web that resembles PHP in coding style ... but it's not my thing.  Both, CGI and mod_python have great  support for databases too.

I do not know if you can use mod_python with lighthhpt tough ... but that should be a matter of googling a bit.

These links may be of help:
http://www.modpython.org/
http://www.cs.virginia.edu/~lab2q/lesson_1/

The second web site is what you want to look at to solve the problems you are currently having because it walks you through the development of both cgi and mod_python.

Hope this helps.

R.

Offline

#4 2009-03-27 16:39:39

pointone
Wiki Admin
From: Waterloo, ON
Registered: 2008-02-21
Posts: 379

Re: How do I get Python working with lighttpd?

I'd look at line 29 of your lighttpd.conf for clues...

"cgi.asssign" is for the "cgi" module, not "fastcgi", AFAIK.

"fastcgi" is more complicated to configure. Is there a reason you can't simply use the "standard" CGI module?

Last edited by pointone (2009-03-27 16:41:12)


M*cr*s*ft: Who needs quality when you have marketing?

Offline

#5 2009-03-27 18:31:25

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 3,731

Re: How do I get Python working with lighttpd?

You probably want to take a look at http://www.wsgi.org/wsgi/


Evil #archlinux@freenode channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#6 2009-03-27 23:53:14

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: How do I get Python working with lighttpd?

pointone wrote:

I'd look at line 29 of your lighttpd.conf for clues...

"cgi.asssign" is for the "cgi" module, not "fastcgi", AFAIK.

"fastcgi" is more complicated to configure. Is there a reason you can't simply use the "standard" CGI module?

pointone is correct.
you want to comment out mod_fastcgi and uncomment mod_cgi, to get your cgi stanza to work.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#7 2009-03-27 23:58:32

xelados
Member
Registered: 2007-06-02
Posts: 314
Website

Re: How do I get Python working with lighttpd?

I enabled the cgi module, removed the url rewrite rule, and it seems like Python works the same way PHP does now; that's a plus.

What are the benefits of going to FastCGI if I'm still learning Python and have little interest in a framework outside of _maybe_ web.py (which, by the way, is confusing)?

EDIT: I spoke with the guys in #python and Mion in #archlinux; the guys in #python suggested learning wsgi (which I'd like to) and using a python-based server (that uses wsgi) to make things easier. At the moment I'm stuck between cherrypy and pylons.. What I'd like is something I can depend on for production, in the event that I decide to use what I create for the real deal. Which is better? I'd prefer something fairly simple to configure and work with but won't restrict me to one way of doing things.

Last edited by xelados (2009-03-28 01:26:16)

Offline

#8 2009-03-28 01:23:06

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: How do I get Python working with lighttpd?

a cgi script dies after completion (fork). each invokation forks a new process, loads a new python interpreter, libraries, and so on, then performs the operations specified in the code, then exits.

a fastcgi script runs in the background, and the webserver communicates to it over a socket. the script keeps running, just serving new requests. you aren't constantly killing off and respawning it. much more efficient. the communication protocol is a bit lighter weight than sending full http headers. scgi works similarly (just a slightly different protocol than fastcgi).

for simple development work or light traffic though, the simplicity of cgi wins out by a long shot.

Last edited by cactus (2009-03-28 01:23:37)


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#9 2009-03-28 01:28:27

xelados
Member
Registered: 2007-06-02
Posts: 314
Website

Re: How do I get Python working with lighttpd?

Ah, thanks for the clarification, cactus. So fastcgi is the way to go (with lighttpd anyway) in the long run? Judging from the way you described how it works, though, it's limited to one python script (since it runs in the background, waiting for requests). I understand this is the better way to do things if you're developing one app.. but I don't think I'm ready for that yet. Do you have any opinion on pylons vs cherrypy or any other python servers that might scale well?

Offline

#10 2009-03-28 04:15:27

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: How do I get Python working with lighttpd?

It depends on what you want to do, and how you want to do it.
I personally really like django as my favorite full-stack framework.
I like cherrypy as a more lightweight http server option, if you don't want a framework.
If you are writing more generic services/servers (tcp/udp), then I would recommend twisted.

I tried to like pylons, but it seemed needlessly convoluted to me. The re-usability of having component based middlewares seems like a good idea, but I found pylons poorly documented (with either no documents, or outdated/wrong documents) and some funky middleware options.

You might look at turbogears too. I haven't used it much at all, but I know a few people that do (xentac for instance).

As far as scalability, any of the above options will be 'good enough'. We are talking about http here, where scaling is pretty 'easy' (just add more nodes). If you hit a point where scaling becomes an issue, the DB/middleware layer will be the pain-point, not the app layer. You can throw in memcached to help alleviate some of that strain and buy you some growth time too. If all else fails, throw more hardware at it. wink


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#11 2010-03-30 17:21:49

firecat53
Member
From: Sammamish, Wa
Registered: 2007-05-14
Posts: 1,447
Website

Re: How do I get Python working with lighttpd?

I spent half the day yesterday trying to get the python fcgi to work with lighttpd. If you're still interested, here's the config and scripts I finally got working. I was following your thread with interest smile I previously had php working fine with fcgi, and they both work now. You just need to install python-flup.

lighttpd.conf (portion):

server.document-root        = "/srv/http/"
fastcgi.server             = ( ".php" =>
                               ( "localhost" =>
                                 (
                                   "socket" => "/var/run/lighttpd/php-fastcgi.socket",
                                   "bin-path" => "/usr/bin/php-cgi",
                                   "max-procs" => 1,
                                 )
                               ),
                                "/test" =>
                                    (
                                        "python-fcgi" =>
                                        (
                                         "socket" => "/tmp/fastcgi.socket",
                                         "bin-path" => "/usr/local/bin/test.py",
                                         "check-local" => "disable",
                                         "min-procs" => 1,
                                         "max-procs" => 1,
                                        )
                                    )
                            )

/usr/local/bin/test.py (make sure to chmod +x):

#!/usr/bin/env python
def myapp(environ, start_response):
    print 'got request: %s' % environ
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello World!']

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

Now you can restart lighttpd, create the directory 'test' in the server root and visit 'domain.com/test' and you should see the 'Hello World!
I found all the various references to be very confusing. I think I finally solved it with the code directly from the lighttpd site. Hope this helps someone!

Scott

Last edited by firecat53 (2010-03-30 17:25:00)

Offline

Board footer

Powered by FluxBB