A Hacker's Journey: from Cook to Chef

16 Understand Apache HTTPD worker MPM

MPM stands for Apache Multi-Processing Modules. There are three type of modules available for various versions of Apache HTTPD:

  • Prefork (developed in Apache 1)
  • Worker (introduced in Apache 2)
  • Event (became stable in Apache 2.4 with exception with SSL connections)

Prefork has the best compatibility with other modules that are not thread safe. It spins off multiple child processes with one thread each. Each process handles one connection at a time which isolates each request, so that a problem with a single request will not affect any other. But it slows down the performance for concurrent requests since they are made to wait in line until a server process is free and has higher memory consumption.

The worker MPM uses multiple child processes with many threads each. Each thread handles one connection at a time. Worker is generally a good choice for high-traffic servers because it has a smaller memory footprint than the prefork MPM. However, note that the treads are attached to connections and not requests - which means that a keep-alive connection always keeps ahold of a thread until it's closed (which can be a long time, depending on your configuration).

The event module uses a dedicated thread to deal with the kept-alive connections, and hands requests down to child threads only when a request has actually been made (allowing those threads to free back up immediately after the request is completed). This is great for concurrency of clients that aren't necessarily all active at a time, but make occasional requests, and when the clients might have a long keep-alive timeout. Under SSL connection mode, it behaves identically to worker which sticks to a given connection to a given thread until the connection closes. It's not available on Apache 2.2.

Each module has a different set of the configuration directives in the Apache webserver configuration file. The default MPM for Unix is the Prefork module, which is tested to work properly with Parallels Plesk Panel. You can switch to worker by the following ways:

  1. Using the default HTTPD process

  2. Uncomment the HTTPD directive in the /etc/sysconfig/httpd file:

    HTTPD=/usr/sbin/httpd.worker

    Then restart the Apache server:

    # /etc/init.d/httpd restart

    Now you can find the worker.c instead of prefork.c is compiled in the modules and is loaded in Apache since you can only have one MPM module loaded at any time:

    # httpd -l

    or

    # apachectl -V

    apachectl is a wrapper over httpd and just sets some environment variables and adds further functionality and appends -k start parameter to the httpd process.

  3. Using separate httpd instance

  4. # sudo httpd.worker -f <configuration file path> -k start/stop

    and you can find the worker.c is compiled in the modules using:

    # httpd.worker -l