Walter shows you how to
tune memory allocation for an
Apache webserver


The Apache web server running on my Amazon Elastic Compute Cloud (Amazon EC2) Linux instance intermittently becomes unresponsive due to memory pressure on the instance. What can I do to fix this?

Two of the most common reasons for memory pressure on an instance running an Apache web server are application memory leaks, or inefficient Apache configuration that causes swapping.

System log messages such as "oom-killer," "failure to fork process," or other messages about insufficient memory usually mean that the memory for the EC2 instance is exhausted. 

Viewing system log messages

You can view system log messages from either a terminal session or from the Amazon EC2 console.

To view system log messages from a terminal session, establish a terminal session to the EC2 instance. You can view the system log messages for your EC2 instance in one of the following locations:

Linux Distro System Log Location
Debian or Ubuntu /var/log/syslog
CentOS or RHEL /var/log/messages
Systems using systemd journalctl

To view system logs from the console, open the Amazon EC2 console, and then select the EC2 instance. From the Actions menu, choose Instance Settings, and then choose Get System Log.

Detecting memory leaks

1.    Initiate a terminal session to the EC2 instance. If you can't connect, you might need to restart the instance.

2.    From the terminal session, run the top command to display a list of memory-resident processes on the EC2 instance.

3.    Sort the list in descending order by the percentage of memory used. To sort on an rpm-based instance, press Shift+O, and then press n. On other Linux distributions, choose the appropriate option to sort processes by memory usage.

4.    Scan the column of %MEM values returned for Apache processes, and calculate an average value.

5.    If one or more Apache processes has an unusually large %MEM value compared to the %MEM value of other Apache processes, there might be a memory leak. This memory leak might be due to a web application running on the server.

6.    Edit the httpd.conf file for the EC2 instance, and change the default value for the MaxConnectionsPerChild configuration variable from 4000 to 1000. Note that MaxConnectionsPerChild (Apache 2.3.9 or later) was called MaxRequestsPerChild. The old name is still supported.

This change reduces the memory pressure on the EC2 instance until the source of the memory leak can be identified and addressed.

7.    From the terminal session, restart the web server by running the service command:

service httpd graceful

Reducing the potential of swapping

You can set limits on the number of processes that are spawned by setting the MaxRequestWorkers and ServerLimit configuration variables. Note that MaxRequestWorkers was called MaxClients before Apache 2.3.13. The old name is still supported.

Limiting the number of processes to serve users prevents the server from swapping due to a shortage of RAM. The limit value is calculated by dividing the typical average memory use of an Apache process into the total percent of memory that you want to allocate to Apache.

1.    Calculate a limit value for the MaxRequestWorkers (or MaxClients) and ServerLimit configuration variables as follows:

If your EC2 instance has more than 4 GB of RAM:

Divide the average %MEM value for Apache processes into 90%. For example, given an average %MEM value of 0.8%, divide 90% by 0.8%

0.9 / 0.008 = 112.5

Round the result down to the nearest whole number. In this case, the limit value is 112.

If your EC2 instance has 4 GB of RAM or less:

Divide the average %MEM value for Apache processes into 80%. For example, given an average %MEM value of 0.8%, divide 80% by 0.8% for a result of 100.

Note: These values are calculated with the assumption that the instance is a dedicated web server. If you are hosting other applications on the server, subtract the total percentage of memory used by these applications from either 90% or 80% before doing the calculation. Performance might decrease if you run other applications in addition to Apache on an instance with 4 GB of RAM or less.

2.    Edit the EC2 instance's httpd.conf file, update the MaxClients (or MaxRequestWorkers) and ServerLimit configuration variables, and save your changes. For example:

MaxClients  112 
ServerLimit 112

3.    Restart the web server by running the service command from a terminal session:

service httpd graceful

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.

Published: 2016-01-07

Updated: 2019-01-04