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.
Comands to install: Firs log with root:
and next command:
Install LAMP To install LAMP first we are going to install Apache2, and we can see the version and status:
Then we install MYSQL client and server, and we can see the status:
After that configurate the MYSQL with the next comands:
Change the Multi-Processing Module
Next, disable the Pre-fork MPM module with the following command:
Next, enable the Event MPM module with the following command:
Configure Apache to Use the FastCGI Process Manager
First, install the PHP-FPM with the following command:
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:
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:
Next, restart the Apache service to apply the changes:
Now, verify the MPM module with the following command:
You should get the following output:
Next, verify the Proxy and FastCGI Proxy module with the following command:
You should get the following output:
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:
Add the following lines:
Save and close the file when you are finished.
Fist we have a file of configuration just include the below lines into your httpd.conf apache configuration file:
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.
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 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.
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.
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:
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.
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.
Rule-of-Thumb: Never set this too low. If the server encounters memory leak issues start with 10,000 and reduce incrementally.