APC (Alternative php cache) is opcode and object cache for php. It caches the php bytecode on first execution of a php script which increases the performance of php code on subsequent executions of same script. For the purpose of this article I’ll use it only for opcode cache. No data cache will be used.
These are the steps to setup apc on Ubuntu Linux for Apache:
- First install the php-apc package using the following command:
sudo apt-get install php-apc
- The package also installs apc.php.gz at
/usr/share/doc/php-apc/apc.php.gz. Unzip and copy it to somewhere in your apache webserver document root. Here is a the command you can use:
zcat /usr/share/doc/php-apc/apc.php.gz > /path/to/document/root/apc.php
This script (apc.php) is pretty useful to show various apc stats. Edit the file and set appropriate user and password for web simple auth access control.
- Visit http://yoursite.com/apc.php to view apc stats and current configuration settings. Here are some screenshots from it showing general cache information and host status.
The full list of apc configuration options at can be seen at php.net. Here are some options which you may want to look at for quick start:
- apc.stat: Default value is 1. This means that apc will check the php file modification time each time to see if file needs to be reloaded. Setting it to 0 may have slightly better performance but it will drawback of executing old php even if a php file is modified unless apache is restarted.
- apc.shm_size. Size of the shared memory segment. You may want to increase it if you see cache is close to full in cache stats.
- apc.max_file_size. Default value is 1M. You can decrease it if you don’t have too much memory and want to cache only smaller files.
I think in most cases the default configuration values should be good enough. You can run apc for sometime with default values and see if something needs to be changed.
APC – benchmarks
Here are the details of the system used:
- WordPress 3.5.1 on Ubuntu Linux server 12.04.1 LTS on AWS EC2 micro instance
- php apc 3.1.7-1
- PHP Version 5.3.10
- Command used to measure performance
ab -n 10 -c 1 http://infoheap.com/
The output is recorded in 2nd run of the command.
Here is the outcome of 3 experiments I did:
- Experiment 1: No apc installed
Time per request: 26.302 [ms] (mean)
- Experiment 2: apc with default apc configuration
Time per request: 16.705 [ms] (mean)
- Experiment 3: apc with apc.stat:0
Time per request: 16.662 [ms] (mean)
There is approx 36% improvement when php apc is installed vs when it is not installed. Switching off apc.stat did not really result in any significant benefit. So its better to keep apc.stat to its default value.