22 March 2021

The Apache web server can use different Multi-Processing Modules (MPMs). An MPM performs various important tasks, including dealing with requests and creating child processes or threads. On cPanel servers the default MPM is prefork. Depending on your web application it may be beneficial to use a different MPM.

You can view and change the MPM in WHM via Software » EasyApache. Alternative, you can also check the Apache configuration on the command line:

# httpd -V
Server version: Apache/2.4.46 (cPanel)
...
Server MPM:     worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
...

Forking and threading

To understand the differences between the available MPM it’s important to understand threading and forking. The Apache web server listens for new connections and acts on requests. Put simply, when someone views a web page the server establishes a connection and serves the content. It’s possible to do this without creating forks or threads but it would be inefficient. Apache would be able to do only one thing at any one time. On a busy server requests would be queued, which results in slow response times.

One way Apache can more efficiently deal with requests is by creating forks. When a httpd process is forked a new child process is created. Initially the child is a clone of the parent but it can go its own merry way. In other words, by using forks Apache can handle multiple connections at the same time.

Forked processes have their own state and memory space, independent of the parent process. As a result Apache uses more resources as more children are created. This is where threads come in. A thread is a sub-process that shares its state and memory space with its parent. This is efficient, as it means threads use less memory than forked processes.

Worker MPM

The worker MPM turns Apache into a multi-process and multi-threaded web server. It uses a parent process that forks child processes as and when needed. The child processes create a listener thread that listens for new connections and a fixed (but configurable) number of threads. Because the MPM uses threads Apache can serve a large number of requests using relatively few system resources. This makes MPM a good candidate for servers dealing with lots of traffic.

Event MPM

The event MPM adds better keep-alive handling to the worker MPM. It uses a listener thread for each process to manage live connections. When a child processes has finished its job it reports back to the listener process and becomes available for any other jobs. The listener process is terminated when the KeepAliveTimeout has been reached. If there are any further connections then the listener will pass the job on hand to the first available listener.

Prefork MPM

The default MPM on cPanel servers is prefork. This is a so-called pre-forking web server. It uses a parent process that tries to make sure there are several spare or idle processes available to serve new requests. The prefork MPM is non-threaded, which means that each child process can handle only one request at a time. A pre-forking server is relatively RAM-hungry, so it’s important to find the right balance between the maximum number of processes and the amount of RAM.

MPM ITK

And finally, MPM ITK is an Apache module that depends on the prefork MPM. The module is designed to deal with the inevitable privilege separation problem. By default, the Apache parent process runs as the nobody user, which effectively is the user with the least amount of privileges. This is why on cPanel server a user’s home directory has the group owner nobody by default. The group ownership is an issue though, as it doesn’t separate different accounts on a server.

There are various modules that can be used to address the issue, such as suexec and mod_ruid2. Another option is using MPM ITK. When the module is enabled Apache reads request headers as root rather than the nobody user, and it then switches to the user before responding to the request. Once that has been done the process is terminated.

The module comes with a performance hit and needs to be used with the CGI handler (which has largely fallen out of favour and is primarily a fallback option for when no other PHP handler is available). Also, it’s not compatible with modules such as mod_ruid2 and CloudLinux’s PHP Selector.

Changing the MPM

The easiest and safest way to change the MPM is via EasyApache. You can use the command line but you need to be careful, as you need to both remove and install some packages in a single transaction (using yum shell or, in the case of RHEL8-based systems, dnf shell). There’s more information about changing the MPM in the cPanel documention. Of course, we are happy to discuss the pros and cons of different MPMs for your server, and we can make any changes for you.