3 Quick Optimisations for Your PHP Site

3 Quick Optimisations for Your PHP Site

Many websites and content management systems (CMS) these days run on the tried and tested LAMP platform (Linux, Apache, MySQL and PHP). I’m going to outline three quick ways to optimise the page response time for your web application on your VPS. Whether it be a WordPress blog, a larger Joomla or Drupal site, or even a heavy ecommerce store such as Magento, the following tips are basic optimisations every webmaster should know.


The first optimisation is to increase the PHP memory limit, this can be done in multiple ways depending on permissions.

With root access to your server (such as on a virtual or a dedicated server), edit the php.ini file to increase the php memory limit. Depending on how big your web application is, the limit can be raised as needed, I recommend 128MB as a good starting point. Keep in mind that this only limits the memory usage of your PHP scripts, it does not set aside that resource for PHP. This means that having a limit which is too high can increase the risk of PHP crashing your server. So start with 128MB and make sure that further increases are actually increasing response time; it is not wise having a limit of 512MB if PHP only uses 100MB during peak times. With that in mind, let’s get started:

Open your php.ini file and look for the ‘memory_limit’ line (on a cPanel server, php.ini can be found at /usr/lib/php.ini, otherwise it’s probably in /etc/php.ini)

> memory_limit = 128M

Without root, you can try adding the following to .htaccess in the site’s public_html directory:

> php_value memory_limit 128M

Apart from the memory limit, there are many further optimisations that can be made to PHP including installing a caching system such as APC or eAccelerator. For the moment though we’ll stick with the basics and move onto some more advanced optimisations in a future post.


MySQL offers perhaps the most important optimisation for high load dynamic web sites. In particular, the Query Caching setting can be tweaked to improve performance significantly. This caches all queries to your database

To check if the query cache is enabled, login to mysql with root:

# mysql -u root -p

After you’ve entered the password type the following statement at the prompt:

mysql> SHOW VARIABLES LIKE ‘query_cache_size’;

The value shows the size the cache is currently set to in bytes. if it is 0, you can increase the query cache size by adding the following line to end of /etc/my.cnf:

> query_cache_size=32M

Depending on the size and load of the web application as well as available RAM and the size of your database, tweak the cache size accordingly. I’d recommend setting the cache size at a minimum of 16MB to start and then incrementally test higher figures. I recommend testing 16MB, 32MB and 64MB and see how the performance grows. Just as with PHP, when the gains are no longer noticeable, it’s probably time to stop increasing the size!


Apache can do zip compression on the fly via mod_deflate. Using mod_deflate will significantly lower your bandwidth throughput, but at the cost of raising your CPU usage. For this reason, please check your resource usage and think carefully about the tradeoff.

It’s better not to enable mod_deflate server wide as it can be quite CPU intensive, so the best way to optimise Apache with compression is to edit the .htaccess file in your web application’s directory. Once open, find the section under ‘<IfModule mod_deflate.c>’ and uncomment the appropriate lines listed below:

<IfModule mod_deflate.c>

SetOutputFilter DEFLATE

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

BrowserMatch ^Mozilla/4 gzip-only-text/html

BrowserMatch ^Mozilla/4\.0[678] no-gzip

BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Header append Vary User-Agent env=!dont-vary


There you have it, a few tips to tune the performance of your Apache/MySQL/PHP application that I’ve discovered in my research. I suggest you test these settings carefully as you implement them to ensure they are giving you actual gains. All optimisations require trading off resources, so there is no set formula for your particular site. If you’re implementing these changes on a live server, I highly recommend you do them one at a time so that if you run into any issues you are easily able to diagnose the problem and roll back if required.

Good luck and happy optimising!