Web2py Lighttpd Deployment

Web2py is "full stack" Python Web Framework, Lighttpd is fast, multi-threaded HTTP server. I'll present a method to connect web2py-based application under lighttpd.

I assume the following setup is already done:

  • A domain named "myapp.com" is configured to point to your server
  • Python / lighttpd is already installed on server
  • Your web2py app is placed under /var/www/web2py
  • Your web2py app has application "myapproot" configured

First of all, you have to configure lighttpd to locate web2py application,  create file /etc/lighttpd/conf-enabled/myapp.conf:

$HTTP["host"] =~ "(www\.)?myapp\.com" {
	server.indexfiles = ( "/myapproot" )
	server.document-root = "/var/www/myapp"
	server.dir-listing = "disable"
	fastcgi.server = (
		".fcgi" => ("localhost" => (
			"check-local" => "disable",
			"min-procs" => "1",
			"max-procs" => "2",
			"socket" => "/tmp/myapp.sock")
		)
	)

	url.rewrite-once = (
		"^/$" => "/ad",
		"^(/.+?/static/.+)$" => "/applications$1",
		"(^|/.*)$" => "/fcgihandler.fcgi$1",
	)
	$HTTP["url"] !~ "^(/ad|/fcgihandler.fcgi|/applications/myapproot/static/)" {
		url.access-deny = ("")
	}
}

Explanation:

  • (www\.)?myapp\.com: regular expression to match domain with or without "www." prefix
  • server.indexfiles: specifies relative URL that should be called when only domain is given
  • server.document-root: specifies location of web2py app in filesystem
  • server.dir-listing: we do not want user to list our files using HTTP
  • fastcgi.server: specifies where socket file is located
  • url.rewrite-once: allow to use elegant (short) URLs
  • url.access-deny: files other than static directory should be forbidden (security)

Then you have to configure fcgihandler.fcgi script properly:

(...)
fcgi.WSGIServer(application, bindAddress='/tmp/myapp.sock').run()

Note that /tmp/myapp.sock must be the same as specified in lighttpd configuration.

Then you have to start the fcgihandler.fcgi proces and ensure it will start on every boot. That's all.

This entry was posted in en and tagged , . Bookmark the permalink.

One Response to Web2py Lighttpd Deployment

  1. Osman says:

    Thanks Darius. The guide worked for me, but for the initial steps I had to follow this page:
    http://web2py.googlegroups.com/attach/e96c0ab84d8e474e/tutorial.htm?gda=N6i1TkYAAAAVjYlgT9UB3kBWUqQr6_gC1kjMEdPDKS7Zi_fUh-rsJT44sv3cMz_k1pAj62WcCvVx40jamwa1UURqDcgHarKEE-Ea7GxYMt0t6nY0uV5FIQ&view=1&part=2

    Then, when I got to the "configuring lighttpd" part, I put your configuration above into lighttpd.conf.
    My final lighttpd.conf file was this (I did not put anything into conf-enabled), and it points to the 'welcome' app:

    server.modules = (
    "mod_access",
    "mod_alias",
    "mod_compress",
    "mod_redirect",
    "mod_rewrite",
    "mod_fastcgi",
    "mod_status",
    "mod_accesslog",
    )

    server.port = 80
    server.document-root = "/var/www/web2py"
    server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
    server.errorlog = "/var/log/lighttpd/error.log"
    server.pid-file = "/var/run/lighttpd.pid"
    server.username = "www-data"
    server.groupname = "www-data"

    index-file.names = ( "index.php", "index.html",
    "index.htm", "default.htm",
    " index.lighttpd.html" )

    url.access-deny = ( "~", ".inc" )

    static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

    ## Use ipv6 if available
    #include_shell "/usr/share/lighttpd/use-ipv6.pl"
    server.socket = "[::]:80"

    dir-listing.encoding = "utf-8"
    ## server.dir-listing = "enable"

    compress.cache-dir = "/var/cache/lighttpd/compress/"
    compress.filetype = ( "application/x-javascript", "text/css", "text/html", "text/plain" )

    include_shell "/usr/share/lighttpd/create-mime.assign.pl"
    include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

    $HTTP["host"] =~ "(www\.)?mysite\.org" {
    server.indexfiles = ( "/welcome" )
    server.document-root = "/var/www/web2py"
    server.dir-listing = "disable"
    fastcgi.server = (
    ".fcgi" => ("localhost" => (
    "check-local" => "disable",
    "min-procs" => "1",
    "max-procs" => "2",
    "socket" => "/tmp/web2py.sock")
    )
    )

    url.rewrite-once = (
    "^/$" => "/ad",
    "^(/.+?/static/.+)$" => "/applications$1",
    "(^|/.*)$" => "/fcgihandler.fcgi$1",
    )
    $HTTP["url"] !~ "^(/ad|/fcgihandler.fcgi|/applications/welcome/static/)" {
    url.access-deny = ("")
    }
    }

Comments are closed.