Install and configure APC on IIS

Categories:

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.

Download APC
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.

Enable APC
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;
extension=php_apc.dll
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.

Configure APC

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;

apc.shm_size=64M


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;

<?php
phpinfo
();
?>

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.

Average rating
(3 votes)

Comments

Anonymous's picture

APC and PHPMyAdmin

APC seems to stop PHPMyAdmin from working. Do you recommend a workaround?

Brashquido's picture

I have struck this problem

I have struck this problem too. I've yet to investigate why this is and for the moment am disabling APC just for phpMyAdmin using per directory values for my PHP config.
----------------
Dominic Ryan
3 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous's picture

Could you explain further?

Could you explain how to turn off APC on a per directory basis? I looked through your article on per-directory php ini setting switches via the registry, but I couldn't see how it would be possible to turn off APC using this method.

Any help would be appreciated!

Brashquido's picture

Set apc.enabled directive

You can use the method in that article to set apc.enabled to 0. This in effect will disable APC.

----------------
Dominic Ryan
5 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster

Anonymous's picture

I can see apc loaded using phpinfo and also apc.php shows up

I can see apc loaded using phpinfo and also apc.php shows up.

However, in apc.php it only shows that apc.php is the only cached script file.

I have drupal 6.19 vanilla installed with php 5.2.14 on IIS 6 with ISAPI module.

I should see index.php in the cached file among the many other drupal includes/modules also.

Mike

Brashquido's picture

Have you looked at WinCache?

Hi Mike,

I've not seen that one before. I take it all your APC directives in php.ini are at their default values? Another thing to consider is that this article is now getting onto being 3 years old, and a lot has changed for PHP on IIS since then. These days I'd actually recommend installing PHP to use the FastCGI module and the WinCache opcode cache which is maintained by the IIS team themselves.

----------------
Dominic Ryan
6 x Microsoft IIS MVP, MCSE, MCSA
IIS Aid owner/webmaster