PHP apc – setup and performance benchmarks on Ubuntu Linux

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.

APC installation

These are the steps to setup apc on Ubuntu Linux for Apache:

  1. First install the php-apc package using the following command:
    sudo apt-get install php-apc
  2. 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.

  3. 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.
    apc-general-cache-infoapc-host-stats

APC configuration

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:

  1. 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.
  2. 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.
  3. 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:

  1. WordPress 3.5.1 on Ubuntu Linux server 12.04.1 LTS on AWS EC2 micro instance
  2. php apc 3.1.7-1
  3. PHP Version 5.3.10
  4. 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:

  1. Experiment 1: No apc installed
    Time per request:       26.302 [ms] (mean)
  2. Experiment 2: apc with default apc configuration
    Time per request:       16.705 [ms] (mean)
  3. Experiment 3: apc with apc.stat:0
    Time per request:       16.662 [ms] (mean)

 Conclusion

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.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments