I have not worked on apache for a long time (if you don’t know, I am a big fan of NginX with PHP-FPM instead of surviving with apache :lol:), so in fact I do not have much experience on tweaking apache. Today when monitoring a server with an apache-based system, I recognized that httpd processes eat ram like a hungry dog *__*. I cannot imagine that one httpd process can eat up to 1.5G RAM of my servers. As a result, RAM is quickly used up, and then SWAP is used. And when the swap is full, all services will be crashed and be stuck. It is not fun this case, so I decided to look into apache configuration to extend the serving capability of the server. Below are some small tweaks for apache that I got from Google search and my experiment case. If you need more references, scroll down to this blog entry and you can find some links there.
- First, if you do not want to spend too much time reading many (stupid) verbose documents, go with ApacheBuddy. Similar to MySQLTunner, that buddy can analyze your server configuration and usage, and then will propose what is necessary to be tweaked. Sample steps are as follows:
[bash]
[root@CENTOS6 ~]# wget https://raw.githubusercontent.com/will-parsons/apachebuddy.pl/master/apachebuddy.pl
[root@CENTOS6 ~]# chmod +x apachebuddy.pl
[root@CENTOS6 ~]# ./apachebuddy.pl
########################################################################
# Apache Buddy v 0.3 ###################################################
########################################################################
Gathering information…
We are checking the service running on port 80
The process listening on port 80 is nginx:
The process running on port 80 is not Apache.
Falling back to process list…
Apache has been running 0d 0h 36m 14s
The full path to the Apache config file is: /etc/httpd/conf/httpd.conf
Apache is using prefork modelExamining your Apache configuration…
Apache runs as bitrix
Your max clients setting is 36Analyzing memory use…
Your server has 7831 MB of memory
The largest apache process is using 126.49 MB of memory
The smallest apache process is using 9.94 MB of memory
The average apache process is using 41.28 MB of memory
Going by the average Apache process, Apache can potentially use 1486.09 MB RAM (18.98 % of available RAM)
Going by the largest Apache process, Apache can potentially use 4553.64 MB RAM (58.15 % of available RAM)Generating reports…
### GENERAL REPORT ###Settings considered for this report:
Your server’s physical RAM: 7831MB
Apache’s MaxClients directive: 36
Apache MPM Model: prefork
Largest Apache process (by memory): 126.49MB
[ OK ] Your MaxClients setting is within an acceptable range.
Max potential memory usage: 4553.64 MBPercentage of RAM allocated to Apache 58.15 %
———————————————————————–
———————————————————————–[/bash]
- Playing with KeepAlive and KeepAliveTimeout directives. In many real world case, we can disable KeepAlive or set KeepAliveTimeout < 5 will result good result, if we do not need to wait for more request from the same client before closing the connection. However if your web application requires to hold a connection for a long time (e.g. user activity is frequently such as instant message, socket, etc.), put a reasonable value is much better. In my case, I end up allow KeepAliveTimeout to 45 due to high frequent activity from website users.
- MaxClients directive should be calculated and considered carefully. Many sources claim that “This setting helps Apache fly when your server is getting hit hard”. As documented: The MaxClients directive sets the limit on the number of simultaneous requests that will be served. Any connection attempts over the MaxClients limit will normally be queued, up to a number based on the ListenBacklog directive. Once a child process is freed at the end of a different request, the connection will then be serviced.
- This should be calculated by your max allowed RAM and max/average memory per apache/httpd process.
- For example, if you have a 8G RAM server with 4G dedicated for MySQL, you can use up to 4G for all httpd process. We can see how much RAM each httpd process is consuming:
[bash]ps aux | grep ‘httpd’ | awk ‘{print $6/1024;}’ | awk ‘{avg += ($1 – avg) / NR;} END {print avg " MB";}'[/bash]
. Of course you can choose max value by checking current httpd process memory usage:
[bash]ps aux | grep ‘httpd’ | awk ‘{print $6/1024 " MB";}’ | sort -rn[/bash]
- In my case, I use the average value and it is 54.3368 MB, so the possible MaxClients = 4G / 54.3368 MB ~ 75. So I use this value for MaxClients.
- Do not set AllowOverride to On globally. If you can control the whole system in a VPS/server, set it manually to each place that you need. This will reduce time for searching for all folders for a .htaccess to parse from apache.
There are plenty of other things that can be tweaked, however each case / application needs to be tweaked differently so in many cases, trying to experiment different settings for a directive/param might help.
References:
- 3 Small Tweaks to make Apache fly and The discussion at Reddit
- Tuning Your Apache Server
- httpd memory usage