apache2 vs lighttpd - take a second look

apache2 vs lighttpd - take a second look

by Justin Haaga -
Number of replies: 19
Hi Everyone,

For those of you in a linux configuration you ought to take a serious look at lighttpd for your php5/moodle configuration. In my testing there is over a 50% performance gain with lighttpd over apache. I'd thought I would share my configuration for my moodle setup using debian lenny or ubuntu 8.0.4 LTS.

you can view the entire post on my blog - http://justinhaaga.wordpress.com/2009/06/18/apache2-fast-or-slow-try-lighttpd/

First, install the server (stop apache services if running or change to different port)

apt-get install lighttpd
apt-get install php5-cgi

Second, enable php

nano /etc/php5/cgi/php.ini
cgi.fix_path_info = 1
upload_max_filesize = 100M
post_max_size = 100

Third, enable php in lighthttpd

nano /etc/lighthttpd/lighthttpd.conf
nano /etc/lighthttpd/lighthttpd.conf
server.modules = (
“mod_access”,
“mod_alias”,
“mod_accesslog”,
“mod_compress”,
“mod_fastcgi
)

#add “mod_fastcgi” to the top as shown

##tweaks (depends on memory and CPU) so run some tests – max-proc and min-proc #specify how many php processes to spawn, make sure you adequately assign #according to memory, default is 2

server.max-connections = 250
server.max-fds = 625

fastcgi.server = ( “.php” => ((
“bin-path” => “/usr/bin/php5-cgi”,
“socket” => “/tmp/php.socket”,
“max-procs” => 4,
“min-procs” => 4,
“bin-environment” => (
“PHP_FCGI_CHILDREN” => “16″,
“PHP_FCGI_MAX_REQUESTS” => “10000″
),
“bin-copy-environment” => (
“PATH”, “SHELL”, “USER”
),
“broken-scriptfilename” => “enable”
)))

reset lighthttpd
/etc/init.d/lightttpd

DONE-now enjoy! default web root is /var/www

Extra-Need a SSL Cert? Here’s the config:

1. generate ssl cert using openssl (see other docs on the net)
2. mkdir /sslcerts cd /sslcerts
3. cat your.key your.crt > lms.pem
4. nano /etc/lighthttpd/lighthttpd.conf
$SERVER["socket"] == “10.10.13.161:443″ {
ssl.engine = “enable”
ssl.pemfile = “/sslcerts/.your.pem”
ssl.ca-file = “/sslcerts/your.crt”
server.name = “lms”
server.document-root = “/var/websites/lms/www”
server.errorlog = “/var/log/lighttpd/lmsserror.log”
accesslog.filename = “/var/log/lighttpd/lmsaccess.log”
}


Toggle on and off your apache and lighttpd (in a dev environment of course) and run through some apache bench tests as well as real time tests, you should find conclusion evidence that it's much faster. Full php configuration is supported so you will not need to add or change anything else other than above.

Average of ratings: Useful (1)
In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by James McLean -
Interesting result, but what was your performance like for HTTP rather than HTTPS? Not that it would be slower, but I'm interested in seeing the comparison.

What's your database?
What version of Moodle?
What was the exact 'ab' command line you used?
In reply to James McLean

Re: apache2 vs lighttpd - take a second look

by Justin Haaga -
That's a good question. I found narrow differences with http and https on both apache and lighttpd.

DB is a dedicated (virtulized on esx 3.5) mysql 5.0 on a ubuntu 8.0.4 linux server, 2GB memory, 2 xeon @ 2ghz ea.

Moodle version 1.9.3

ab is apachebench tool used to simulate concurrent connection and requests. It's not perfect as it's hard to simulate the different load sequence in moodle because each page may compile differently. However, it does push the webservers and while pushing it I browse the page to get a feel for the speed. What some of these numbers don't show is how lighttpd handles a large influx of requets in a much more efficient way. This is due to the different method of handling request vs apache.

Apache has it's place especially if you want to use the .htaccess (which lighttpd doesn't even have), however for a moodle configuration lighttpd is ideal and I cannot find a downside to it as everything about is pro.

Lighttpd uses fastcgi to tie into php5, so all of the php5 extensions are support just the same as they are in apache.

If you are having any type of load problems you ought to look seriously into this webserver. There isn't much documentation out there on lighttpd, any even less on moodle+lighttpd, but hopefully this will expose it to more people and give them a chance to test it on their own and reap the same benifits as I have.
In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by John Bonner -
I read your posting on this topic and had been trying to get php5 to work on a Buffalo Linkstation (NAS) with a modifed (Freelink) firmware based on Debian etch (arm).
I couldn't get php5 to work with apache 1.3.7 or 2.2 (though this should be possible?)
The server modules setting is:
server.modules = (
"mod_access",
"mod_alias",
"mod_accesslog",
# "mod_rewrite",
# "mod_redirect",
# "mod_status",
# "mod_evhost",
# "mod_compress",
# "mod_usertrack",
# "mod_rrdtool",
# "mod_webdav",
# "mod_expire",
# "mod_flv_streaming",
# "mod_evasive"
)

AND I added the following to the end of the file: (Must have done something wrong)


server.max-connections = 250
server.max-fds = 625

fastcgi.server = ( “.php” => ((
“bin-path” => “/usr/bin/php5-cgi”,
“socket” => “/tmp/php.socket”,
“max-procs” => 4,
“min-procs” => 4,
“bin-environment” => (
PHP_FCGI_CHILDREN” => “16″,
“PHP_FCGI_MAX_REQUESTS” => “10000″
),
“bin-copy-environment” => (
“PATH”, “SHELL”, “USER”
),
“broken-scriptfilename” => “enable”
)))


Any ideas welcome,

JB


In reply to John Bonner

Re: apache2 vs lighttpd - take a second look

by Justin Haaga -
If you can't get php to work in apache then you'll have problems in lighttpd. Your missing a module somewhere.

Here's a list of packages I needed for moodle using apache2:
apt-get install apache2-mpm-prefork
apt-get libapache2-mod-php5 php5-cli php5-common php5-cgi
apt-get install php5-mysql
apt-get install php5-curl
apt-get install openssl
apt-get install php5-xmlrpc
apt-get install php5-gd
apt-get install php-xchache
apt-get intall php5-memache
apt-get install rsync (**probably don't need)
apt-get install ntp
apt-get install php5-ldap (** only need if you use ldap)
apt-get install php-pear (** only need if you want to do radius auth)

(radius auth)
apt-get install php5-radius
apt-get install php5-auth-pam
pear install radius Auth_RADIUS

For lighttpd just need:
apt-get install lighthttpd
apt-get install php5-cgi
(plus the bold apt-get above)

nano /etc/php5/cgi/php.ini
cgi.fix_pathinfo = 1
upload_max_filesize = 100M
post_max_size = 100M


nano /etc/lighthttpd/lighthttpd.conf
server.modules = (
"mod_access",
"mod_alias",
"mod_accesslog",
"mod_compress",
"mod_fastcgi", <<---------- your missing this in your cfg, must have
)

server.document-root = "/var/websites/lms/www/"

##tweaks
server.max-connections = 250
server.max-fds = 625


fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/bin/php5-cgi",
"socket" => "/tmp/php.socket",
"max-procs" => 12,
"min-procs" => 12,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"broken-scriptfilename" => "enable"
)))


---------------
Check your "bin-path" and make sure php5-cgi is there. If it's debian based it should, but perhaps it's debian 3.0 or 4.0? which might place it in a different spot. Try a search if you have to.

By the way, these configs work with both Debian 5.0 (lenny) and Ubuntu 8.0.4(LTS).

What's the error you get? It can't compile php at all or perhaps it's a moodle problem configuration.



In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by John Bonner -
Thanks for the advice.
First of all, I'm trying something very unusual - installing php on a Buffalo LinkStation Pro with a modified firmware "Debian Freelink Etch" (arm processor).

I was able to remove apache1 and install lighttpd.
The only file I couldn't install was "php-xchache"
I think you meant php-xcache?
Couldn't install it:

Package php5-xcache is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package php5-xcache has no installation candidate

I was curious that you suggested using "/var/websites/lms/www/" for server.document-root ?

