Understanding Apache 2 MPM (worker vs prefork)

(转载):http://www.garron.me/en/blog/apache2-mpm-worker-prefork-php.html

 

 

Understanding Apache 2 MPM (worker vs prefork)

Written by Guillermo Garron .
Date: 2012-12-26 14:28:00 +0000

From time to time I want to learn more about stability and high availability web servers. I usually get a sandbox site where I start to play with configurations.

This time all started because I also like testing service providers, and this week I have been moving my site between Site44 and NearlyFreeSpeech to test its features for static sites.

Finally in order to test how Apache may work when serving static files (I usually use Nginx for that matter) I installed Apache 2.2 on an Ubuntu 12.10 server (Linode). After doing that I headed toblitz.io and tested my Apache 2.2 serving static files. To my surprise it served without a sweat for one minute a page with user concurrency of 250 users per second.

The next day I tried to install a Wordpress site on the same server, as soon as I tried to install php, apt told me that it was going to uninstall apache2-mpm-worker and install apache2-mpm-preforkinstead.

I instructed apt to do nothing and started to read about the differences. It turned out that Apache may work in two different ways which are:

Apache MPM Worker

In this mode, Apache works more or less like Nginx.

This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.

A single control process (the parent) is responsible for launching child processes. Each child process creates a fixed number of server threads as specified in the ThreadsPerChild directive, as well as a listener thread which listens for connections and passes them to a server thread for processing when they arrive.

There is the reason why my tests with blitz.io and Apache2 serving static files went so well, I was using a Nginx-like configured Apache.

Apache MPM Prefork

This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.

Differences are notable, and because we usually install Apache in a LAMP stack, we install something like this

 sudo apt-get install apache2 php5 mysql-server mysql-client

That way we end up with apache2-mpm-prefork package. Because I have installed my Apache2 copy as a stand alone server Ubuntu installed the apache2-mpm-worker pre-build package instead.

Why prefork

Despite MPM worker being more efficient than MPM prefork when the server is under heavy load, it is usually not installed that way because mod_php can’t work with MPM worker.

We need to know a little bit about how you can execute PHP code when working with Apache.

The most common way is with mod_php which is an Apache module capable to understand and execute PHP code inside Apache itself. This is the most common way to run PHP under Apache.

But there is another way, which is similar to what Nginx does, and that is having PHP interpreted as CGI, which means that Apache is going to forward PHP to an external interpreter and get the result back. Using this method allows you to keep Apache configured to run in worker MPM mode, and still be able to “understand” PHP.

In the next two post of these series I will show you how to configure a LAMP server using Apache with MPM worker and running eficiently a copy of Wordpress.

你可能感兴趣的:(apache2 mpm)