The Lighttpd is a security, speed, lightweight and flexibility alternative to Apache HTTPD, and it is designed and optimised for high-traffic websites. It has a small memory footprint, can manage system load very effectively. Furthermore, it has advanced features to address FastCGI, SCGI, Auth, URL-Rewriting and so on. The best of best is that Lighttpd is open source, totally free.
If you have Linux server set up with default configuration as the distribution, you will see the Apache HTTPD is the default web server, because it is great for most websites. However, it you have an extremely high traffic and load sites, Apache HTTPD can run slower than Lighttpd, and Nginx. Today we will focus on Lighttpd, if you would have a look at Nginx, please check this post.
Lighttpd is specifically designed to servers which have minimal or limited memory footprint and maximise its productivity. It is specially designed for serving up static content, working very well across multiple servers, and can be integrated with FastCGI.
How to install Lighttpd?
To set up lighttpd, it is very simple and straightforward. In most Linux distributions, the package name should be Lighttpd, no matter you are using Debian/Ubuntu/CentOS/RHEL.
Debin/Ubuntu $ sudo apt-get install lighttpd RHEL/CentOS # yum install lighttpd
If you would like to have the most up to date version, it is possible to download the source package from http://www.lighttpd.net, at this moment it is 1.4.35.
If you already have another web server running on TCP port 80, such as Apache HTTPD. Please switch it off before start lighttpd.
# /etc/init.d/httpd stop
If you would like to set up Lighttpd for test only without shutdown Apache HTTPD, we can absolutely change the default TCP port from 80 to another one by editing the server.port line in /etc/lighttpd/lighttpd.conf, for example:
server.port = 8000
Now we can start lighttpd by running /etc/init.d/lighttpd start. Go to http://localhost:8000 and you should see a basic placeholders page. Let’s take a look the default configuration:
Server.modules = ( “mod_access”, “mod_alias”, “mod_accesslog”, “mod_compress”, ) Server.document-root = “/var/www/” Server.port = 8000 Server.errorlog = “/var/log/lighttpd/error.log” Accesslog.filename = “/var/log/lighttpd/access.log” Index-file.names = (“index.php”,”index.html”,”index.htm”,”default.htm”) url.access-deny = (“~”, “.inc”) static-file.exclude-extensions = (“.php”,”.pl”,”.fcgi”) include_shell “/usr/share/lighttpd/create-mime.assign.pl”
Bear in mind that the default configuration won’t handle any dynamic content, since it has not loaded and configured external modules. Meanwhile, we do not want to display source file as a security problem.
If you have any updates in the configuration file, remember to perform test before restart lighttpd
lighttpd -t -f /etc/lighttpd/lighttpd.conf
FastCGI and PHP
The Lighthttpd is very good at dealing request for static content, but we also have PHP dynamic pages. Lighthttpd can do a very good job at that as well, and specifically working with the FastCGI protocol. You can use php-fpm, or Apache mod_fastcgi. To confirm if we have mod_fastcgi, check the directory /etc/lighttpd/mods-available/, and then enable it by using
# /usr/sbin/lighty-enable-mod # /etc/init.d/lighttpd force-reload
We will be given a list of available modules and able to choose which to enable; then we can reload the server to load all changes in.
If you are using php-cgi, you will need to edit the main configuration file /etc/php5/cgi/php.ini, to add this line at the end:
cgi.fix_pathinfo = 1
And then restart lighttpd again, let’s say we have phpinfo.php file, the go to http://localhost:8000/phpinfo.php, you will see PHP support via FastCGI, awesome!