Rahul's Tech Blog

Growth Hacking, Just Hacking or No hacking !!!

How to tune apache for maximum performance ?

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. 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 hulk 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. You are dead ! 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 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 (multiple in fact, but I will discuss only one, it doesn’t mean that I don’t know about others. Don’t judge me, Nerd !!! ) engine 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. As sidenote, 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 some configuration changes to enable php5 support in apache2 mpm_event. Google it or here is an explanatory guide for that
http://jaswsinc.com/ubuntu-lamp-apache-mpm-event-fastcgi-php5-fpm/