Which to install: Apache Worker or Prefork? What are the (dis-)advantages of each?

57,814

Solution 1

As the docs say, you should use the prefork MPM if you need to avoid threading for compatibility with non-thread-safe libraries. Typically, any non-trivial Apache module (mod_php -- or, more precisely, the myriad of extensions and libraries that it links to -- being the canonical example) has some sort of non-thread-safe library (or has non-thread-safe code in it), so unless you're using a pretty stock Apache install, I'd go for the prefork MPM.

Solution 2

The classic solution to running unsafe extensions while serving large numbers (>100) of concurrent connections is to run PHP on fastCGI (mod_fcgid, a native apache module) and proxy dynamic requests to that from an apache instance that runs the Worker MPM.

This would enable you to scale from a few hundred up to >1000 concurrent connections with a modest amount of memory (4~8GB) when serving a mix of static and dynamic content.

Of course, you should also investigate front-end caching solutions as part of your overall deployment (memcached, varnish).

Alternatively, upgrade to apache 2.4 and its native event MPM, which handles concurrency in a much improved fashion (threads are fired off upon connection, not waiting to be polled.)

Solution 3

It's been about 3 years since the question got posted but I would recommend you go with worker MPM instead of pre-fork even if using PHP, to get the better performance.

As to the differences, pre-fork is non-threaded hence the server forks a process for each client request (it pre-forks in anticipation of new requests so that forking doesn't eat into the response time). Since requests are server in a separate process, this usually taxes your memory and CPU a lot more than. The worker bring multi-threading which is lighter and has better memory utilization.

Solution 4

This is something very particular to what you're serving. If you're doing lots of little static connections, threads would be lighter and faster. If you just have few big apps constantly spawned, prefork might have an edge due it's maturity and stability. Why not just set up what you need, test one, swap out the MPM module, try it again, see which one suits you better?

Solution 5

that needs on the type and kind of traffic you will have. And also first you need to understand the main difference between the prefork and worker. Hope the below article will help you figure out! http://slashroot.in/how-is-nginx-different-from-apache

Share:
57,814

Related videos on Youtube

Aron Rotteveel
Author by

Aron Rotteveel

Words describing me in third person.

Updated on September 17, 2022

Comments

  • Aron Rotteveel
    Aron Rotteveel almost 2 years

    Based on the descriptions for both the Prefork and Worker MPM, it seems the prefork type is somewhat outdated, but I can't really find a proper comparison of the two types.

    What i'd like to know:

    • What are the differences between the two versions?
    • What are the (dis-)advantages of each server type?
    • Are there any basic guidelines on which type to choose based on the conditions?
    • Are there any big performance differences between the two?
  • David Pashley
    David Pashley almost 15 years
    I would have recommended the worker MPM, unless you're running PHP. Worker is the recommended MPM from apache, and gives better performance and lower overhead. It's only that PHP developer have never heard of thread-safety that you need to use prefork.
  • Joseph Anderson
    Joseph Anderson almost 15 years
    PHP has been thread safe for a very long time. They only suggest the use of pre-forkers as they cannot control what other libraries do. Quit blaming PHP for others developers inactions.
  • Aleksandar Ivanisevic
    Aleksandar Ivanisevic almost 15 years
    PHP may be thread safe (although I doubt it) but all the libraries that it links to are definitely not. Here we run a few fairly large PHP applications and every couple of months we try to switch from prefork to worker, but we get corrupted data straight away.
  • radius
    radius almost 15 years
    At least function changing ENV variable will not be thread safe, setlocal php.net/manual/en/function.setlocale.php is a common exemple of that.
  • adaptr
    adaptr about 12 years
    You can't arbitrarily "swap out" the MPM in apache 2.2; it is set at compile time.
  • adaptr
    adaptr about 12 years
    While the worker MPM was already thread-based, and hence much faster to start and lighter to run, the event MPM no longer polls the socket - it gets notified on activity; therefore, "event".
  • Sirex
    Sirex about 12 years
    so it should work better on high traffic (13k/sec) sites ?
  • user2902302
    user2902302 over 11 years
    One note: These problems do not apply if PHP is attached e.g. with php-fpm via FastCGI. Then the worker MPM is just fine -- then the fpm will run every PHP request in an own process while the Apache can run threaded. The PHP-Thread-safety problem only prevents you from using mod_php, which runs PHP inside the Apache process.
  • Deb
    Deb over 11 years
    We prefer answers to have content, not links to content. If you could provide a summary of what's on the link-target, that is best-practice. Link-rot happens.
  • SineSwiper
    SineSwiper about 11 years
    You can with apt or RPMs. Debian has several different Apache 2 packages, depending on which style you prefer.