19
Practical LAMP Optimisations
There are numerous articles floating around in internet land eschewing the various ways to squeeze the very last drop of performance from PHP. Unfortunately most of the time these articles list micro-optimisations that are quite frankly a complete waste of time. Two of the all-time classics have to be “echo is faster than print” and the whole “single vs double quoted strings”.
What most of the articles seem to ignore, or at least gloss over, is that PHP rarely stands on it’s own. More often than not it’s a single component of a larger (typically LAMP based) application.
There’s no point changing all instances of print to echo or worrying about which quoting style to use, while ignoring the mammoth database query that runs on every page request against fifteen unindexed tables containing millions of records.
The best way to ensure good application performance is to write Good Code™ and implement Good Database Design™ in the first place. Both of which are topics I’ll be covering in the future. In the mean time, here’s my guide to practical optimisations for LAMP applications…
- Use an opcode cache such as APC or XCache.
This is mostly likely the single biggest performance gain you will achieve with PHP! - Use E_STRICT and don’t suppress errors.
Setting error_reporting toE_STRICTand not using@to suppress errors and warnings will catch most common code bugs such as undefined variables and unquoted strings. Use the following to ignore errors when absolutely necessary:
$old = error_reporting(0);
action();
error_reporting($old); - Identify slow queries.
Turn on the Slow Query Log in MySQL and useEXPLAINto examine offending queries. - Make Fewer HTTP Requests.
Combine and minify your Javascript and CSS files. Consider implementing CSS sprites to reduce requests for images. - Enable output compression on the webserver.
This will reduce the amount of data travelling over the network, reducing the time required to load each page. - Use the Expires header.
This will greatly reduce the number of requests for static content such as images, Javascript and CSS.
The above tips should be at the top of your optimisation list as they are almost certainly the ones that will provide the largest performance increases. Once they have been taken care of we move into the realms of more specific optimisations of code, queries and server settings; which I’ll be covering in future posts.