Installing APC in Windows
Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Installing APC in Windows.
- 1 Introduction
- 2 Installation on Windows Server
- 3 Monitoring and tuning the cache
- 4 Checking performance
- 5 Uninstalling APC
- 6 See also
APC (Alternative PHP Cache) is a PHP opcode cache. It stores PHP pages in RAM and reduces hard disk activity. This makes a real difference to the performance in Moodle, and you should be able to achieve a 50% increase in system capacity and your CPU will be 50% less busy.
This guide is for installation of APC on a Windows server. The instructions have been tested on a Windows 2003 server, and they should also be applicable if you are using Windows XP as a test server.
Installation on Windows Server
Warning: You cannot install APC and Zend Optimiser on the same server. You should choose one or the other.
Follow these steps to install APC.
Download the correct version
Download the windows binary for the PHP version you have installed from http://downloads.php.net/pierre/. Save into the PHP extensions folder, which is c:\php\ext by default.
Note: If you cannot find a binary for your version of PHP at that location, try the cutting edge binaries built by Gabriel Dragffy at http://www.dragffy.com/posts/php-apc-windows or the ones at http://dev.freshsite.pl/php-accelerators/apc.html
Note: Make sure you choose the right extension for your version (branch) of PHP.
Enable the APC extension in your PHP.INI file
Edit the php.ini file and add the following line in the extensions section:
extension = php_apc.dll
Check that this has worked by restarting your web server and looking at the output of phpinfo from the Moodle administration screens or from a text file containing this line:
<?php phpinfo(); ?>
Check the memory available
To check the amount of memory that is available for APC to use:
- Run WordPad and create a text file called apcmeminfo.php.
- Copy and paste this line into the file
<?php print_r(apc_sma_info()); ?>
- Save the file into moodle/admin/apcmeminfo.php
- Browse to http://mymoodle.com/moodle/admin/apcmeminfo.php.
- Note the avail_mem value (in bytes)
... [avail_mem] => 31447368 ...
The total amount of memory used by the cache is given by this formula:
total_memory = apc.shm_segments * apc.shm_size
In this example, the available memory is approx 32Mb. Usually if we need more than 32Mb in our cache, we should increase the number of segments that APC uses. However, this is can cause web server faults, so it is best to increase the segment size instead.
When setting the total memory available for the cache, start with the lowest that a single Moodle install can work with (64Mb). This amount may not be enough if you have extra modules and blocks installed, so check by monitoring the cache regularly in the first two weeks that your server is running. Look at the monitoring section below and gradually increase your total cache memory until there is no more cache churn.
Customizing APC settings for Moodle
The default APC settings are as follows:
apc.cache_by_default = On apc.enable_cli = Off apc.enabled = On apc.file_update_protection = 2 apc.filters = apc.gc_ttl = 3600 apc.include_once_override = Off apc.max_file_size = 1M apc.num_files_hint = 1000 apc.optimization = Off apc.report_autofilter = Off apc.shm_segments = 1 apc.shm_size = 30 apc.slam_defense = 0 apc.stat = On apc.ttl = 0 apc.user_entries_hint = 100 apc.user_ttl = 0 apc.write_lock = On
For a full explanation of these settings, see http://www.php.net/apc.
For a general Moodle installation several changes are needed. These are:
- The apc.shm_size (the size of the cache) which should be set initially to 64Mb.
- The apc.shm_segments which should be set to 1. This gives a total initial cache size of 64Mb.
- The apc.stat (file stating) which should be set to 1 otherwise you may get blank pages.
- The apc.max_file_size setting which should be increased to 10Mb.
Note that to disable APC temporarily set apc.enabled = 0 in PHP.INI and restart your webserver.
A working APC section of of PHP.INI is shown below, so copy and paste the following at the end of your PHP.INI file:
[APC] apc.enabled = 1 apc.shm_segments = 1 apc.shm_size = 64 apc.max_file_size = 10M apc.stat=1
The remainder of the settings can be left as their default.
Set the temp directory
APC needs a temporary directory to save its files. It will attempt to save the files in the windows temp directory, so set the C:\WINDOWS\TEMP directory to be writable by the web server user (for IIS this is IUSR_machine-name, for Apache no change to permissions are necessary), e.g. at least Read & Execute permissions.
Monitoring and tuning the cache
The APC source contains a php script that is useful for monitoring and tuning the performance of your cache.
- Download the APC monitoring file from http://pecl.php.net/package/apc and extract the contents.
- Look for the file apc.php as this is the file that displays APC monitoring information.
- Save the file apc.php into the /moodle/admin folder on your server.
Note: Once you have successfully installed APC, move this file to a password protected area for security.
- Verify that the script is working by browsing to http://mymoodle.com/moodle/admin/apc.php (or whatever the location of the apc.php file is on your server). You should see a graphical display of your cache with some statistical data on the left side.
To tune the cache, look at the General Cache Information and Detailed Memory Usage and Fragmentation sections. Watch the Cache Full Count and Fragmentation % indicators.
- If Cache Full Count > 0, this indicates that the cache is filling-up and churning as there is not enough memory allocated. To solve this problem increase the memory allocation (apc.shm_size).
- The Fragmentation % indicator should be 0% but may increase from time-to-time especially if the cache is churning.
You can configure the APC monitoring script to use Moodle security. Create a new file apc.conf.php in the admin directory containing the following code. This will be automatically loaded by apc.php
<?php // Moodle user Authentication require_once("../config.php"); require_once($CFG->libdir.'/adminlib.php'); require_login(); require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)); // Disable APC Auth defaults('USE_AUTHENTICATION',0); ?>
To confirm that there has been an improvement in performance, try the following:
- Disable the cache in PHP.INI and restart the web server.
- Run ab.exe (included in the apache bin folder) at the command prompt as follows:
c:\apache2\bin>ab.exe -n 50 -c 1 -d http://mymoodle.com/moodle/index.php
- Note the transfer rate and the time per request values.
- Enable the cache in PHP.INI and restart the web server.
- Load http://mymoodle.com/moodle/index.php in your browser so that it is stored in the cache.
- Run ab.exe again and compare the transfer rate and time per request values. You should see an increase in both of these values by approximately 50%.
To uninstall APC, add a semicolon to the extension line in your PHP.INI as follows:
;extension = php_apc.dll
Remember to restart your web server.