APC (Alternative PHP Cache) is a PHP accelerator, very useful for MediaWiki: one of the best immediate performance improvements that can be made is to speed up execution of the application itself.
The original APC provides PHP bytecode caching and object caching services to PHP applications.
From PHP 5.5.0 on, PHP includes OPcache for bytecode caching. OPcache does not provide object caching services for PHP applications though; APCu can provide these missing services to applications. HHVM has APC object caching built-in.
MediaWiki is capable of scaling to meet the needs of large wiki farms such as those of the Wikimedia Foundation, Wikitravel and Wikia and can take advantage of a wide number of methods including multiple load-balanced database servers, memcached object caching, front-end Squid caches (see Manual:Squid caching) or Varnish caches (see Manual:Varnish caching) and multiple application servers. For most smaller installations, this is overkill though, and simply optimizing PHP performance is sufficient for good results.
As of PHP 5.5, part of APC was merged into PHP itself, and the rest moved into a module named APCu (an object caching API for PHP applications).
APC is a bytecode cache, that is, it stores the compiled bytecode from the PHP interpreter for future runs of the same script file. A large application such as MediaWiki, which has a substantial initialization time, benefits from using such an extension rather noticeably.
The exact preferred method for installing APC depends on your platform; it can be installed from PECL in the standard fashion, or compiled and installed manually. It's not bundled in PHP 5.5. Many Linux distributions offer packages for APC which are typically installed in the same fashion as PHP itself.
Once APC is installed and running, which can be verified in the output of the
php -r 'phpinfo();' | grep apc command, MediaWiki needs no special configuration to benefit from the bytecode caching capabilities it offers. However, APC also offers a "user cache", which allows application developers to store object data in the in-memory cache, rather like a "mini memcached", and MediaWiki can take advantage of this to use as a parser, message and general-purpose cache.
To configure MediaWiki to use the APC user cache, edit LocalSettings.php and add the following line:
$wgMainCacheType = CACHE_ACCEL;
Some parts of MediaWiki will use the APC user cache if it is available regardless of whether it is configured as the main cache.
A script, apc.php is bundled with the APC package which can be used to inspect the status of the cache, and also examine the contents of the user cache to verify that MediaWiki is correctly using it.
- The APC user cache is not suitable as the main cache for use in environments where multiple application servers are used, as the cache is per-webserver. However, it is recommended that you still install it, as some parts of MediaWiki that can be cached per-webserver will take advantage of it.
- All PHP applications on the server are able to access the same user cache, thus, if multiple users are using the server to run web applications, they are able to read the values of the user cache, which could lead to information being taken from the cache. This might include serialized user objects, for example, containing email addresses, password hashes and edit tokens.
- Command line scripts and the web server will use different caches. This means that if a maintenance script invalidates some cache, it may not be reflected on the web interface.
MediaWiki will still benefit from APC's bytecode caching features if you decide not to configure it to use the user cache, however, so it is still well worth installing.