The most crippling bottleneck for many PHP-based software is almost always related to memory consumption. The PHP command interpreter does an excellent job for any low-traffic website, but when your WordPress blog (or Joomla! commercial website) jumps out of the unknown and really starts to hit the mark, you can bet you will soon face the issue: pages becoming slower, requests hanging out and often reaching the 503 - Service Unavailable or other timeout errors, and other stuff that will drive crazy your monitor tools like Yslow (if you don't use it yet, you better start now!), Analytics and so on
To drastically improve the performance of any high-traffic PHP website running under IIS you can consider installing one of the free caching PHP extension tools available: in this post we'll cover the Microsoft one, namely WinCache, which is included in all the base PHP installation packages for Windows since version 5.2. This neat extension grants a serious performance boost thanks to a five-level caching mechanism:
- OPCode Cache: Caches the compiled bytecode of the most used scripts, so the system can avoid to perform the same identical task multiple times.
- File Cache: Caches the php files by loading them in a shared memory block, so they can be easily accessed by all processes avoiding expensive IO/file operations.
- Resolve File Path Cache: Stores the mappings between relative and absolute file paths, thereby reducing the number of path resolutions that the PHP engine has to perform when it needs to calculate the absolute file path from its relative-to-current-folder reference.
- User Cache: Stores any PHP objects and variables built for each user to reuse them for subsequent requests. This can be used to improve performance of PHP scripts and to share the data across multiple PHP processes.
- Session Cache: Stores the PHP session data in the shared memory cache: this avoid file system operations for reading and writing session data, which improves performance when large amount of data is stored in PHP session.
Installing WinCache is fairly easy. The first thing you need to do is to check whether the php_wincache.dll file is included in your PHP installation or not. This file is located in the /ext/ subfolder of your PHP installation folder. If it's not there, you can download it manually from the WinCache official website (just be sure to pick the file matching your PHP version), unpack it and copy the .dll file to the aforementioned folder.
Then you need to enable the WinCache extension to your PHP installation by adding the following two lines to your php.ini file, which is usually located in your PHP installation folder OR in the Windows root folder:
1 2 |
[PHP_WINCACHE] extension=php_wincache.dll |
IMPORTANT: Before you do that you might want to check if the extension is already enabled by doing a quick text search for "php_wincache.dll" inside the file.
If you're using PHP Manager for IIS (which you really should) you could also install the WinCache extension directly from the PHP Manager GUI.
If you're NOT using PHP Manager for IIS and/or you have no idea of where your php.ini file actually is, you can easily track it by executing the phpinfo() command on your server (if you don't know how, just click here to download a sample script, put it on your server and execute it): as soon as you did that you can review your whole PHP configuration, which contains the full path of the currently Loaded Configuration File:
That same phpinfo() command can be also used to check whether the WinCache extension has been properly installed or not. If that's the case you will be able to see something like that by scrolling down a little:
In order to do so we need to add some additional configuration settings to our php.ini file:
1 2 3 4 5 6 7 8 9 10 11 12 |
[WinCache] ; Full reference: http://php.net/manual/en/wincache.configuration.php wincache.fcenabled = 1 wincache.ocenabled = 1 wincache.ucenabled = 0 wincache.fcachesize = 64 wincache.fcndetect = 1 wincache.maxfilesize = 256 wincache.chkinterval = 10 wincache.enablecli = 1 wincache.ucachesize = 8 wincache.scachesize = 8 |
These settings are generally OK for most production environments. You can fine-tune them by taking a look to the WinCache reference guide, where you will find a detailed description of any possible configuration setting to adapt WinCache to your needs.
When you think you're done, save the php.ini file and restart IIS: you'll be able to check the results of your hard work by executing the phpinfo() command again:
That's about it: enjoy caching!