Lighttpd, affectionately known as Lighty is an extremely efficient and lightweight web server. I run Apache 2.2 on all my servers, but have been using Lighttpd for a couple years now to stream video files for a popular web site I run. Lighttpd has a flv streaming module which can handle more advanced streaming than the usual progressive http streaming. In fact, I hear YouTube uses Lighttpd for their video streaming, so it’s pretty rock solid. Lighttpd also excels at serving static content and setting up an assets server running Lighttpd is one way to take some of the burden off of Apache and dramatically increase the load time of a popular web site.
So, on to configuring Lighttpd. First we need to download and extract it.
cd /usr/local/src wget http://www.lighttpd.net/download/lighttpd-1.4.20.tar.gz tar -xzf lighttpd-1.4.20.tar.gz cd lighttpd-1.4.20
Now, before we configure Lighttpd we need to install a few dependencies, otherwise it won’t configure.
yum install pcre-devel zlib-devel bzip2-devel
Now with those installed, we can configure, make and install Lighttpd
./configure make make install
If all goes according to plan, Lighttpd should be installed on your system. Now we need to edit the configuration file. In the doc directory is a default configuration file which we can edit for our needs. So lets make a copy of that and put it where it will reside on the server.
cd doc mkdir /etc/lighttpd cp -rfp lighttpd.conf /etc/lighttpd/.
So now we have a copy of the default configuration file in /etc/lighttpd/. Lets also move a copy of the startup script. There are a couple rc.* files in the doc directory. For my system I used the rc.lighttd.redhat version and moved it to my servers init.d directory.
cp -rfp rc.lighttpd.redhat /etc/rc.d/init.d/lighttpd
So now we have some editing to do. Open up the official copy of lighttpd.conf in your favorite editor. I prefer emacs.
emacs /etc/lighttpd/lighttpd.conf
Under server modules you will see all the default modules Lighttpd comes with. Most of these will be commented out using a #. For my assets and flvstreaming server I uncommented mod_status (provides server status information at domain.com/server-status), mod_compress (allows for compression of static files which can save bandwidth and speed up serving), and mod_expire (allows for setting expire headers of cachable assets to save bandwidth and speed up serving). By default, mod_access and mod_accesslog should be enabled.
Now under the server modules area I add my customizations. You may to add your own according to your server’s purpose and capabilities.
# extension of flv files flv-streaming.extensions = ( ".flv" ) # aggressive timings for performance. server.max-keep-alive-requests = 6 server.max-keep-alive-idle = 15 server.max-read-idle = 15 server.max-write-idle = 15
Next, we should set up the modules we configured. In order for mod_status to work we need to find this line and uncomment it like so:
status.status-url = "/server-status"
Now you might not want to make this URL publicly accessible, in which case you can wrap it in a block like so, replacing the IP address with the IP of the computer you are accessing the page from:
$HTTP["remoteip"] == "192.168.0.1" {
status.status-url = "/server-status"
}
Next, let’s configure mod_compress. Search for compress.cache-dir. Here are the mod_compress settings I am using:
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ("text/plain", "text/html", "text/css", "image/png", "text/javascript", "image/jpg", "image/gif")
You will also need to create the cache directory for the mod_compress to do it’s business. So back in the shell you will want to type.
mkdir /var/cache/lighttpd mkdir /var/cache/lighttpd/compress
Now, I am telling lighttpd to compress plain text, html, css, javascript, as well as various image formats. Performance gains compressing images is debatable since images are often compressed by default and the cpu cycles used to compress images often outweighs the bandwidth improvement. By the way, when we talk about compression under mod_compress, the actual files the browsers receive are not impacted. mod_compress simply compresses the file for transfer and the receiving browers uncompresses back to it’s original size. So degradation due to image compression is not an issue.
Next, let’s set up the expire module, mod_expire. This allows you to configure lighttpd to set an expire header on certain files or directories. Since I organize all my files in folders like /images /stylesheets, /javascripts it’s pretty easy to tell lighttpd which files I want to set expire headers. You can search for expire module and add this down there, but I prefer to add it higher up.
$HTTP["url"] =~ "^/images/" {
expire.url = ( "" => "access 240 hours" )
}
$HTTP["url"] =~ "^/javascripts/" {
expire.url = ( "" => "access 240 hours" )
}
$HTTP["url"] =~ "^/stylesheets/" {
expire.url = ( "" => "access 240 hours" )
}
This tells lighttpd to assign a expire header of 240 hours to files served from these directories. This will often save the browser from making additional requests for these files to check to see if they have changed. It can save a lot of overhead and bandwidth on a popular web site.
Lastly, we need to set up our host information. Again I like to place this in the upper part of the configuration file beneath my server.* configurations.
$HTTP["host"] == "flvstream.domain.com" {
server.document-root = "/home/lighttpd/flv_files/"
}
And if I want to create another host for assets, I’d just create it just below the first.
$HTTP["host"] == "assets.domain.com" {
server.document-root = "/home/lighttpd/flv_files/"
}
Now, with these two hosts set up, you can comment out this line since it’s not needed for our multi-host setup:
#server.document-root = "/srv/www/htdocs/"
Now, we are just about ready to start up our server. So save this file and exit. Lighttpd will log access to the /var/log/lighttpd/directory. Let’s create that:
mkdir /var/log/lighttpd
Alright, so we are all set. If you have your files in place go ahead and start up lighttpd.
/etc/rc.d/init.d/lighttpd start
If all goes according to plan it should start up and you can call up your host in the browser. If you get an error message, Lighttpd is pretty clear about what went wrong and will give you some tips or guide you to what line in the conf file is out of whack.
Now if you are running lighttpd publicly, you will want to run it as an unprilegdged user unless you like to be hacked and cracked. So in the shell lets add a lighttpd user and group.
groupadd lighttpd useradd -g lighttpd -d /home/lighttpd -s /sbin/nologin lighttpd
Now lets edit lighttpd.conf and tell lighttpd to run under this user. Uncomment and set the following two lines.
server.username = "lighttpd" server.groupname = "lighttpd"
Any files that Lighttpd reads or writes to will need to be owned by Lighttpd. So your web files, log files, mod_compress directory, etc. Once you are set, you can start Lighttpd.
This concludes this quick and dirty guide to compiling and configuring Lighttpd for use as a high performance assets server. There is a lot more to Lighttpd, so I encourage you to read around. This is just a quick-start guide to configuring a relatively high performance assets and flv streaming server under Lighttd. I have noticed a 8 – 10X increase in performace under Lighttpd over Apache, while using only a fraction of the memory and CPU usage. So Lighttpd lives up to it’s name.
Some Lighttpd resource: