November 21, 2014

Speedup your LAMP stack with LigHTTPD

Apache Web server could frequently be bottleneck for your LAMP stack, or you might be wasting resources on your web servers. The problem with Apache server comes from “client per process” architecture which you have to stick to at least for dynamic content served by PHP or ModPerl. With “client per process” you end up having large amount of processes if you have many clients. As processes service all requests they can get you end up having a lot of memory allocated per process which is not fully released to OS even if process is serving static requests or doing keepalive. So why do you need many processes ? Keep alive is one thing, which you can disable of course. But the real problem is slow clients which need to be spoon feed. This is especially the problem for large pages and files but with bad/slow networks even small objects may cause the stall. There are multiple solutions and workarounds to this problem:

  • Just get more memory/more boxes. The good thing with Web servers is – it is easy to scale, this is if you have money to waste of course. Just get more servers, beef them up with memory and you should be fine. If this is your choice you can stop reading :)
  • Restrict resources. You can limit number of Apache children (MaxClients 50) , disable keep alive (KeepAlive Off) and relay on Operation System connection buffering (ListenBacklog 1024) but such approach is often bad for site performance. Even if you can balance it to work right in normal case, minor network or database server slowdowns may cause overload.
  • Use LingerD. I have not ever used this one, but in theory it should relieve apache from closing connection which is part of the job, however it does not play good with keep alive and does not eliminate all problems.
  • Use FastCGI instead of mod_php etc. Apache itself without any programming language modules is light, futhermore you may use threaded worker models to keep overhead lower. However FastCGI is a bit less tested interface, so some software might not play well with it, some, including APC requires careful configuration to work like you expect it to work.
  • Use other server, ie lighttpd with FastCGI. Honestly I like this solution more – it allows you to get rid of all the fat Apache server has and get efficient event driven socket processing. This is however hard step as it may require changes in your application if you used apache specific modules – ie mod_rewrite, .htaccess, apache specific environment variables etc. So this is typically the path for new applications which do not require legacy applications.
  • Use Reverse proxy in front. You may configure other Apache server to use as reverse proxy, you may also use Squid, Lighttpd, Nginx etc, in fact Most of light http servers support such configuration. There are several benefits in this configuration. One obvious one is Caching – it however requires you to provide cache expiration information for dynamic contents etc which your applications might not be doing at this point. The other benefit is more important for scope of this article. Now you have proxy server spoonfeeding the clients, doing keep-alive and your apache is relieved from this job, so you can shrink number of needed children dramatically. One more benefit is load balacing and HA – you may have round robin or random request routing to your proxy servers, which pretty much any load balancer can do, but you may configure each of them to do hash routing to real servers (especially for static content this may help to increase file cache efficiency dramatically). Such solution also typically allow you to relieve Apache from serving static content – Proxy may be configured to handle only dynamic requests, serving static files by itself. The tiny but annoying with such configuration could be logging. Instead of real IP of your clients you would see 10.0.0.1 or whatever IP your proxy server is located. Just recently I found out – there is great module out where – mod_rpaf which can replace remote host with IP passed by your local proxy. So now you can put reverse proxy infront of your Apache web server in 10 minutes and you will likely need no changes in your application.
  • Dedicated server for static content. If you serve a lot of static content – music, videos, file download I guess it is good idea anyway as such servers might be configured differently. You also may have different routing policies set for these servers – you want Web pages do be real time while you do not really care about latency for large files download. If your mostly static contents are few images used in pages, setting them to be directly services by reverse proxy servers could be enough.

The note about LigHTTPD – I like this one in particular because it is really easy to set up, it is pretty stable which is proved by number of major sites using it. It has good set of modules, decent documentation and community helping to develop it. So it is not single guy project any more. I however can’t say it is best one – I simply did not test too much of other alternatives as it already worked well. There are few features which it lacks and which could make it faster. Few missing items I should mention:

  • Persistent connections to FastCGI servers
  • Keep alive from Reverse proxy to web server
  • Async IO for serving large large files

The good thing I hear most of these are already in works. Thanks Jan and Lighttpd contributors.

About Peter Zaitsev

Peter managed the High Performance Group within MySQL until 2006, when he founded Percona. Peter has a Master's Degree in Computer Science and is an expert in database kernels, computer hardware, and application scaling.

Comments

  1. Vadim says:

    Lingerd is good for Apache 1.3.x, but it doesn’t work for 2.x series

  2. peter says:

    Could be. As I mentioned I never really used it. I just found it looking for solution one day… but decided it will not solve all the problems anyway so why bother.

  3. Apachez says:

    Is there any solution other than apache 2.x + mod_perl 2.x if you need to boost your vanilla perl cgi scripts ?

    Does LigHTTPD for example have some kind of “accelerator” for perl scripts available ?

  4. peter says:

    Not what I know about. But you still can have Lighttpd in front of your apache for spoonfeeding. I do not have much experience of using lighttpd with perl to be honest

  5. Vadim says:

    Apachez,

    You can try LigHTTPD + FastCGI for Perl also. FastCGI has a good interface to Perl: FCGI (http://fastcgi.com/#TheDevKit).
    However I did not test it, so I can’t say if it is better than apache + mod_perl

  6. erkko says:

    LogFormat %{X-Forwarded-for}i %l %u %t \”%r\” %>s %b %{Referer}i %{User-Agent}i combined

  7. thank you for the greate article.

  8. IT_Architect says:

    Why not mod_proxy + mod_cache + proxy to nginx for static? That way you retain the .htaccess and mod_rewrite functionality used by most of the applications out there, and retain the speed and reliability advantages of mod_php.

  9. jerome says:

    Hello Peter, nice article.

    However creating your own Apache module can sometimes solve a lot of problems and may not force you to switch to Lighttpd (or something else).
    The framework (APR and APR-Utils) used by Apache is really powerful and makes things really easier to developp you own modules.

    Have a nice day

    Best Regards. :)

    APR : http://apr.apache.org/

  10. sohbet says:

    I’m happy to see the team grow. If you guys ever open up a Whistler office let me know, I’m so in

Speak Your Mind

*