Master lighttpd – the lightweight alternative to Apache
Is your Apache web server too slow? Koen Vervloesem shows you how to use lighttpd instead. This lightweight web server alternative is perfect for virtual private servers and ageing hardware solutions…
Apache is obviously the most popular open source web server, and it’s a good one too. But in some circumstances it really pays off to look at alternatives. For instance, when you rent a virtual private server with not much RAM, you’ll soon meet the limits of Apache. By default, this web server is quite resource-hungry and although there are ways to let it use less memory, tuning Apache is something of a dark art.
So if you don’t have the time to fine-tune Apache on your low-end VPS, why not try another web server? lighttpd (pronounced ‘lighty’) is a promising alternative, because its developers are focusing on performance and simplicity. Moreover, most of the functionality you are used to in Apache – such as modules, URL rewriting and support for CGI and FastCGI – is also available in lighttpd.
The main difference between the two web servers is simple: Apache creates a separate process for each request, whereas lighttpd is able to process multiple requests inside one process. That’s why the latter uses less RAM, as well as less processor time. And that’s why lighttpd can be a better choice than Apache when your server has a small amount of RAM or has to handle large amounts of requests. The latter situation explains why lighttpd is used by popular websites such as Wikipedia and YouTube, and the former situation is why you could benefit from using lighttpd on your VPS to host your blog.
Installing and configuring lighttpd
lighttpd is available in the repositories of most mainstream Linux distributions. Just install the lighttpd package and maybe some lighttpd-mod packages for extra modules. After starting the web server manually with ‘/etc/init.d/lighttpd start’, it normally starts serving the webpages in /var/www or (in openSUSE) /srv/www/htdocs. For this article, we used Debian Lenny (5.0).
All configuration of lighttpd is done in the file /etc/lighttpd/lighttpd.conf. The syntax is simple: each line has a variable name, an equals sign and the value of the variable. Strings and Boolean values are surrounded by double quotes; numbers are not. A list is surrounded by parentheses and uses commas to separate its elements.
Some basic variables you have to define are the document root and the port that lighttpd is listening to:
[sourcecode language=”bash”]server.document-root = “/var/www/”
server.port = 80
You’ll also want to define the location of the error log and access log (which needs the mod_accesslog module, which is enabled by default):
[sourcecode language=”bash”]server.errorlog = “/var/log/lighttpd/error.log”
accesslog.filename = “/var/log/lighttpd/access.log”
If you don’t want to show a directory listing in directories without an index file, add the following line:
[sourcecode language=”bash”]dir-listing.activate = “disable”
Now after you have changed something in the configuration file, you have to tell lighttpd to reload it:
[sourcecode language=”bash”]/etc/init.d/lighttpd force-reload
It’s also a good idea to test the syntax of the configuration file first:
[sourcecode language=”bash”]lighttpd -t -f /etc/lighttpd/lighttpd.conf
Continue to page 2…