While a large proportion of the world's webservers are currently using Apache, a competitor has been steadily gaining popularity on many high-usage sites.
Lighttpd, pronounced "lighty", is a small-footprint, high-speed webserver, and is notably used by sites such as
Sourceforge,
YouTube and
MiniNova. Netcraft state that Lighttpd is currently being used on
1.38 million sites, and is steadily gaining on Sun's share of the market. This introductory article provides a guide to getting Lighttpd installed and configured.
Configuration file
We'll set up a very basic configuration file, enough to get lighttpd serving just static webpages. Firstly, we'll enable the accesslog module, to provide logging:
server.modules = ( "mod_accesslog" )
Modules are optional extensions to the webserver, which are loaded at start-up time, and are specified in a comma-separated list to the
server.modules directive. It's best to only load the modules needed, to reduce both the memory footprint and the number of potential security issues.
In order to bind to ports below 1024 (including the default webserver port 80), lighttpd will need to be started as root. For security reasons, however, we don't want the server to continue with root privileges once it has bound to the port. Here, we'll set the webserver to run as the user
nobody, with a group of
nogroup.
server.username = "nobody"
server.groupname = "nogroup"
In the long run, however, it would be better to create a separate user and group to run the webserver (for example,
www), rather than using
nobody.
Now, we'll tell the webserver which directories we wish to use for the document root (where our webpages are stored), and where access and error logs should go:
server.document-root = "/usr/local/lighttpd/www/"
accesslog.filename = "/usr/local/lighttpd/logs/access.log"
server.errorlog = "/usr/local/lighttpd/logs/error.log"
Here, we specify a list of files that will be the default page displayed if a directory is viewed. The files are listed in a descending order of priority; in this case, if a directory contains both an index.html and an index.htm file, it will be the index.html file that is displayed by default:
index-file.names = ( "index.html", "index.htm" )
Now, set the port on which the webserver listens:
server.port = 80
Although this webserver isn't yet configured to run any sort of fast-cgi processes, we will take a precaution and stop it from serving up the source code of any php, perl or fcgi scripts that we may put in our document root in the future:
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
Finally, we'll assign mappings of filename extensions to mime-types. These are used to inform web clients of the type of file they will be downloading, so they can spawn it off to the correct program, if necessary. The list of mime-types is too long to include here in full, so I've just chosen a select few to provide an idea of the syntax. Download the full configuration file below for more.
mimetype.assign = (
".pdf" => "application/pdf",
".mp3" => "audio/mpeg",
".ogg" => "application/ogg",
".gif" => "image/gif",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
".html" => "text/html",
".htm" => "text/html",
".text" => "text/plain",
".txt" => "text/plain",
".dtd" => "text/xml",
".xml" => "text/xml",
".mpeg" => "video/mpeg",
".mpg" => "video/mpeg",
)
You can download the full configuration file here:
lighttpd.conf. Put in in
/usr/local/lighttpd/etc/.
Start it up with:
/usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/etc/lighttpd.conf
There's no special method for shutting the webserver down, so just killing the process will suffice. Rather than having to run
ps every time to find the process ID, it's best to configure the server to store it in a file when it starts:
server.pid-file = "/usr/local/lighttpd/var/lighttpd.pid"
Add the above to the configuration file, kill and restart the server and from then on, the webserver can be stopped in one step with:
kill `cat /usr/local/lighttpd/var/lighttpd.pid`
If you found this article helpful, consider making a donation to offset the costs of running this server, to one of these addresses: