How do I tune memory allocation for an Apache web server running on an Amazon EC2 Linux instance?

Last updated: 2021-01-04

My Apache web server running on an Amazon Elastic Compute Cloud (Amazon EC2) Linux instance is intermittently unresponsive. I'm seeing messages in the system log for the instance about "oom-killer," "failure to fork process," or other insufficient memory. How do I fix this?

Short description

To view the system log to verify error messages:

  1. Open the Amazon EC2 console, and then select Instances.
  2. Select the checkbox for the instance.
  3. Choose Actions, Monitor and troubleshoot, Get system log.

Note: If you're not using the new Amazon EC2 console, select Actions, Instance Settings, Get System Log.

If you establish a terminal session to the instance, you might see stack traces in the location appropriate for the distribution of your Linux instance.

Debian and Ubuntu: /var/log/syslog

CentOS and RHEL: /var/log/messages

Systems using systemd: journalctl

Resolution

Intermittent stoppages and insufficient memory error messages might indicate that memory for the instance is exhausted.

You can set limits on the number of connections that the server accepts and the number of processes it starts. You can get the limit value by calculating the typical memory use of an Apache process and dividing the total memory you want to allocate to Apache by that average.

If you're running Apache 2.4, do the following:

1.    Initiate a terminal session to the 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 instance.
Sort the list in descending order by 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.

3.    Scan the column of %MEM values returned for Apache processes and determine an average value.

4.    Look for one or more Apache processes with an unusually large %MEM value compared to the %MEM value of other Apache processes. A large %MEM value might be a memory leak in a web application running on the server. To mitigate the impact of a potential memory leak, change the default value for the configuration variable MaxRequestWorkers from 4000 to 1000.

To change the configuration variable:
Create a new file inside the /etc/httpd/config.d directory. In the following example command, the new file is prefork.conf.

$ sudo vim /etc/httpd/config.d/prefork.conf

Set the MaxRequestWorkers variable to the new value.

5.    Calculate a value for the ServerLimit and MaxClients configuration variables as follows:

If your 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% (0.9) by 0.8% (.008) for a result of 112.5, and round down to the nearest whole number, 112 in this case.

If your 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% (0.8) by 0.8% (.008) for a result of 100.

Note: These values are calculated with the assumption that the instance is a dedicated web server. If you're hosting other applications on the server, subtract the total percentage memory use of 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.

6.    Update the MaxClients and ServerLimit configuration variables in the instance's httpd.conf file with the new value and save your changes.

For example:

MaxClients = 112
ServerLimit = 112

7.    Restart the web server by running the following command from a terminal session:

apachectl graceful

If you're running Apache 2.2, do the following:

1.    Initiate a terminal session to the instance. If you are unable to 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 instance.
Sort the list in descending order by 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.

3.    Scan the column of %MEM values returned for Apache processes and determine an average value.

4.    Look for one or more Apache processes with an unusually large %MEM value compared to the %MEM value of other Apache processes. A large %MEM value might be a memory leak in a web application running on the server. To mitigate the impact of a potential memory leak, change the default value for the configuration variable MaxConnectionsPerChild from 4000 to 1000.

This change provides some relief for the problem while you identify and correct the source of the memory leak. If you suspect a memory leak, update the httpd.conf file on the instance with the new configuration value, save your changes, and skip to step 7.

5.    Calculate a value for the ServerLimit and MaxClients configuration variables as follows:

If your 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% (0.9) by 0.8% (.008) for a result of 112.5, and round down to the nearest whole number, 112 in this case.

If your 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% (0.8) by 0.8% (.008) for a result of 100.

Note: These values are calculated with the assumption that the instance is a dedicated web server. If you're hosting other applications on the server, subtract the total percentage memory use of 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.

6.    Update the MaxClients and ServerLimit configuration variables in the instance's httpd.conf file with the new value and save your changes.

For example:

MaxClients = 112
ServerLimit = 112

7.    Restart the web server by running the following command from a terminal session:

apachectl graceful

Did this article help?


Do you need billing or technical support?