NSLU2-Linux
view · edit · print · history

SCGI is a simple protocol between web application and web server. Because it offers much better performance than CGI, it is commonly used to deploy web application written in Python or Ruby.

The example below is on unslung firmware and using optware packages.


Install web app framework packages

Install cherrypy

At the time of this writing, py-cherrypy is 2.1.1-2.

# ipkg install py-cherrypy py-scgi py-paste 

Or, install Ruby on rails

# ipkg install rubygems
# gem install rails --remote --include-dependencies
# wget http://www.zedshaw.com/downloads/scgi_rails/scgi_rails-0.4.3.gem
# gem install scgi_rails-0.4.3.gem --include-dependencies

Write a web app

A simple web app using cherrypy

$ cat ~/test-scgi/test-cherrypy-scgi.py 
import cherrypy, datetime
from cherrypy._cpwsgi import wsgiApp
from paste.util.scgiserver import serve_application

class HelloWorld:
    def index(self):
        return "%s Hello world!" % datetime.datetime.now()
    index.exposed = True

cherrypy.root = HelloWorld()

# init cherrypy
cherrypy.server.start(initOnly=True, serverClass=None)

# run the server
serve_application(application=wsgiApp, prefix="/", port=4000) 

Launch the web app

$ python2.4 ~/test-scgi/test-cherrypy-scgi.py 

A web app using RoR & SCGI Rails Runner

Someone please fill in the blank.


Install a web server with SCGI support

lighttpd

# ipkg install lighttpd 

Or, cherokee

# ipkg install cherokee

Please note cherokee 0.4.31b18 fixed a couple of SCGI/FastCGI problems. Version earlier than this probably does not work.


Configure & launch the web server

Let's setup a static test page

$ cat ~/public_html/test-scgi/static/index.html 
<html>
  <head>
    <title>static page</title>
  </head>
  <body>
    <h2>Testing page for the non-SCGI static content.</h2>
  </body>
</html>

lighttpd

$ cat ~/test-scgi/lighttpd-scgi.conf 
server.port                = 8081
server.modules              = (
                                "mod_rewrite",
                                "mod_access",
                                "mod_scgi",
                                "mod_accesslog" )
server.document-root        = "/home/mylogin/public_html/test-scgi"
server.errorlog             = "/home/mylogin/tmp/lighttpd-error.log"
accesslog.filename          = "/home/mylogin/tmp/lighttpd-access.log"
index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm" )
mimetype.assign             = (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "application/ogg",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".cpp"          =>      "text/plain",
  ".log"          =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
 )
$HTTP["url"] !~ "^/static/" {
    scgi.server = (
        "/" => (
            "127.0.0.1" => (
                "host" => "127.0.0.1",
                "port" => 4000,
                "check-local" => "disable"
            )
        )
    )
}

Launch it.

$ /opt/sbin/lighttpd -D -f ~/test-scgi/lighttpd-scgi.conf 

Or, cherokee

$ cat ~/test-scgi/cherokee-scgi.conf 
Port 8082
IPv6 Off
Timeout 60
KeepAlive On
MaxKeepAliveRequests 500
ServerTokens Full
PidFile /home/mylogin/tmp/cherokee.pid
Icons /opt/etc/cherokee/icons.conf
MimeFile /opt/etc/cherokee/mime.types
MimeFile /opt/etc/cherokee/mime.compression.types
Include /opt/etc/cherokee/advanced.conf
Include /opt/etc/cherokee/mods-enabled
Documentroot /home/mylogin/public_html/test-scgi
DirectoryIndex index.html
Directory /static {
    Handler file
}
Directory / {
    Handler scgi {
        Server localhost:4000
        ErrorHandler on
    }
}

Launch it.

$ /opt/sbin/cherokee -C ~/test-scgi/cherokee-scgi.conf 
Cherokee Web Server 0.4.31b18: Listening on port 8083, TLS disabled
 IPv6 disable, using poll, 1024 fds limit, 5 threads, 204 fds in each
 standard scheduling policy

Test

Here I'm using w3m as web browser, you can also use elinks, or any graphic browser on a different machine (adjust the URL correspondingly).

$ w3m -dump http://localhost:8081/
2006-03-25 19:43:37.34 Hello world!

Build time: 0.061s, Page size: 0.03KB
$ w3m -dump http://localhost:8081/static/
Testing page for the non-SCGI static content.
$ w3m -dump http://localhost:8082/
2006-03-25 19:51:16.82 Hello world!

Build time: 0.074s, Page size: 0.03KB
$ w3m -dump http://localhost:8083/static/index.html
Testing page for the non-SCGI static content.

References:


Up and Running

Let other people know that you successfully use the above procedure to run apps:

view · edit · print · history · Last edited by Brian Zhou.
Based on work by BrianZhou and bzhou.
Originally by bzhou.
Page last modified on May 04, 2006, at 11:40 PM