HomePage » WebServer » Lighttpd


Lighttpd

lighttpd seems to be picking up momentum as a high performance alternative to apache.

Installation
Configure options:
./configure --prefix=/usr --sysconfdir=/etc/lighttpd --disable-ipv6 --with-openssl=/usr


Configuration
One can use the sample config under lighttpd-src/docs/lighttpd.conf and make modifications.

Server tuning
server.network-backend - http://www.lighttpd.net/benchmark

server.max-fds = 4096 # open files
server.max-worker = 2 # this may cause issue with access logs
server.use-ipv6 = "disable" # no point
server.event-handler = "linux-sysepoll" # linux specific
server.network-backend = "gthread-aio" # sendfile() minimizes the work in the application and pushes a file directly into the network card. 
# gthread-aio balance between big and small files. posix-aio is good for large files, writev is good for small ones.


SSL
Check that lighttpd has ssl support
> lighttpd -v
lighttpd-1.4.13 (ssl) - a light and fast webserver


Then add these to lighttpd.conf. No need to add 443 to server.port
$SERVER["socket"] == "206.188.19.110:443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/ssl/web.adblade.com.pem"
ssl.ca-file = "/etc/lighttpd/ssl/gd_bundle.crt"
server.name = "web.adblade.com"
server.document-root = "/home/admin/httpdocs"
}


Starting
lighttpd -f /etc/lighttpd/lighttpd.conf


php as fastcgi
lighttpd.conf
fastcgi.server = ( ".php" =>
    ( "localhost" =>
        (
            "socket" => "/tmp/php-fastcgi.socket",
            "bin-path" => "/usr/local/php-fcgi/bin/php"
        )
    )
)


tunings on fastcgi
# theory 1 - 2 lighttpd processes, many php children. reason for 2 processes is for redundancy. If one process dies, all php children die too.
fastcgi.server             = ( ".php" =>
( "localhost" =>
(                                    
"socket" => "/var/run/lighttpd/php-fastcgi.socket",                                    
"bin-path" => "/usr/local/bin/php-cgi",
"min-procs" => 2,
"max-procs" => 2,
"kill-signal" => 9,
"idle-timeout" => 30,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "1000"
))))

# theory 2 - more lighttpd processes, 1 php child per proc
fastcgi.server             = ( ".php" =>
( "localhost" =>
(                                    
"socket" => "/var/run/lighttpd/php-fastcgi.socket",                                    
"bin-path" => "/usr/local/bin/php-cgi",
"min-procs" => 25,
"max-procs" => 50,
"kill-signal" => 9,
"idle-timeout" => 30,
)))


php (lighttpd 1.5+)
Run a fastcgi background process, and use mod_proxy to connect to it.
 /usr/bin/spawn-fcgi -s /tmp/php-fastcgi.sock -f /usr/bin/php-cgi -uwww -g www -C 5 -P /var/run/spawn-fcgi.pid


lighttpd.conf
server.modules += ( "mod_proxy_backend_fastcgi" )
$HTTP["url"] =~ "\.php$" {
       proxy-core.balancer = "round-robin"
       proxy-core.allow-x-sendfile = "enable"
#       proxy-core.check-local = "enable"
       proxy-core.protocol = "fastcgi"
       proxy-core.backends = ( "unix:/tmp/php-fastcgi.sock" )
       proxy-core.max-pool-size = 16
}



Tomcat (lighttpd 1.5+)
lighttpd.conf
server.modules  += ( "mod_proxy_core",  "mod_proxy_backend_ajp13" )
$HTTP["url"] =~ "^/(tomcat|xwiki)" {
  proxy-core.balancer = "round-robin"
  proxy-core.protocol = "ajp13"
  proxy-core.backends = ( "localhost:8009" )
  proxy-core.max-pool-size = 16
}


Redirect
   $HTTP["url"] !~ "^/admin" {
		url.redirect = ( "(.*)" => "www.mentalhelp.net$1" )
   }


		   $HTTP["querystring"] == "page=home" {
				$HTTP["url"] =~ "^/home.html" {
						url.redirect = ( "^/(.*)" => "/index.html" )
				}
		   }


