Installing lighttpd on Ubuntu 10.04

DEPRECATED: This post has been marked as deprecated and may no longer contain industry best-practices.

On a VPS with 512 MB of RAM, installing a LAMP stack (Linux, Apache, MySQL, PHP) and the trimmings (eaccelerator @ 40MB, memcached @ 40MB) eats up a LOT of RAM (in my particular case, about 480 MB). In an attempt to bring the amount of RAM usage down, I was looking through the results of the top command. To my surprise, Apache was eating up 11 processes, which equaled about 1/3 of the total RAM! It was this servers biggest overhead, and seemingly did the least important work (PHP and MySQL are the heavy lifters).

So, I started looking into lighttpd, which is a super fast / lightweight Linux server. The configuration files are a bit easier to understand as well. lighttpd only uses one process, while Apache uses a new process for each request. lighttpd lacks some of the features of Apache (none of which I apparently use because I couldn't find said features). One of the major drawbacks is that it doesn't use per-directory configuration files (.htaccess files) which means if you want to reconfigure something, you need to change the main server config file and do a restart (which isn't a big deal, lighttpd restart = 0.25 seconds, Apache restart = 10 seconds). This means Apache is better for multi site environments, and shared hosts, while lighttpd is better for single application servers with one owner.

Anyway, here are the commands to get lighttpd up and running with PHP. It uses Fast CGI to run PHP.

sudo apt-get install lighttpd php5-cgi
sudo lighttpd-enable-mod fastcgi
sudo /etc/init.d/lighttpd reload

The first command installs lighttpd and PHP's CGI package. The second command is a lighttpd configuration app which enables fastcgi. The third command restarts the server.

Once you've got this installed and setup, you'll need to do some server configurations. The PHP configuration file (if you've been using Apache) is now located in a new directory (/etc/php5/cgi/php.ini) and you may need to migrate some settings over. The lighttpd configuration file is of course in a different directory than Apache's (/etc/lighttpd/lighttpd.conf).

Thomas Hunter II Avatar

Thomas is the author of Advanced Microservices and is a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.