How to Limit Memory Usage in Apache

limit memory usage apache

By default, Apache comes with a lot of default modules and settings. As your website traffic grows, this can lean to high memory usage and memory limits. Let us take a look at how to limit memory usage in Apache.


How to Limit Memory Usage in Apache

Here are the steps to limit memory usage in Apache.


1. Reduce Concurrent Connections

Too many simultaneous connections is a common reason for high memory usage in Apache. You can limit it with the following parameters

  • MaxClients
  • MaxSpareServers
  • MinSpareServers
  • StartServers
  • MaxRequestsPerChild
  • KeepAlive
  • KeepAliveTimeout
  • TimeOut

Here are the steps to reduce concurrent connections in Apache and free up some RAM.


2. Replace Prefork with Worker MPM

By default, Apache uses the Prefork module that can result in creation of too many child processes. Each child process can only handle one connection at a time.

If you have a high traffic website, it is better to switch to Worker MPM module. It uses threading to manage multiple connections in each child process. Each thread manages one connections. This reduces memory usage of Apache.


3. Reduce number of modules

Over time, you may end up adding many modules to Apache causing it to consume a lot of memory. Delete the modules that you don’t need

Get a list of all available modules in apache with the command

$ sudo a2enmod

You will see a list of modules

  • proxy_balancer_module (shared)
  • proxy_ftp_module (shared)
  • proxy_http_module (shared)
  • proxy_ajp_module (shared)
  • proxy_connect_module (shared)
  • cache_module (shared)

Delete unnecessary module with a2dismod command

$ sudo a2dismod proxy_ajp_module


4. Reduce logging

Apache logs a lot of messages, especially if your site gets high traffic. If you use other analytics tools like Google Analytics, you can set Apache to log only error messages.

You can also pass its log to third party service like logrotate, which will periodically clean up logs and rotate them.


5. Optimize PHP and MySQL

Many times, Apache’s high memory usage is because of issues in PHP and MySQL.

For example, by default, Apache uses mod_PHP. Use PHP-FPM to reduce memory usage.

In MySQL, adjust key buffer size, query cache, table cache to reduce memory usage.


It’s important to try each of the above methods, one by one, to see which one fixes your problem.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!