Home / guides   Print version

Configure Apache with MPM Event and PHP-FPM

Apache server has three different MPM (Multi-Processing Module) modules. These are the modules that handles the request from the browsers.

  • Pre-fork MPM:

    Pre-fork is the default way Apache handles requests.
    Each incoming request creates a new process that handles that request.
    These processes are independent and each has its own memory space.
    The advantage of this approach is that an issue in one process doesn’t affect the other processes.
    The disadvantage is that each process consumes memory and computational resources, which can limit the server’s performance.
  • Worker MPM:

    Worker MPM is a more advanced way of processing requests in Apache.
    Instead of creating new processes for each request, Apache uses a single main process and multiple threads.
    Each thread handles one request at a time.
    Threads share memory space, reducing memory consumption and computational resources compared to pre-fork.
    By using threads, the server can efficiently handle multiple requests simultaneously and achieve higher performance.
  • Event MPM:

    Event MPM is even more advanced than Worker MPM.
    It uses both threads and asynchronous I/O operations (read or write operations without waiting for completion) for efficient request processing.
    Event MPM uses an event-driven model that allows the server to handle multiple requests at once.
    This approach is highly efficient for handling a large number of requests when the server is heavily loaded.
    Event MPM also minimizes "keep-alive" connections, where the connection between the client and server remains open for reuse.

We will explain you how to setup Event MPM on Apache, and that PHP still works.
We recommend you first test it on developments server and test you site before doing it to the production server.

Step one: stop apache
systemctl stop apache2
disable PHP module (that only works with pre-fork)
/sbin/a2dismod php8.4
Disable MPM pre-fork and enable MPM event module
/sbin/a2dismod mpm_prefork
/sbin/a2enmod mpm_event

Next step is installing the programs to get PHP working on apache again.
The communication between Apache and PHP will take place via a UNIX socket now.
apt-get install php-fpm
apt-get install libapache2-mod-fcgi

Activate PHP
/sbin/a2enconf php8.4-fpm
And the modules
/sbin/a2enmod proxy
/sbin/a2enmod proxy_fcgi

Test if there no obvious errors in the config
/sbin/apachectl configtest

If everything is OK, start apache
systemctl restart apache2

with /sbin/apachectl status you can see how many process your server is using now.

You can fine tune by adapting the config in, depending on how much resources your server has.
/etc/apache2/mods-enabled/mpm_event.conf

# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of requests a server process serves
StartServers            2
MinSpareThreads         25
MaxSpareThreads         75
ThreadLimit             64
ThreadsPerChild         25
MaxRequestWorkers       150
MaxConnectionsPerChild  0
MaxRequestWorkers is the rhougly the amount of connection you server can handle at the same time.
You can increase it by the amount of ThreadsPerChild, 25 by default.
We recommend you do it in steps, and see the effects on your server (memory and CPU use)

Because MPM-event uses the resources of your server better than Pre-fork MPM, you'll be able to serve more visitors to your site.

 

TOP