After spending countless hours trying to optimise Apache2.2 to run with the smallest memory footprint possible, I decided to give lighttpd a try. This is my 'hack guide' to convert your server from Apache to Lighttpd.
I'm glad I gave it a try, there seems to be much more memory kicking around now, no exact figures but you can find benchmarks elsewhere.
Installing Lighttpd
I used Yum for this. I'm coming around to the idea of managed packages rather than compiling everything from source. Unfortunately Lighttpd is not in the default repository, so you'll need to add RPM Forge;
1 |
rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
|
Note: that RPM is for i386 on CentOS5 (or RHEL equivelent) retrieved on 26/07/2009. Check https://rpmrepo.org/RPMforge/Using for a more up-to-date URL.
Install using YUM. Dont forget the fastcgi bit if you want to use PHP or similar.
1 |
yum install lighttpd lighttpd-fastcgi
|
Move your htdocs to the correct directory (or change the www directory to the old htdocs);
1 |
mv /usr/local/apache/htdocs/* /srv/www/
|
Add lighttpd to startup, i.e. init.d scripts. CentOS does this with;
1 |
chkconfig lighttpd on
|
Remove Apache2 startup scripts:
1 |
chkconfig apachectl off
|
Note: This did not work correctly for me so I had to remove apachectrl and relevant links from init.d (rc*.d/ etc) manually.
If you are just hosting static HTML pages, everything is finished! Stop apache2 and start Lighttpd and away you go;
1 2 |
/etc/init.d/apachectrl stop /etc/init.d/lighttpd start |
What if you are running PHP and mySQL?
This bit can be a little tricky if you have just been hacking away like I did.
You can probably leave your mySQL server install alone.
For PHP to work you'll need to run it as a fast cgi module (cgi-fcgi), not cli. To find out if you have this already,use the '-v' flag;
1 2 |
php -v php-cgi -v |
If either of these show something like this...
PHP 5.2.8 (cgi-fcgi) (built: Jul 25 2009 21:58:49)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
...then you're cooking on gas. Otherwise, I found it easier to recompile PHP. You can use YUM but I had problems with mySQL compatibility and dependencies and things. If you already have fcgi, skip down to the editing the config file part...
Recompile
Before starting to recompile, you'll want the new PHP to be compatible with everything in the old one. Create a test php file and enter the following;
1 2 3 |
<?php phpinfo(); ?> |
Open the page USING APACHE (or just echoing it into PHP on the CLI and grepping - if you know how/would prefer to do it go for it, I'm not going to discuss it here) at the top there will be the 'config string' Copy this down.
Remove all flags in the config line that contain 'aspx' or 'aspx2' or similar. These are Apache specific and will compile PHP as an apache module. Insert the following flags;
'--enable-fastcgi' '--enable-force-cgi-redirect'
They will cause it to compile as a FastCGI module.
Run ./configure with the new configure line. Then make, and make install (last one as root).
Once thats done, check where PHP has installed itself. I ended up with a couple of php's in a couple of directories (I hack around too much for my own good). The '-v' flag will come in handy here again. My php-cgi of the correct version was located in /usr/local/bin/php-cgi
Editing the configs for PHP
Firstly you'll need to fiddle with php.ini to put in a fix. php.ini may be located at either /etc/php.ini or /usr/local/lib/php.ini - if anyone knows why please tell me.
Add this to php.ini;
cgi.fix_pathinfo = 1
Then edit /etc/lighttpd/lighttpd.conf;
- Uncomment the mod_fastcgi server module
- Uncomment all of the fastcgi.server section
In the fastcgi.server section, ensure the php-cgi binary path matches the binary that you used the '-v' on earlier. If you just entered the binary name at the prompt then use 'whereis php-cgi' to find out where it resides.
Also make sure the location where the socket resides ("socket" => "/var/run/lighttpd/php-fastcgi.socket") definitely exists! If in doubt, run the following to make sure.
1 |
mkdir -p /var/run/lighttpd/
|
Save all that and exit.
Now run the server and see if it works. Mine does!
1 |
/etc/init.d/lighttpd start
|
Adding Virtual Hosts
Virtual hosts work in the same way as apache. There is also a 'simplehosts' feature that I havent really looked in to. The way shown here seems to be the most common way.
Edit /etc/lighttpd/lighttpd.conf, add the following;
1 2 3 4 5 6 |
$HTTP["host"] =~ "www.domain.com$" { server.document-root = "/srv/www/htdocs" server.errorlog = "/var/log/lighttpd/error_domain.log" accesslog.filename = "/var/log/lighttpd/access_domain.log" } |
That looks a bit nicer than Apache's way of doing things. I'm sure you can work out what it all means. Note that the hostname can be a regular expression! Also note that if you are using a non-default port number, the port will be appended to the hostname (domain.com:8080). The following will catch any subdomain, .com and .co.uk TLDs and any port for example;
"(^|.)domain.co(m|(.uk))($|:)"
Rember to restart lighttpd after you fiddle with the config;
1 |
/etc/init.d/lighttpd restart
|