Anyway, so near and yet so far. Just missing one file, I think?
Apache 2 can be installed but doesn't work as well as apache1. There may be missing dependencies or conflict.

JB

In reply to John Bonner

Re: apache2 vs lighttpd - take a second look

by John Bonner -
Update:
I tried installing the missing file php-xcache (not available for etch) by adding Lenny's source to /etc/sources.list and installing it. However this didn't make any difference.

So I tried Apache2 once more following your suggestion. (Lighttpd works faster than Apache1 and I had thought of keeping it) I understand it can be installed with apache if the port is different? Will report back on Apache2.

JB


In reply to John Bonner

Re: apache2 vs lighttpd - take a second look

by Justin Haaga -
you don't have to use php-xcache though it certainly will make things run faster. Since it's etch there is no prebuilt php accelerator package. You can go two routes, 1) update your source list with lenny (like you did but you may have the wrong ones). 2) compile one.

Here's a link to compile accelerator:
http://www.howtoforge.com/eaccelerator_php5_debian_etch

I wouldn't nessicarly do that yet until you got php working though, as it won't affect it from running.

Also, for your question "I was curious that you suggested using "/var/websites/lms/www/" for server.document-root ?"

We archive our lms data so we have several websites running on the same server, so we can easily append to them by doing /var/websites/lmsarchiveXX/www and so on. Just keeps in nice and clean, your default is /var/www


When you start lighttpd does it run or error out? You can see what it spits out by looking at the syslog- tail /var/log/syslog or try a tail -f /var/log/syslog as you start it Or it may also be in/var/log/lighttpd/error.log

since you are doing apache1 my instructions probably are off a little. Might want to try to focus on the lighttpd config.
In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by John Bonner -
I had some success - still using lighttpd. I couldn't open a php file using Apache 2 but Eureka! I finally managed to open one when I created it using Vi (rather than with text editor via Windows). Perhaps this would have worked in apache2 but I had other problems with Apache 2 - it wouldn't even open a html file when reloaded.

So far I managed most of the compiling of eaccelerator but had 2 errors:

1. checking for best semaphores type... configure: error: "You need to pass the user id eaccelerator will be running under when using sysvipc semaphores"

2. On restarting lighttpd (which now takes a 1-2 mins), I get this:
root@LS-GL7D6:/tmp/eaccelerator-0.9.5.3# PHP Warning: PHP Startup: Unable to lo ad dynamic library '/usr/lib/php5/20060613+lfs/eaccelerator.so' - /usr/lib/php5/ 20060613+lfs/eaccelerator.so: cannot open shared object file: No such file or di rectory in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/2006061 3+lfs/eaccelerator.so' - /usr/lib/php5/20060613+lfs/eaccelerator.so: cannot open shared object file: No such file or directory in Unknown on line 0

This error message is repeated a few times. Perhaps it's as a result of semaphores error?

JB

P.S
Sorry, I forgot to do "make" and "make install", but should I sort out the semaphores error first?

In reply to John Bonner

Re: apache2 vs lighttpd - take a second look

by John Bonner -
While delaying the make/make install of eaccelerator, I installed moodle. Of course it was very slow on the NAS and even installing took a long time. It should be a bit faster with eaccelerator enabled (but the memory is only 128MB on the Linkstation). If it's still too slow to run, at least it's been a challenge and achievement installing it.

Hoping that I can still install eaccelerator
Thanks,
JB
In reply to John Bonner

Re: apache2 vs lighttpd - take a second look

by Justin Haaga -
128mb of memory huh... ouch that is not alot. PHP can be CPU and memory intensive, to be honest, I don't think that box is going to cut it.

You can try compiling xcache-but this might actually hurt you as it stores compiled PHP info into memory, which you don't have!

http://xcache.lighttpd.net/

Also, with that low of memory I would try this for the lighttpd config:

server.max-connections = 150
server.max-fds = 255

fastcgi.server = ( “.php” => ((
“bin-path” => “/usr/bin/php5-cgi”,
“socket” => “/tmp/php.socket”,
“max-procs” => 2,
“min-procs” => 2,
“bin-environment” => (
PHP_FCGI_CHILDREN” => “16″,
PHP_FCGI_MAX_REQUESTS” => “10000″
),
“bin-copy-environment” => (
“PATH”, “SHELL”, “USER”
),
“broken-scriptfilename” => “enable”
)))



