Apache MPM config

What is MPM apache?

Apache MPM (Multi Processing Module) handles and processes incoming requests, Prefork, Worker and Event are the different processing modules that we can choose when compiling our Apache, that is, they are the different types of processing. I will detail the characteristics of each of the possible MPMs (Prefork, Worker and Event), this so that we can adapt the version according to the needs of our server.

One important problem Apache HTTP has to solve, like any web server, is how to handle different processes to serve an http protocol request. This involves opening a socket, processing the request, keeping the connection open for a certain period, handling new events occurring through that connection, and returning the content produced by a program made in a particular language (such as PHP, Perl, or Python). These tasks are performed and controlled by a Multi-Processing Module (MPM).

How to enable or install MPM apache module on ubuntu

Before you begin this guide you’ll need the following:

- A FreeBSD 12.0 server set up following this guide.
- The FAMP stack installed on your server following this tutorial.
- Access to a user with root privileges (or allowed by using sudo) in order to make configuration changes.

Step 1

Comands to install: Firs log with root:

    - sudo su

and next command:

    - apt update 

Step 2

Install LAMP To install LAMP first we are going to install Apache2, and we can see the version and status:

    - apt install apache2
    - apache2 -v
    - service apache2 status

Then we install MYSQL client and server, and we can see the status:

    - apt install mysql-client mysql-server
    - service mysql status

After that configurate the MYSQL with the next comands:

    - mysql -u root -p
    - SELECT user,plugin,host FROM mysql.user;
    - ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'password';
    - SELECT user,plugin,host FROM mysql.user;
    - FLUSH PRIVILEGES;
    - exit

Install PHP

    - apt install php libapache2-mod-php php-mysql
    - php -v

Install phpmyadmin

    - apt install phpmyadmin
    - select ("apache2") and select ("yes")
    - create new password

Step 3

Change the Multi-Processing Module

    - systemctl stop apache2
    - a2dismod php7.4

Next, disable the Pre-fork MPM module with the following command:

    - a2dismod mpm_prefork

Next, enable the Event MPM module with the following command:

    - a2enmod mpm_event

Step 4

Configure Apache to Use the FastCGI Process Manager

First, install the PHP-FPM with the following command:

    - apt-get install php7.4-fpm -y

Once installed, you will need to install libapache2-mod-fcgid library in order to communicate Apache and PHP.

You can install it with the following command:

    - apt-get install libapache2-mod-fcgid -y

Once installed, you will need to enable the PHP-FPM, Proxy and FastCGI Proxy module in Apache webserver.

You can enable them with the following command:

    - a2enconf php7.4-fpm
    - a2enmod proxy
    - a2enmod proxy_fcgi

Next, restart the Apache service to apply the changes:

    - systemctl restart apache2

Now, verify the MPM module with the following command:

    - apachectl -M | grep 'mpm'

You should get the following output:

    - mpm_event_module (shared)

Next, verify the Proxy and FastCGI Proxy module with the following command:

    - apachectl -M | grep 'proxy'

You should get the following output:

    - proxy_module (shared)
    - proxy_fcgi_module (shared)

Step 5

Verify FastCGI Process Manager

At this point, Apache web server is configured to use the FastCGI Process Manager. It’s time to verify if PHP is using the FastCGI Process Manager.

First, create an info.php file inside the Apache document root directory:

    - nano /var/www/html/info.php

Add the following lines:

    <?php phpinfo(); ?>

Save and close the file when you are finished.

Sample Configuration

Fist we have a file of configuration just include the below lines into your httpd.conf apache configuration file:

    <IfModule mpm_prefork_module>
    StartServers 5
    MinSpareServers 10
    MaxSpareServers 15
    MaxRequestWorkers 50 #must be customized
    ServerLimit 50 #must be customized
    MaxRequestsPerChild 8000
    </IfModule>

StartServers

Created at startup, are the initial amount of Apache child servers.

This seldom changed directive only impacts Apache startup and restart processes. Generally not altered because Apache uses internal logic to work out how many child servers should be running.

Many modern servers periodically restart Apache to address configuration changes, rotate log files or other internal processes. When this occurs during a high load traffic surge, every bit of downtime matters. You can manually set the StartServers directive to mirror that of your MinSpareServers to shave off time from the Apache startup. By default it is 5.

