How To Speedup Apache Server
For many people, 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 Apache.
** This article was written to optimize a Apache/Ubuntu/Linux set up (the most 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 it can get. This is why it’s a good idea to find out which modules you really need for your website(s) to work correctly and unload those which your website doesn’t need. A good way to do this is to unload every single module one by one to find out if it affects your website:
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 if 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 documentation how to use it.
Tune Your MySQL Settings For Performance
If you use a MySQL database there’s a great free little script which can be run in Apache called mysqltuner.pl. It can detect the bottlenecks of 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 ) and change the necessary values to fix the bottlenecks. Go to the website to download the script and for more information how to set it up.
Use A PHP Accelerator
If 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 correctly.
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. If this happens often to your server it’s a good idea to limit the amount of processes to a number your server set up can handle. This way existing visitors will be able to quickly load your website. The downside is that every visitor who reaches the limit amount of processes will get an error and will not be able to load your site. But, and you will probably agree, this is still better as having a site which will be slow or hang for everybody. 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 REScolumn 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.1which 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 thempm_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.