http proxy
$HTTP["host"] =~ "www\.foobar\.com" {
		# proxy everything except images
		$HTTP["url"] !~ "^/images" {
		# If no file extension is needed, put "" as the first argument
			proxy.server = ( "" =>
			( (
					"host" => "127.0.0.1",
					"port" => 18080
			) )
			)
		}
		server.document-root = "/usr/local/jira/atlassian-jira"
		accesslog.filename = "/var/log/lighttpd/jira.log"
}


	proxy.server = ( ".jsp" =>
					   ( ( 
						   "host" => "10.0.0.242",
						   "port" => 81
						 ) )
					 )


simple vhost
$HTTP["host"] =~ "domain1\.com" {
			server.document-root = "/home/lighttpd/domain1.com/http"
			accesslog.filename         = "/home/lighttpd/domain1.com/logs/access.log"
}


evhost vhost
evhost is a very powerful vhost configuration tool. if one doesn't wrap the evhost directive around with a socket match, it will serve request for 0.0.0.0:*. Not a problem if all the sites are ran on the same IP and no SSL sites are served. Otherwise, the following example uses evhosts in conjunction with simple vhost to serve plain and secure sites:

$SERVER["socket"] == "0.0.0.0:80" { 
	evhost.path-pattern        = "/home/sites/www.%0/web/"
}

$SERVER["socket"] == "1.2.3.4:443" { 
	ssl.engine                  = "enable" 
	ssl.pemfile                 = "/home/sites/www.domain.tld.pem" 
	server.document-root        = "/home/sites/www.domain.tld/web" 
}


evhost variable:
%0 => domain name + tld
%1 => tld
%2 => domain name without tld
%3 => subdomain 1 name
%4 => subdomain 2 name


Lighttpd init script

#!/bin/sh
#
# lighttpd     Startup script for the lighttpd server
#
# chkconfig: - 85 15
# description: Lightning fast webserver with light system requirements
#
# processname: lighttpd
# config: /etc/lighttpd/lighttpd.conf
# config: /etc/sysconfig/lighttpd
# pidfile: /var/run/lighttpd.pid
#
# Note: pidfile is assumed to be created
# by lighttpd (config: server.pid-file).
# If not, uncomment 'pidof' line.

# Source function library
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/lighttpd ]; then
    . /etc/sysconfig/lighttpd
fi

if [ -z "$LIGHTTPD_CONF_PATH" ]; then
    LIGHTTPD_CONF_PATH="/etc/lighttpd/lighttpd.conf"
fi

prog="lighttpd"
lighttpd="/usr/sbin/lighttpd"
RETVAL=0

start() {
    echo "Starting fcgi server"
    spawn-fcgi -s /tmp/php-fastcgi.sock -f /usr/bin/php-cgi -ulighttpd -glighttpd -C3 -P /tmp/spawn-fcgi.pid
    echo -n $"Starting $prog: "
    daemon $lighttpd -f $LIGHTTPD_CONF_PATH
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
    return $RETVAL
}

stop() {
    echo "Stopping fcgi server"
    killproc -p /tmp/spawn-fcgi.pid
    echo -n $"Stopping $prog: "
    killproc -p /var/run/lighttpd.pid
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
    return $RETVAL
}

reload() {
    echo -n $"Reloading $prog: "
    killproc $lighttpd -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
            stop
            start
        fi
        ;;
    reload)
        reload
        ;;
    status)
        status $lighttpd
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart|reload|status}"
        RETVAL=1
esac

exit $RETVAL


Rewrite

Interesting pass through example. This also demonstrated how different lighttpd's rewrite is than apache's

  url.rewrite-once = (
   "^/images/.*$" => "$0",
   "^/dispatch.php.*$" => "$0",
   "^/sitemap.xml" => "$0",  
   "^/([^/]+)/([^/]+)/([^/]+)/?$" => "/dispatch.php?arg1=$1&arg2=$2&arg3=$3",
   "^/([^/]+)/([^/]+)/?$" => "/dispatch.php?arg1=$1&arg2=$2",
   "^/([^/]+)/?$" => "/dispatch.php?arg1=$1"
  )

There is one comment on this page. [Display comment]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki