Apache vs. lighttpd

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.

21 thoughts on “Apache vs. lighttpd

  1. Anonymous says:

    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. ;)

  2. XANi says:

    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.

  3. Miki says:

    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 :-)

  4. rata says:

    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

    1. John Goerzen says:

      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

  5. Harmen says:

    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.

  6. Gour says:

    Hi,

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

    Sincerely,
    Gour

  7. Michael Goetze says:

    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.

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

  9. 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! :)

  10. . says:

    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.

  11. Sake says:

    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.

  12. Marco says:

    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.

  13. Stig says:

    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.

  14. Jan Van Buggenhout says:

    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.

  15. cate says:

    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.

  16. 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.

  17. niq says:

    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.

  18. Np237 says:

    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.

  19. Jed says:

    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!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.