MinSpareServers

This directive defines a minimum number of spare children the Apache parent process can maintain in its memory. An additional server is a preforked idle Apache child that is ready to respond to a new incoming request. Having idle children waiting for new requests is essential for providing the fastest server response times. When the total idle children on the server drop below this value, a new child is preforked at the rate of one per second until this directive is satisfied. The “one per second” rule is in place to prevent surges of the creation process that overload the server, however, this failsafe comes at a cost. The one per second spawn rate is particularly slow when it comes to handling page requests. So it’s highly beneficial to make sure enough children are preforked and ready to handle incoming requests.

MaxSpareServers

MaxSpareServers controls the maximum number of idle Apache child servers running at one time. An idle child is one which is not currently handling a request but waiting for a new request. When there are more than MaxSpareServers idle children, Apache kills off the excess.

If the MaxSpareServers value is less than MinSpareServers, Apache will automatically adjust MaxSpareServers to equal MinSpareServers plus one.

Like with MinSpareServers, this value should always be altered with available server resources in mind.

MaxRequestWorkers/MaxClients & ServerLimit.

Used to control the upper limit of children that the Apache parent server is allowed to have in memory at one time. These children (also called workers) handle requests on a 1:1 ratio. This translates into the maximum number of simultaneous requests the server can handle.

If this directive is too low, Apache under-utilizes the available hardware which translates to wasted money and long delays in page load times during peak hours. Alternatively, if this directive is too high, Apache outpaces the underlying hardware sending the system into thrashing (link to thrashing article) scenario which can lead to server crashes and potential data loss.

By default it is 256.

Server Limit

The ServerLimit directive represents the upper limit of MaxRequestWorkers. This setting is generally used as a safeguard or ceiling against input errors when modifying MaxRequestWorkers.

It becomes necessary to adjusted ServerLimit when the server is expected to handle more than the default of 256 requests simultaneously.

ServerLimit ties in directly with the thrashing point. The thrashing point is the maximum number of children Apache can run before memory usage tips the scale into perpetual swap. Match the ServerLimit to the calculated thrashing point to safeguard the server.

Note

Increasing ServerLimit is not recommended with MPM Prefork. Running more than 256 simultaneous requests is hardware intensive when using the MPM Prefork module.

You can use this shell script to determine an average amount of memory consumed by one Apache process. In addition to that it’ll show total amount of memory consumed by all Apache processes. Just unzip and execute as follows:

        wget http://cloudinservice.com/wp-content/uploads/2011/03/ap.sh.zip

        unzip ap.sh.zip

        sh ap.sh
    The output will be something like that:

        Apache Memory Usage (MB): 1372.6
        Average Proccess Size (MB): 54.9041

How to calculate values for MPM

If we want to calculate the value of MaxClients, it can be done as follows, but it is not highly recommended since you can set default values, but if you wanted to do it, it would be as follows.

Calculate MaxClients

Try to execute it several times to compare the numbers; good results will be shown when server is under a heavy load. Now when you know average amount of memory consumed by Apache and total amount of memory of your server, it is possible to calculate value to be used for MaxClients setting. For example, if in average one your Apache process consumes 50MB RAM and server RAM is 2GB, and you want to leave 512MB for the rest processes, then: MaxClients = (2GB – 512MB)/50MB = 30.72 ~ 30. ServerLimit is, as I understand, the same thing, but while MaxClient setting can be changed on the go without a need to restart Apache, for new ServerLimit value to take effect Apache restart is required. MaxClients should always be <= ServerLimit. To make it easy, I set ServerLimit = MaxClients calculated by above formula.

MaxConnectionsPerChild / MaxRequestsPerChild

This directive equals the number of requests a single Apache child server can handle.

This directive is a stop-gap for accidental memory leaks. Code executed through Apache may contain faults which leak memory. These leaks add up over time making less and less of the shared memory pool of the child usable. The way to recover from leaked memory is to recycle the affected Apache child process. Setting a MaxConnectionsPerChild limit will protect from this type of memory leakage.

Note

Rule-of-Thumb: Never set this too low. If the server encounters memory leak issues start with 10,000 and reduce incrementally.