In our earlier articles, we have discussed How to Install Apache and PHP on Ubuntu 22.04 Server and What is Keepalive in the Context of Apache Webserver. From the previous articles, we have learned that:
Keepalive is a mechanism in data transmission with the goals of maintaining a network connection and convincing oneself of the accessibility and function of a communication partner. Keepalives are usually specific packets of a network protocol. A keep-alive signal is often sent at predefined intervals. If no response is received after a signal is sent, it is assumed that the connection is lost, or future data is routed through a different path until the connection is restored. A keep-alive signal can also be used to indicate to the Internet infrastructure that the connection should be maintained. So far, we have mentioned the following points:
Advantages of Keepalive
---

- Reduces latency associated with HTTP transfers
- Reduces CPU usage while serving different files such as images, stylesheets, javascript files etc
Disadvantages of Keepalive
- Increases memory usage
How to Optimize Apache Keepalive Settings
You can test whether Keepalive is working or not by running this command:
1 | curl -Iv https://example.com 2>&1 | grep -i 'connection #0' |
If you get:
1 | * Connection #0 to host example.com left intact |
That means keepalive is working. You can use some online tools to check keepalive:
1 2 | https://technumero.com/keep-alive-test/ https://www.giftofspeed.com/check-keep-alive/ |
Coming back to the topic. We are using the mpm_event module. The main configuration file for Apache settings is this one:
1 | /etc/apache2/apache2.conf |
Open that file:
1 | nano /etc/apache2/apache2.conf |
Search for the phrase keepalive by pressing ctrl + w. This is the default settings:
1 2 3 4 5 6 | ... Timeout 300 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 5 ... |
Usually, this settings is enough good. Open the mpm_event configuration file:
1 | nano /etc/apache2/mods-enabled/mpm_event.conf |
For a server with 16GB RAM, you can try this setting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <IfModule mpm_event_module> KeepAlive On KeepAliveTimeout 5 MaxKeepAliveRequests 128 ServerLimit 10 StartServers 4 ThreadLimit 128 ThreadsPerChild 128 MinSpareThreads 256 MaxSpareThreads 512 MaxRequestWorkers 1280 MaxConnectionsPerChild 2048 </IfModule> |
Restart Apache after any change:
1 2 3 | sudo apachectl -t # If syntax is OK sudo systemctl restart apache2 |
Keepalive is indirectly related to the other settings. Apache documentation says “When a client uses a Keep-Alive connection, it will be counted as a single “request” for the MaxConnectionsPerChild directive, regardless of how many requests are sent using the connection.” In our case, it is 2048.
Whenever an Apache server is started, it will start the child processes which are determined by the StartServers parameter. In our case, it is 4.
Then each process will start 128 threads determined by the ThreadsPerChild parameter so this means 4 processes can service 512 concurrent connections/clients i.e. 128×4=512.
Now if more concurrent users come, then another child process will start, which can service another 128 users. But how many child processes can be started is controlled by the ServerLimit parameter, this means that in the configuration above, we can have 10 child processes in total, with each child process can handle 128 threads, in total handling 10×128=1280 concurrent users. It is huge enough. We have to keep so high because of legitimate bots & crawlers. If we increase too much, there is a chance of abuse, apart from resource limit.
But if the number defined in MaxRequestWorkers (previously it was MaxClients) is less than 200, then this means that after 10 child processes, no extra process will start since we have defined an upper cap of MaxRequestWorkers. In this case, we may need to also increase ServerLimit i.e. MaxRequestWorkers /ThreadsPerChild. As you can see, we have done the math for you.
Related Apache documentation: https://httpd.apache.org/docs/2.4/mod/core.html#keepalive