Install and configure APC on IIS
Previously using PHP4 with FastCGI I was using eAccelerator as my PHP opcode / bytecode accelerator as APC was too unstable causing several crashes a week. However since upgrading to PHP 5.2.5 using ISAPI I have migrated across to APC and to date have found it to be very stable. So I thought it about time that I cover the topic of installing APC into your PHP environment for IIS, which is what we'll cover in this article.
APC is not included in the standard Windows package for PHP, but it is included in the PECL binaries package which can be downloaded from the PHP site. One word of caution is that APC is not stable when using PHP on IIS using Microsoft's new FastCGI handler due to the memory management within APC itself, so in this article we'll be expressly talking about using APC on IIS when PHP is configured using the ISAPI module. Also be aware that using PHP on IIS in ISAPI mode does restrict you to what PECL extensions you can use as not all of them are thread safe.
Before we enable the APC extension, first make sure you have correctly set your extension_dir value in php.ini. This setting tells PHP where to look for the PECL extensions when loading. With php.ini open you add the following line to the list of enabled extensions;
As covered in my previous how to guide on Enabling PHP PECL extensions for Windows, you will notice that the only difference between an enabled an disabled PECL extension is that the semicolon is removed from in front of the enabled ones.
At last count there were 26 APC configuration directives, so rather than go through them all I just through the ones that generally need alteration over the default values in an IIS environment. To specify your APC directives in php.ini, you should first create a titled section called [APC] and then set your directives in the format of directive=value. For example;
The APC directives I've found useful for PHP on IIS are;
- apc.shm_size - This directive specifies the size for each shared memory segment in MB. The default here is 30MB which I generally increase substantially so all my scripts are able to fit into the cache. This does depend on your system resources tough, but keep in mind you are only really going to get the benefit of an opcode caching system if when used with physical memory.
- apc.shm_segments - If you have allocated as much memory as you can to APC you can raise this value from the default of 1 to segment you APC memory space.
- apc.max_file_size - This is the maximum amount of memory any one PHP script can occupy in memory. The default here is 1MB which I tend to find is a bit lean, so unless you really don't have a lot of memory to spare feel free to increase this to 2MB which should get all your scripts.
- apc.num_files_hint - If you have thousands upon thousands of PHP scripts, then you may want to set this to equal roughly the number of files you are planning to host. The default is 1000 which is enough in a lot of cases.
- apc.ttl - This is the number of seconds an idle cache entry is allowed to occupy a slot in memory before being replaced by other active scripts (if any). I usually leave this to the default of 0 which means cache entries never expire, however if your system has limited physical memory available you may want to look at using this so that only your most used scripts occupy memory.
- apc.rfc1867 - Unlike the others this is one directive you SHOULD NOT alter at all. This option is off by default and is not threadsafe, and therefore not safe or stable to run under IIS when PHP is configured in ISAPI mode.
As mentioned there are many other APC directives which I have not included as they are either not relevant to using PHP on IIS, their default values are fine or I from my testing I suspect them of causing instability in the PHP environment.
Restart IIS & Verify
That should be it for configuring APC, now all you should have to do is restart IIS to make the changes effective. You can verify APC is loaded using the phpinfo function by placing the following code in a file with a php extension in your website root and accessing it with your web browser;
Even better is if you goto the APC page on the PECL website and download the source. Amongst the source files you'll find a file called apc.php which is a script that provides some very useful stats and functions for APC. Simply place this script somewhere on your website (preferably secure) so you can then use it to keep an eye on things.
Hopefully this guide is enough to get you going with an opcode / bytecode accelerator for PHP on IIS, as it really is one of if not the biggest single performance increase you'll get. As always feel free to leave a comment or post in the forums if you wish to leave any feedback.