Alternative PHP Cache (APC) is an opcode and variable cache for PHP. When you run a PHP script it is first compiled into a series of opcodes which are then used by the Zend engine to run the program before being discarded. APC sits between the source files and the Zend engine and will stop the opcodes generated during the PHP script execution being thrown away. This means that when you run a PHP script a second time the work done in generating the opcodes has already been done and the script will execute faster. In fact, the opcode cache alone can substantially increase the speed of PHP execution and provides an 'easy win' when improving the speed of a PHP website. Having APC for a complex system like Drupal means that you will see a substantial increase in performance, so it is well worth having.
There are several methods to install APC, but the easiest is to use apt-get (or similar package manager).
sudo apt-get install php-apc
You can also install it through PECL.
pecl install apc
Once it has been installed you can double check that the apc.so file is included in the apc.ini file. This file was found at /etc/php5/apache2/conf.d/apc.ini on my localhost setup, but your system might be different. Use the command php --ini to see which ini files have been loaded by PHP and you should see that the apc.ini file has been included. A default APC install will have the following INI file setup.
Once you are sure APC is installed then you'll need to restart Apache (or whatever web server you are using) in order for the APC cache to start being used. After being restarted the APC will be active and will start being used by PHP to store opcodes.
To see what is going on inside APC you can use a script file that is bundled with APC. The file is called apc.php and is usually found in the directory /usr/share/doc/php-apc/, although this depends on your system and how you installed the package. Note that on some systems this file is called apc.php.gz, in which case you'll need to unzip it to apc.php before it can be used. To use this file just copy it into your web root and access it via a web browser. This will give you all sorts of statistics about how the APC is performing, including some graphs if GD is available on the server.
Of primary importance in the APC report is the Cache full count value on the left hand side. This will display the number of times that APC cache has been filled and has needed to be forcibly emptied of items that haven't been used in a certain amount of time. This time constraint is defined with the run time configuration option apc.ttl, which can be set in your apc.ini file. This is an expensive step that is usually run on a item by item basis and should be kept to a minimum on a well configured APC environment. The easiest way to stop the cache getting full is to increase the amount of memory that is available to it. By default APC has a memory segment size of 32M, which can be altered using the apc.shm_size setting in the apc.ini file. I can't tell you what you should set your APC segment size to, you'll just have to set up APC and see how the stats progress. If it looks like the cache is filling up too quickly then set the segment size higher, restart apache and interact with the site to add items to the cache to see if it fills up too quickly. One thing to remember is that there is a significant performance hit from having the APC cache set too low. This is because APC will be continuously checking and invalidating files stored within the cache, perhaps multiple times per page load. What I can tell you is that 32M is almost certainly too low for most Drupal sites so you should immediately increase that to 64M or even 128M.
There are plenty of other configuration options available in APC, and the PHP manual on APC is a good place to find out more about them.
APC And Drupal
There are currently two modules available that interact with APC.
APC Status allows you to view the APC information file from the Drupal administration pages. In order to see the reports you need to copy the apc.php file to the location /sites/all/libraries/APC/apc.php.inc. Once the file is in place and the mode activated you will see a link in your status report page allowing you to view the APC information page. This module is a good way of allowing you to view what APC is doing, without putting your server at risk by having the apc.php file freely available. If you have the apc.php file available on your webroot then you are open to denial of service attacks as a malicious user will repeatedly empty your APC cache, rendering it useless.
APC is a module that allows you to use the APC key/value cache as a way of caching items in Drupal. This means that your cache entries are saved to the APC cache, rather than to a database, which speeds up Drupal significantly. This is similar to using a key/value server like memcached (with the corresponding modules) but uses the APC instead. Once the module is enabled you need to tell Drupal to start using the APC cache. This can be done easily in Drupal 7 by setting some values in your settings.php file.
- $conf['cache_backends'] = array('sites/all/modules/apc/drupal_apc_cache.inc');
- $conf['cache_class_cache'] = 'DrupalAPCCache';
- $conf['cache_class_cache_bootstrap'] = 'DrupalAPCCache';
Those settings worked for me, and you should make sure that everything works on your site if you enable them on your system. Some modules are not suited to using an alternative cache so a little bit of testing is needed. Make sure you test whilst logged in and logged out as it although things seem to work whilst logged in, they might not when logged out.
The APC module README.txt file also states that you can tell Drupal not to start the database when looking for a cache entry. It's a good idea to follow the APC module instructions and test things out each time you add a config setting to make sure that your site still works with the different forms of caching turned on.
- $conf['page_cache_without_database'] = TRUE;
- $conf['page_cache_invoke_hooks'] = FALSE;
The page_cache_without_database setting didn't work on my setup, and it appears that a bug within the Drupal core is causing this to error for anonymous users. There are a couple of issues that are attempting to fix this but I wouldn't turn on this setting for your website until it works. The page_cache_invoke_hooks works nicely though.
Bear in mind that if you are storing values in you APC cache then you need to make sure that you have enough memory for APC to store them. If you have maxed out on the memory available on your system already then using this module is only going to slow down your site.
One other thing to note is that if you use the APC module then you will have trouble clearing your caches using Drush. This is because the APC command line interface (CLI) uses different memory storage than the web server, and so it isn't possible to clear it from Drush.
When I setup the server that #! code runs on one of the first things I did was to install and configure APC. I spent a while tweaking the configuration and keeping an eye on the information page to see that APC was behaving correctly. With just APC turned on I saw a good 20-30% increase in my page load times. With the APC module and APC being used as a key/value cache by Drupal by page load times were reduced by another 10%.