Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to limit the total (not per process) memory Apache uses?

I have a web server that runs PHP and MySQL. I need a PHP memory limit of 1G or some scripts will fail (dompdf/phpexcel). The problem is that sometimes people run those scripts at the same time. Apache uses several GB of memory and MySQL is the one that dies due to lack of memory.

I want to limit it to like 4 GB, so that if 5+ of those scripts are called simultaneously, they fail to run.

I took a look at RLimitMEM, but it is per process.

I use Apache 2.4 on Centos 6 with event MPM.

share|improve this question
A friend suggested that since the limit is per process that I should only allow Apache to launch one process. That seemed a good idea. As a test, I tried: ServerLimit 1 ThreadsPerChild 1 MaxRequestWorkers 1 RLimitMEM 538443776 538443776 and PHP was limited to 1 GB. But it didn't work. The process only stopped when it reached the 1 GB limit imposed by PHP. Reading the documentation again: "This applies to processes forked off from Apache httpd children servicing requests, not the Apache httpd children themselves." So, Apache module is not bounded by RLimitMEM. – Italo Sep 4 '13 at 5:15

I was looking for virtualization solutions when I found just what I was looking for: cgroups. It does exactly what I want: limit the total amount of memory for all Apache processes.

To install it on Centos 6:

yum install -y libcgroup

Edit /etc/cgconfig.conf and add:

group http {
  memory {
    memory.limit_in_bytes = 4G;

Start the cgroups service:

service cgconfig start

You can also use chkconfig to start it automatically.

Finally start Apache under the cgroup:

cgexec -g memory:/http /usr/local/apache2/bin/apachectl start

I did some preliminary tests and it worked. Let's see how it will do tomorrow under normal workload.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.