How do I tune Apache web server's memory allocation to prevent out of memory errors in an EC2 Linux instance?
My Apache web server running on an Amazon Elastic Compute Cloud (Amazon EC2) Linux instance is intermittently unresponsive. I see messages in the instance's system log such as "out of memory", "oom", "oom-killer," "failure to fork process," or other insufficient memory.
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 aren't 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
Amazon Linux, 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 that 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, set the default value for the variable MaxRequestsPerChild to 4500.
To set 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 MaxRequestsPerChild variable to the value as shown in the example in step6.
5. Calculate a value for the ServerLimit and MaxRequestWorkers configuration variables as follows:
If your instance has more than 4 GB of RAM, then 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, then 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, then 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 MaxRequestWorkers and ServerLimit configuration variables in the prefork.conf file and save your changes.
For example:
IfModule mpm_prefork_module StartServers 10 MinSpareServers 20 MaxSpareServers 40 MaxRequestWorkers 112 ServerLimit 112 MaxRequestsPerChild 4500 /IfModule
7. Restart the web server by running the following command from a terminal session:
sudo service httpd restart
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, then update the httpd.conf file on the instance with the new configuration value, save your changes, then 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, then 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, then 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
Related information
Tutorial: Install a LAMP web server with the Amazon Linux AMI

Relevanter Inhalt
- AWS OFFICIALAktualisiert vor einem Jahr
- AWS OFFICIALAktualisiert vor 2 Jahren
- AWS OFFICIALAktualisiert vor 7 Monaten
- AWS OFFICIALAktualisiert vor 2 Jahren