Lighttpd Performance

Lighttpd (often called Lighty) is a small and popular web server. Some large web applications are using lighttpd to serve their content (youtube, imageshark.us, myspace). The opponent of lighty is apache2 which is process based web server. But the big advantage of lighty is the thread model that makes the handling of the request really slim. This article would give short introduction into speeding up lighty. This article is a part of the web-application performance series.

Maximize the number of Connections

Lighty comes out with maximal 1024 open file descriptors by default. You can modify this in the configuration file lighttpd.conf. For running the server in a production environment with heavy load you have to increase this value. Set it to 2048 or 4096 (depends on your hardware). Be aware, that a simple PHP request needs 3 file descriptors in lighttpd. Now, you can calculate how many file descriptors do you need to serve your load. The 3 file descriptors are used for handle-TCP/IP-with-user, socket-to-FastCGI and Check-whether-a-file-exists.

#number of open file descriptors
server.max-fds = 2048

stat_cache

Lighty has an intelligent stat handling. The stat command is used to get file system information from a specific file. The stat_cache caches this requests to avoid an unnecessary number of stat commands an the associated accesses to the hard drive. Another option to handle something like stat() is FAM (file alternation monitor). FAM comes out with a deamon, that monitors all files. You can enable the stat_cache with the following line in you configuration file:

#enable the simple stat_cache
server.stat-cache-engine = "simple"
#or with a running fam deamon
server.stat-cache-engine  = "fam"

Keep-Alive vs. Close

If you are running a web application with high concurrency you have to consider about keep-alive requests. It’s not good to keep your file descriptors alive and let them idle. When you do so, you waste a lot of resources. It would be a huge performance improvement to set server.max-keep-alive-requests to 0 and avoid unused file descriptors and threads.

server.max-keep-alive-requests = 0

Using XCache

XCache is a opcode cache coded by the lighty labs. It improves the performance of Lighttp+PHP by caching the opcode into the SHM (shared memory segment). So, the interpreted PHP file comes directly from the RAM. This is approx. more than 5 times faster than a simple PHP request on lighttpd.

Important configuration settings are xcache.ttl, xcache.size, xcache.cacher and xcache.optimizer. Xcache.ttl is the time to life for the cached op-code file, before it would be marked as invalid. If this value was set to 0, then the time to life is endless and the file would never be marked as invalid. The xcache.size option points out the maximum allowed size of a cache file (memory mapped file). To turn the cache on or off you can use the xcache.cacher directive with “on” or “off”.

Below you can see an example:

xcache.shm_scheme = 		"mmap"
xcache.mmap_path = 		"/var/cache/xcache.mmap"
xcache.size  = 			64M
xcache.count = 			1
xcache.ttl   = 			0
xcache.slots = 			8K
xcache.gc_interval = 		0
xcache.readonly_protection = 	Off
xcache.cacher = 		Off
xcache.var_gc_interval = 	300
xcache.stat = 			On
xcache.optimizer =		On
xcache.var_count =             	1
xcache.var_size  =            	0M
xcache.var_slots =            	8K
xcache.var_ttl   =             	0
xcache.var_maxttl   =          	0

Mod_compress

This module is used to compress files with gzip, bzip2 and deflate. Compressing files reduces your bandwith and the throughput. Be aware, that any compression needs time and cpu load. To save cpu time, the results of the compression can be cached. To do so, mod_compress needs a few configurations:

compress.allowed-encodings = ("bzip2", "gzip", "deflate")
# you have to create the cache directory! the web server
# is not able to do that for you
compress.cache-dir = "/var/www/cache/"
#add all filetype you need pictures, php files etc.
compress.filetype           = ("text/plain", "text/html")

For more infos check the manual.

Mod_expire

Mod_expire is used to control the cache headers of lighttpd. To cache your static files, this module is all you need. It excepts the following directives:

expire.url = ( "/images/" => "access plus 20 days" )

To learn more about the syntax and how to use this module check out the manual.

5 thoughts on “Lighttpd Performance

Leave a Reply

Your email address will not be published. Required fields are marked *