Apache vs. lighttpd

October 27th, 2009

I’ve had a somewhat recurring problem that Apache on my server is a memory hog. The machine has a full GB of RAM now, but even so, heavy activity from spidering bots or reddit can bring it to its knees.

I’ve been trying to figure out what to do about it. I run Apache on there, and here’s an idea of what it hosts:

  • This blog, and two others like it, using WordPress
  • Several sites that are nothing but static files
  • gitweb
  • A redmine (ruby on rails) site
  • Several sites running MoinMoin
  • A set of tens of thousands of redirects from mailing list archives I used to host over to gmane (this had negligible impact on resource use when I added it)
  • A few other smaller PHP apps

Due to PHP limitations, it will only work with the Apache prefork or itk MPMs. That constrains the entire rest of the system to those particular MPMs. Other than PHP, most of the rest of these sites are running using FastCGI — that includes redmine and MoinMoin, although gitweb is a plain cgi. Many of these sites, such as this blog, have changed underlying software over the years, and I use mod_rewrite to issue permanent redirects from old URLs to their corresponding new ones as much as possible.

I do have two IPs allocated to the server, and so I can run multiple webservers if desired.

lighttpd is a lightweight webserver. I’ve browsed their documentation some, and I’m having a lot of trouble figuring out whether it would help me any. The way I see it, I have four options:

  1. Keep everything as it is now
  2. Stick with Apache, discard mod_php and use FastCGI for PHP, and pick some better MPM (whatever that might be)
  3. Keep Apache for PHP and move the rest to lighttpd
  4. Move everything to lighttpd, with FastCGI for PHP

I know Apache pretty well, and lighttpd not at all, so if all else is equal, I’d want to stick with Apache. But I’m certainly not above trying something else.

One other wrinkle is that right now everything runs as www-data — PHP, MoinMoin, static sites, everything. That makes me nervous, and I’d like to run some sites under different users for security. I’m not sure if Apache or lighttpd is better for that.

If anybody has thoughts, I’m all ears.

Categories: Uncategorized

Leave a comment

Comments Feed21 Comments

  1. Anonymous

    I’ve run lighttpd for a while now, and I’d put it this way. On the one hand, lighttpd seems worlds better than apache in almost every way. On the other hand, more people run apache, so you may find it easier to get help, or to find sample configurations.

    Lighttpd does have very good documentation, though.

    Personally, I’d suggest an incremental approach. First, migrate to using FastCGI for PHP. Then, you can go ahead and switch to a sensible apache MPM. Finally, you can start incrementally moving things to lighttpd, if you want to.

    And most importantly, as step 1, install etckeeper. ;)

    Reply

  2. XANi

    PHP can be run using FastCGI and then u can use much better (in terms of memory and speed) mpm_worker.
    U can run apache + suexec + fastcgi, but its complicated to setup and ull probably be better with lighttpd (i have similar config, wordpress, redmine, and one or 2 php apps), tho u will probably spend some time with googling “how to do that in lighttpd” as it dont have things like .htaccess so u have to do rewrites directly in config.

    Reply

  3. Miki

    Hi,

    I recently switched from Apache 2.2 to nginx (similar to lighttpd but less buggy) and in a couple of hours I’ve been able to retain old php stuff proxying Apache through nginx AND to develop a couple of applications (in php and python) directly in nginx (with FastCGI)…

    …and I’m very happy :-)

    Reply

  4. rata

    Redmine uses a lot of memory (and IO, IIRC). I don’t know if that has anything to do with your problem, but you can check :)

    Also, there’s an article in debian administration about a similar problem: http://www.debian-administration.org/article/Speeding_up_dynamic_websites_via_an_nginx_proxy

    Reply

    John Goerzen Reply:

    I’m very unhappy with redmine, but I’m happier with it than the alternatives. I know it IS part of the problem, though its botblock module has helped some. http://changelog.complete.org/archives/1123-free-software-project-hosting

    Reply

  5. Harmen

    The normal solution is to use a small proxy server on port 80 (‘pound’ works very well), and let it proxy everything to different http deamons listening on different ports. This way you can run as many exotic things as you like, multiple apaches with their own configs, modules, thread models. They can run under different users, they can be lighttpd, apache, pure ruby servers. You can restart 1 server without bothering any other. You can put in loadbalancing/failover or move a site to another box in your network without the rest of the world knowing. And you’ll probably use less memory in total since it’s a small deamon which does the slow talking to the client. The heavy apache gives the content to the little proxyserver, and that ‘spoonfeeds’ it to the client. You’ll need way less apaches.

    Google ‘reverse proxy’ for more info.

    Reply

  6. Gour

    Hi,

    I’m happy with Cherokee (http://www.cherokee-project.com/).

    Sincerely,
    Gour

    Reply

  7. Michael Goetze

    I think you forgot the option of running two different instances of Apache with different MPMs.

    Nevertheless, I would encourage you to dive into lighttpd and try it out, IMHO it has a very powerful, yet easy-to-learn configuration syntax.

    Reply

  8. Paul Tötterman

    I’ve had a very good experience with nginx + mod_rails (phusion passenger) + redmine.

    Reply

  9. Leo 'costela' Antunes

    I’d agree with the first Anonymous. It’s a safe bet and makes your life easier if you ever should be disappointed with lighttpd.

    I’ve personally made this same migration a few months ago and haven’t looked back:
    http://costela.net/2009/07/migrated-to-lighttpd/
    (sorry for the lack of technical details on the migration)

    At first the memory gain doesn’t seem so incredible, but over longer periods of time lighttpd keeps a stabler memory footprint then apache and my flimsy VPS has been overall a lot more responsive, since it had been swapping heavily under Apache.

    Notes:
    – I didn’t try nginx for lack of documentation.
    – I’m using the squeeze version of lighttpd to avoid some bugs
    – I have nothing against Apache and also felt originally more comfortable with it, but the curiosity of trying some new approach won in the end! :)

    Reply

  10. .

    A while ago, I actually made a point of getting rid of PHP. It seems my apache memory problems went away with that as well. It’s hard, but it seems to be the sanest way.

    Reply

  11. Sake

    A year ago or so, I made the switch from Apache to lighty. The reason was that I only have a quite small (130 mb ram) virtual server. Apache was using the majority of the memory by just running idle. A Friend pointed me to lighty and I tried it. The effect was that my server was more responsive and it practically used no mem at all if waiting.
    As I also had some stuff running with php I was curious if it would work. After some digging in the web I found this simple config line for mod_cgi.
    cgi.assign = (“.php” => “/usr/bin/php-cgi”,)
    With that, all php files are executed as it is done in Apache.

    The multiple server stuff is simply a matter of creating a new config file with
    $HTTP[“host”] == “anysub.yourhostname.com” {
    server.document-root = “/home/someuser/html”
    }
    in it. There are directives for https and ip based selections, too.
    In this host section the php stuff from above can be nested and is thereby only activated for this host.

    I hope this clarifies things for you.

    Reply

  12. Marco

    I second the suggestion to use a reverse proxy. Try varnish
    http://varnish.projects.linpro.no/ . You can deploy it without changing your webserver configuration or changing very little.
    Regarding Apache vs Lighttpd, I use extensively both.
    Lighttpd has a smaller memory footprint and is super-fast in serving static files, but regardind php/fastcgi I think most on the problems reside in the php court. I didn’t see a large improvement switching to lighttpd/fastcgi, and the configuration is quite different (but well documented). Of course YMMV, depending on the php applications you are using.

    Reply

  13. Stig

    If you want to move the moinmoins to separate processes, each running with another user, you can do what I’ve documented at http://fnord.no/sysadmin/web/moinmoin-spring-cleaning

    That’s Apache HTTPD, mod_wsgi and moin. Varnish in front, with a small 30MB cache, speeds up the wikis immensely, and moiinmoin serves the correct headers to tell Varnish what to do.

    Reply

  14. Jan Van Buggenhout

    A while back, I did some research about all the possible ways of combining apache & php. I’ve written down a list of all the options I could find, with some notes:

    http://chipzz.safehex.be/apache-php.html

    The list is fairly incomplete, in that it doesn’t give credit to where I found the information (it was after all only meant for my own usage). Hope this helps.

    Reply

  15. cate

    Now I use lighttpd without problems. The configuration is different, but easy to learn, and simpler to maintain.

    But if you need less memory footprint, look for alternatives to wordpress. IMHO it is unacceptable that an web-application requires 64MB per process (an PHP hard limit to 32MB make wordpress unusable). With few calculations you will find that few user could take down your server.

    Reply

  16. Michael Alan Dorman

    Of the new wave of “lightweight” httpds, I’ve been most pleased with cherokee.

    Lest I be accused of being a simple fanboy:

    I used lighttpd for a couple of years, but when I decided to convert some small linodes from apache about six month ago I looked around, and the project seems to have stalled—they became way too ambitious about 2.0 and it appears to be perpetually on the horizon.

    I also use nginx on some production servers, but it has some limitations that make it less general purpose than the others—no straight-up CGI, for instance—so I only use it in very specialised places, in this case as IMAP/POP proxies.

    So I tried cherokee, and it does everything I want at least as simply. If I had any criticism, it’s that they’re very oriented towards their web-based admin tool. This has turned out to annoy me less than I expected, but it’s something to consider.

    All of that said, I think many of your issues could be resolved by moving the PHP code out to fast cgi, using suexec with mod_fcgid to move away from www-data for everything, and just use a different MPM—I have many production systems doing this and they haven’t annoyed me enough to move to cherokee yet.

    Reply

  17. niq

    1. If apache+php is eating excessive memory, then moving to fastcgi (and worker or event MPM) is a worthwhile solution – and indeed brings you into line with the recommended lighttpd configuration. mod_fcgid has recently been donated to Apache, and will be included in future releases.

    2. If it’s just serving as a web server with PHP, then any of the “usual suspects” will do just fine. It’s possible a lightweight server may outperform a bigger one (Apache 2.x is much more of an appserver than Apache1/lighttpd/nginx/etc), but any difference is dwarfed by PHP’s footprint.

    3. Fanboys for minority webservers love to bash apache, but in doing so they often report ridiculously poor figures for apache. Serious users get serious performance from it.

    Reply

  18. Apache Update | The Changelog

    […] everyone for the helpful comments yesterday on Apache vs. lighttpd. I’ve taken the first few steps towards improving things. I’ve […]

  19. Np237

    The real interest of lighttpd over Apache is not performance, unless your site has very high traffic. It is not memory, unless you have a very small amount of it (except for mod_php/mod_python of course, these are shit). The real interest is having a configuration file that doesn’t look like a voodoo incantation. Lighty’s configuration is simple and very powerful, it allows for complex setups without difficulties.

    Reply

  20. Jed

    The G-Wan server does not use any configuration file, and it is massively faster than Nginx, Cherokee or Lighttpd.

    Plus, it comes with an embedded script engine to generate dynamic contents which is much faster than PHP, C# or Java.

    Makes life easier… and less expensive!

    Reply

Leave a comment

 

Feed

http://changelog.complete.org / Apache vs. lighttpd