How to tune apache for maximum performance ?

Rahul Prasad
A Coder’s Laboratory
3 min readMay 25, 2015

I started with apache2 because, lets face it, it comes default with ubuntu & mac. If you are wondering “what about windows?”, then this article is not for you, xampanzee !

Before we begin, let me tell you, Shortest way to achieve maximum performance is to use Nginx instead of apache2 :)

Apache is slow because of its age old architecture. It creates a thread for every connection. Each thread captures large chunk of memory (10–20 MB). If you have enabled PHP support then apache embeds php interpreter (mod_php) in each of its thread irrespective of content being served, which will increase memory to 25–35 MB. You have limited RAM so number of parallel connection is dependent it. Which is also set to 150 by default.

Check MaxRequestWorkers value in
/etc/apache2/mods-enables/mpm_preform.conf

If your server has more than 5 GB RAM. Then you can increase it to 256. If you have a even bigger server with more than 10 GB of RAM you can still increase it. Just add one more line ServerLimit xxx. Now you can keep MaxRequestWorkers as xxx.

The real issue here in not that you have limited RAM. Issue is that end users have shitty internet. Your server processes data in mili seconds and client take seconds to download it. That keeps the thread for an end-user alive. If you have enough number of users per second, these threads will start piling up and once you reach MaxRequestWorkers value, next request will be in queue.

To solve this either you have to change the way apache work or just apt-get install nginx :)

Lets go ahead with 1st way, we will do it the hard way, because who doesn’t like to screw their servers.

Nginx doesn’t has this issue because of its event based architecture. It takes only 1–2 MB of RAM per connection. Apache came up with similar approach. Apache designed an engine (multiple in fact, but I will discuss only one) which is event based. You can enable it using sudo a2enmod mpm_event. Sad thing about mpm_event is, it does not work with vanilla php. You have to apt-get install php5-fpm and tell apache to process php files using php5-fpm. php5-fpm runs as a service. You have to restart it if you make any changes to php configuration (sucks isn’t it?).
You need to make configuration changes to enable php5 support in apache2 mpm_event. Here is an well commented configuration taken from http://web.archive.org/web/20150110175604/http://jaswsinc.com/ubuntu-lamp-apache-mpm-event-fastcgi-php5-fpm/

<IfModule fastcgi_module>
# Add a new action that points to a virtual (non-existent) handler.
# I will repeat, NON-EXISTENT. This is a virtual (non-existent) handler.
# i.e. `/fastcgi.php5-fpm` does not exist, that's OK :-)
# The name of our handler is decided here. Let's call it `fastcgi-php5-fpm`.
# See also <http://httpd.apache.org/docs/current/mod/mod_actions.html#action>

Action fastcgi-php5-fpm /fastcgi.php5-fpm virtual

# Now let's add an alias mapping that gives meaning to our virtual handler.
# This forwards requests hitting our virtual (non-existent) handler;
# sending them to FastCGI; which is yet another virtual handler.
# I repeat, NON-EXISTENT. This is a virtual handler that does not exist.
# i.e. `/var/www/cgi-bin/fastcgi.php5-fpm` does not exist, that's OK :-)
# See also <http://httpd.apache.org/docs/current/mod/mod_alias.html#alias>

Alias /fastcgi.php5-fpm /var/www/cgi-bin/fastcgi.php5-fpm

# Now, let's setup FastCGI so it works w/ our virtual handler and PHP5-FPM.
# See also <http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiExternalServer>

FastCgiExternalServer /var/www/cgi-bin/fastcgi.php5-fpm -socket /var/run/php5-fpm.sock -idle-timeout 900 -pass-header Authorization -pass-header Range

# Associate our handler with PHP files; including PHAR files.

AddHandler fastcgi-php5-fpm php phar

# Now let's configure security-related options in our CGI bin.

<Directory /var/www/cgi-bin>
AllowOverride none
Options FollowSymLinks
<IfModule authz_core_module>
# Only when redirected internally by FastCGI.
Require env REDIRECT_STATUS
Options +ExecCGI
</IfModule>
</Directory>
</IfModule>

--

--