In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by John Bonner -
Thank you for your help. I won't install xcache, following your advice. The adjustments will help a little. I don't intend to use moodle on this box for memory reasons. - In fact, if I do too many operations it beeps continuously (due to memory overload, I think) and has to be rebooted.
It was an interesting challenge just to get Moodle installed on this tiny box, but there's no point in using it as I already have a school moodle installation hosted online via cpanel and have installed practice sites on my home PC and mac laptop.

Thanks again for all your valuable knowledge and help.

JB
In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by Ash Tech -
"Apache has it's place especially if you want to use the .htaccess (which lighttpd doesn't even have), however for a moodle configuration lighttpd is ideal and I cannot find a downside to it as everything about is pro."

I was all set to use lighttpd until I discovered, (as far as I can tell anyway) that it doesn't support Single Sign On using NTLM. Not a massive issue but it is a great convenience when on campus.

In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by Nguyen An Thuan -
IMO, Apache still is the best web server for PHP smile It's more reliable.
In reply to Nguyen An Thuan

Odp: Re: apache2 vs lighttpd - take a second look

by Bartosz Cisek -
Could you give some test results that proves your opinion about apache reliability?
In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I gave Lighttpd a very serious chance a while back. My trial was possibly not as scientific as it might have been but... In a simple scenario (i.e. just Moodle) lighttpd works well and seems rock solid.

I had no issues at all with PHP in CGI mode.

I came unstuck as soon as I wanted to do anything much else. Firstly, I couldn't get MNET (Moodle and Mahara) to work at all. I did get to the bottom of the problem (there's a tracker report somewhere) but it requires a small code hack. Also, the configuration files are rather arcane and limited - you will quickly find stuff that's easy in Apache but tricky (or impossible) in lighttpd.

Support is very limited as, clearly, there are not so many users.

So... good for simple installs for anything else use Apache IMHO.
In reply to Howard Miller

Re: apache2 vs lighttpd - take a second look

by Justin Haaga -
To each his own. But numbers speak, more importantly, you can feel the difference under load:

apache2-

Server Software: Apache/2.2.9
Server Hostname:
Server Port: 443
SSL/TLS Protocol: TLSv1/SSLv3,DHE-RSA-AES256-SHA,1024,256

Document Path: /
Document Length: 50825 bytes

Concurrency Level: 250
Time taken for tests: 33.324 seconds
Complete requests: 700
Failed requests: 0
Write errors: 0
Total transferred: 36709423 bytes
HTML transferred: 36135271 bytes
Requests per second: 21.01 [#/sec] (mean)
Time per request: 11901.542 [ms] (mean)
Time per request: 47.606 [ms] (mean, across all concurrent requests)
Transfer rate: 1075.76 [Kbytes/sec] received

lighthttpd
Server Software: lighttpd/1.4.19
Server Hostname:
Server Port: 443
SSL/TLS Protocol: TLSv1/SSLv3,AES256-SHA,1024,256

Document Path: /
Document Length: 50825 bytes

Concurrency Level: 250
Time taken for tests: 13.606 seconds
Complete requests: 700
Failed requests: 0
Write errors: 0
Total transferred: 36342698 bytes
HTML transferred: 35862831 bytes
Requests per second: 51.45 [#/sec] (mean)
Time per request: 4859.177 [ms] (mean)
Time per request: 19.437 [ms] (mean, across all concurrent requests)
Transfer rate: 2608.53 [Kbytes/sec] received

In addition lighttpd has a smaller memory footprint less cpu overhead which means you can do more on the same physical hardware.

Apache is bloated and the modules system, while good and necessary, has a major disadvantage when it comes to static html and php.
In reply to Justin Haaga

Re: apache2 vs lighttpd - take a second look

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
If you are answering me... did you read what I wrote? I entirely agree with the above BUT I caution people against using it in complex environments because lighttpd simply doesn't have the versatility that Apache does. For most users this is a non issue.

We get it, you like lighttpd. However, in the real world the performance you advocate is not always the only consideration. Look at the people who use Windows servers if you want proof of that wink