Configurare Cherrypy con Lighttpd ed il modulo FastCGI o SCGI
Un How-To di Samuele Catuzzi alias V3nt0. Questo è un esempio per gli utenti Python che vogliono utilizzare Lighttpd webserver nelle loro applicazioni.
Configurare Cherrypy con lighttpd ed il modulo Fastcgi o scgi
| author: | Samuele Catuzzi |
|---|---|
| contact: | samuele.c@majaglug.net |
| license: | Creative Commons http://creativecommons.org/licenses/by-nc/2.5/ |
| url: | http://www.hostnotfound.it |
Questo è un esempio per gli utenti Python che vogliono utilizzare Lighttpd webserver nelle loro applicazioni.
Requisiti
Prima di tutto, dovresti compilare ed installare lighttpd webserver nel tuo sistema, puoi scaricarlo da http://www.lighttpd.net , è facile da installare come descritto nel Tutorial di Installazione http://trac.lighttpd.net/trac/wiki/TutorialInstallation.
Se intendi usare il modulo FastCGI puoi trovarlo http://www.fastcgi.com/ dopodichè compilalo ed installalo oppure salta questa fase se vuoi utilizzare il modulo scgi che trovi assieme a lighttpd server.
Un altra fase importante è recuperare ed installare uno dei python toolkit di interfacce WSGI, puoi scaricati l'ottima collezione di Allan Saddi "flup" da http://www.saddi.com/software/flup/ che offre supporto per entrambi FastCGI e SCGI in versioni threaded e pre-forking. Il processo di setup per flup come utente root è lo standard python setup.py install.
cd flup_directory python setup.py install
Per ottenere ed installare il modulo SCGI http://www.mems-exchange.org/software/scgi/ devi semplicemente eseguire questo comando:
easy_install scgi
Ora siamo pronti per iniziare a configurare lighttpd server ed impostare la semplice applicazione python "Hello world!" dal pytonico, object-oriented web development framework Cherrypy http://www.cherrypy.org/, prima con un esempio d'uso del modulo FastCGI e poi con il modulo SCGI.
Configurazione Lighttpd+FastCGI
Qui sotto un esempio di file /etc/lighttpd.conf, questa configurazione gestisce tutte le richieste per il modulo FastCGI tramite lo unix file socket /tmp/fcgi.sock che permette migliori perfomance sul sistema locale, ma possiamo anche configurarlo per attendere le connessioni dal WSGI server della nostra applicazione attraverso l'indirizzo di rete e la porta specificati.
server.modules = ( "mod_access",
"mod_fastcgi",
"mod_accesslog",
"mod_rewrite",
"mod_staticfile" )
server.document-root = "/srv/www/pages/cherry01"
server.errorlog = "/tmp/lighttpd.error.log"
accesslog.filename = "/tmp/lighttpd.access.log"
# enable this on Mac OS X or FreeBSD
#server.event-handler = "freebsd-kqueue"
$HTTP["url"] !~ "^/static/" {
fastcgi.server = (
"/" =>
( "127.0.0.1" =>
(
# you can use network address and port binding :
# "host" => "127.0.0.1",
# "port" => 5000,
# or use unix file socket for best performance :
"socket" => "/tmp/fcgi.sock",
"check-local" => "disable"
)
)
)
}
Note:
Non puoi usare entrambi collegamenti file socket e network.
Puoi cambiare il percorso "server.document-root" per rispecchiare la root directory del tuo web server
La nostra applicazione Cherrypy con FastCGI+WSGI
Qui sotto un esempio di applicazione "hello world" che usa il framework Cherrypy con l'interfaccia WSGI , salvalo come hello_fcgi.py nella directory di root del tuo web server.
/srv/www/pages/cherry01/hello_fcgi.py
#!/usr/bin/env python
import logging
import cherrypy
from cherrypy._cpwsgi import wsgiApp
from flup.server.fcgi import WSGIServer
class HelloWorld:
def index(self):
return "Hello world!"
index.exposed = True
cherrypy.root = HelloWorld()
# initialize
cherrypy.config.update({
'global':{
'server.environment': 'production',
'server.logToScreen': False,
}
})
cherrypy.server.start(initOnly=True, serverClass=None)
# run the server :
# you can choose network bind or better unix file socket
#WSGIServer(application=wsgiApp,bindAddress=('127.0.0.1', 5000)).run()
WSGIServer(application=wsgiApp,bindAddress='/tmp/fcgi.sock').run()
Assicurati che il tuo script sia eseguibile tramite il comando chmod +x hello_fcgi.py e finalmente:
- avvia il tuo lighttpd server
- esegui la tua applicazione
E vai su http://localhost/ per vedere il messaggio "Hello world!"
Configurazione Lighttpd+SCGI
Qui un esempio del file /etc/lighttpd.conf, questa configurazione mette il server lighttpd in ascolto verso l'SCGI server della nostra applicazione attraverso l'indirizzo di rete e la porta specificati.
server.modules = ( "mod_access",
"mod_scgi",
"mod_accesslog",
"mod_rewrite",
"mod_staticfile" )
server.document-root = "/srv/www/pages/cherry01"
server.errorlog = "/tmp/lighttpd.error.log"
accesslog.filename = "/tmp/lighttpd.access.log"
# enable this on Mac OS X or FreeBSD
#server.event-handler = "freebsd-kqueue"
$HTTP["url"] !~ "^/static/" {
scgi.server = (
"/" =>
( "127.0.0.1" =>
(
"host" => "127.0.0.1",
"port" => 4000,
"check-local" => "disable"
)
)
)
}
| [1] | Puoi cambiare il percorso "server.document-root" per rispecchiare la root directory del tuo web server |
La nostra applicazione Cherrypy con SCGI+WSGI
Qui sotto un esempio di applicazione "hello world" che usa il framework Cherrypy con l'interfaccia WSGI, salvalo come hello_scgi.py nella directory di root del tuo web server.
/srv/www/pages/cherry01/hello_scgi.py
#!/usr/bin/env python
import logging
import cherrypy
from cherrypy._cpwsgi import wsgiApp
from flup.server.scgi import WSGIServer
class HelloWorld:
def index(self):
return "Hello world!"
index.exposed = True
cherrypy.root = HelloWorld()
# initialize
cherrypy.config.update({
'global':{
'server.environment': 'production',
'server.logToScreen': False,
}
})
cherrypy.server.start(initOnly=True, serverClass=None)
# run the server :
# you can disable console log with loggingLevel=logging.disable as parameter
WSGIServer(application=wsgiApp,bindAddress=('127.0.0.1', 4000)).run()
| [2] | Non puoi usare lo unix file socket |
Assicurati che il tuo script sia eseguibile tramite il comando chmod +x hello_scgi.py e finalmente:
- avvia il tuo lighttpd server
- esegui la tua applicazione
E vai su http://localhost/ per vedere il messaggio "Hello world!"
Se vuoi comunicarmi dei suggerimenti per migliorarlo, inviami un email! Sarà felice di migliorarlo.
LE INFORMAZIONI SU QUESTO SITO SONO FORNITE SENZA NESSUN TIPO DI GARANZIA, IL VISITATORE DI QUESTO SITO WEB SI ASSUME TUTTE LE RESPONSABILITA' E RISCHI NELL'USO DELLE INFORMAZIONI CONTENUTE IN QUESTO SITO. MAJAGLUG.NET E L'AUTORE DELLE INFORMAZIONI IN ESSO CONTENUTE NON SI ASSUME NESSUNA RESPONSABILITA' IN NESSUNA PARTE PER PERDITA O DANNEGGIAMENTO CAUSATO DA ERRORI O OMISSIONI DI INFORMAZIONE.
- Login o registrati per inviare commenti

Bookmark this site
Bookmark this page
Make Us your homepage