How to speed up Apache.


The easiest solution to fixing a slow server is adding more hardware in the form of RAM or CPU power. But there’s often a cheaper solution: maximizing the performance of your existing system. Below are some useful tips to speed up a Apache server.
** This article was written to optimize a Apache/Linux/Ubuntu set up (a common Apache set up) but could be used for other set ups as well

Disable modules you don’t use

Every server install comes with a set of automatically enabled modules. Usually the more modules your server loads, the slower the server can get. This is why it’s generally a good idea to find out which modules you really use for your website(s) and apps and unload those you don't use. A good way to do this is to unload every single module one by one to find out if it affects your website or app:
1. Navigate to the /etc/apache2/mods-enabled/ folder to find out which modules are enabled on your server.
2. Make a copy of this list so you know which modules to enable again in case you wrongfully disable a module (you can find every available mod back in the /etc/apache2/mods-available/ folder).
3. Now disable each module one by one using the sudo a2dismod module command in SSH access where module should be replaced with the name of the module you want to disable.
4. Check your website every time you disable another module. No problems? Leave the module disabled. Problems? Re-enable the module by typing and executing sudo a2enmod module (replace module with name of the module you want to enable).

Use Apache caching

Caching frequently accessed files and content can increase your server performance. One of the best Apache applications right now for this is Varnish Cache, which is completely free to use. Installing this application in front of a Apache server can speed up delivery by a stunning 1000 times (going by their own words). Go here to install the latest release on your server and here for the documentation on how to use it.

Tune your MySQL settings for performance

When you use a MySQL database there’s a great free little script which can be run in Apache called mysqltuner.pl. It can detect bottlenecks inside your MySQL databases. When you run the script from the command line in Apache you can instantly see where the bottlenecks occur and how you have to fix them. After that you can edit the MySQL config file (etc/mysql/my.cnf in our case) and change the necessary values to fix the bottlenecks. Go to their website to download the script and for more information on how to set it up.

Use a PHP accelerator

When you are using PHP for your website (which is likely) you should install a PHP caching/accelerating server application. These type of applications store the compiled script bytecode in the shared memory to be able to serve it from the cache whenever website users request it. The most commonly used right now is ZEND OPcache which is automatically installed with PHP 5.5.0 and later. Go to the the website to learn how to set it up to get it working on your server.

Reduce the amount of processes

Sometimes, especially on smaller servers with low RAM, the server can get overloaded with too many processes at the same time and slow down significantly or even hang. When this happens often to your server it’s a good idea to limit the amount of processes to a number your server can handle. This way existing visitors will be able to quickly load your website. The downside is that when the limit amount of processes are reached visitors will get an error and will not be able to load your site. But, and you will probably agree, this is still better than having a wesite that is extremely slow or will hang for a long period of time. Follow these steps:
1. First find out how much RAM you have available for Apache processes. Usually this amounts to the RAM you have left after you’ve discounted the amount you use for MySQL and other server components you use RAM for. As an example we take 250mb.
2. Now run the top command in SSH access. In the list that follows first find a www-data process. Once you see one quickly look for its RES column and see which value it has, for example: 5109 or 19m. The example value 5109 stands for 5.1mb and 19m for 19mb. This RES value is the amount of RAM every single process needs.
3. Now we take the amount of RAM we have available for Apache and we’ll divide it with the amount every process needs. So let’s divide the example amount of 250mb of free Apache RAM and divide it with the 5.1mb example RES value, in other words: 250 divided by 5.1 which has a outcome of about 49. This is the amount of processes the example Apache set up with 250mb reserved for Apache processes can handle. If you go over this limit, the server can get overloaded.
4. Now we know the amount of processes the example set up can handle we need to change the MaxClients value in the Apache config file. We can do this by editing the /etc/apache2/apache2.conf file in SSH access. We search for the MaxClients value under the mpm_prefork_module section and change it to 49. Now the server has a limit of 49 processes. Every additional process which goes over that amount will now be blocked instead of slowing down the server. Keep in mind, the 49 value is just an example value. You need to find out and calculate the fitting number for your server by yourself.