PHP, for all of its shortcomings, is still an extremely popular language and a useful one as well. One of the things that makes it so useful - its dynamic and interpreted nature - also makes it more difficult to achieve decent performance. One area that can greatly improve PHP performance is the proper use of an Opcode cache extension.
A PHP Accelerator (aka Opcode cache) is an extension that caches the output of the PHP bytecode compiler into memory. When a request comes in for content containing PHP code, the Zend Engine compiles the PHP source code on the fly into executable bytecode which can be served up in the response. Since this happens every time a request is made, the same code will be interpreted and compiled over and over again, even though the compiled result is the same, which is a waste of time.
An Opcode cache will insert itself into that interpretation / compile process and cache the compiled result into RAM for reuse. The next time a request for PHP content comes in, the Opcode cache will be checked first and, if available, the cached executable bytecode will be immediately returned. If a cache record does not exist, the Zend engine will go about its business and compile the code, at which point the Opcode will intercept and cache the result for future use before it’s returned.
Since there are basically no side effects to enabling an Opcode cache, it’s recommended that all production sites utilize one. On larger PHP applications like Wordpress or Drupal, performance can generally be increased around 3x by simply turning one on. For busy sites or servers hosting many sites on the same box however, you may need to tune the cache configuration to realize any performance gains.
The specifics of how to tune your cache will depend on your stack, in my case we use XCache + Nginx + PHP5-FPM. Most of the popular accelerators come with a web interface to give you some insight into the cache and help you tune it. A mistake a lot of people make is to just set up the cache and assume it’s working properly. After you install and run the cache for a bit, you should log into the cache web stats page and see how it’s doing.
The big thing to look for is the number of requests which could not be cached due to a lack of available memory for the cache. In XCache, this is the OOMs column (Out of Memory). By default, the cache is usually given 64MB of RAM to use for storage. If you host a bunch of sites, like it the picture above, that storage will quickly be consumed and the number of OOMs will rise into the millions rather quickly, thus eliminating the benefit of an Opcode cache. You should incrementally increase the size of the cache until the OOMs fall to 0, meaning everything that can be cached is being cached.
You can also look at the size of the available cache storage. If that number is consistently high, you’re allocating too much RAM for the Opcode cache and can reduce the size to make that memory available for other processes.
So if you’re running a PHP application, especially if you’re hosting many of them, set up your PHP Opcode cache properly and you’ll see noticeable speed improvements in your sites.