APC Cache Optimization & Monitoring Using Web Interface

While we installed PHP with other things, we also installed a php-apc package.

APC is one of the most popular caching mechanism for PHP’s op-code caching. Once activated, it starts caching PHP codes automatically. It also works nicely with W3 Total Cache plugin for storing Object & MySQL caches.

APC – Web-based User Interface

Its always better to check if APC is working correctly. Thankfully, APC comes with file apc.php, which provides a simple web-based interface. Just copy it to your site’s web-root folder:

cp /usr/share/doc/php-apc/apc.php /var/www/example.com/htdocs

Then open http://example.com/apc.php in browser.

In case you want to use admin-side of apc.php, you will need to change default password to something else.

Just open apc.php file and edit following line:

defaults('ADMIN_PASSWORD','password');          // Admin Password - CHANGE THIS TO ENABLE!!!

You can change a few more defaults to customize web-based UI as per your needs.

APC Cache Configuration

You can tweak APC’s parameter by editing vim /etc/php5/conf.d/20-apc.ini (if you have installed PHP this way). In other cases, you can simply add following values to your php.ini

If apc.php is showing miss-rate above 10%, the chances are high that you need to change value of some APC configuration option.

There are many APC configuration options, but I recommend making changes to the following:

Total RAM storage allocated for APC (default is 32 MB)

apc.shm_size=1024M

Maximum size of a single file APC can store (default is 1MB)

apc.max_file_size=10M

Number of op-code “files” APC can store (default is 1000)

apc.num_files_hint=20000

Number of data entries APC can store (default is 4096)

apc.user_entries_hint=20000

Once you are done with tweaking, you need to restart PHP. Command: service php5-fpm restart

APC & WordPress + WP3 Total Cache

If you are using W3 Total Cache plugin, then you must make use of APC for Database Cache and Object Cache. 

Check following screenshot:

Note: W3 Total cache will offer APC storage option for Page Cache and Minify Cache also. Do NOT use it. “Disk enhanced” is better for Page Cache & Minify Cache.

There are many caching engines available for PHP but I like APC for its simplicity. Also APC will be official caching mechanism for PHP 6 (someday…)

More:

18 responses to “APC Cache Optimization & Monitoring Using Web Interface”

  1. Hey there,

    Up to now, the following is wrong:

    apc.shm_size=1024

    The apc.shm_size parameter has to be extended with a size unit, e.g. M or G:

    apc.shm_size=1024M

    or

    apc.shm_size=1G

    Thanks!

    • @Pascale

      Thanks for pointing it out. I will update it now. 🙂

      What I used was old-syntax. Its not wrong. It still works but generates warning.

  2. In your final note you mention not to use Opcode APC for Page cache. But when I use “Page cache: Disk Enhanced”, my SSL sites break, and the style sheets aren’t loaded. APC option seems to be working, can you elaborate on why I shouldn’t be using it, or how I might solve the SSL problem with Disk Enhanced? Thanks for the excellent tutorials.

  3. i’m sorry, that was browser cache that i was having problems with, and it seems o be resolved. you can delete my comments here. thanks.

  4. Are you sure about values for apc.shm_size and espacially apc.max_file_size. Most webservers for small blogs don’t have so much RAM, that they can reserve 1G only for APC. Like my V-Server only has 1G running a multisite installation. I have only 96M fpr APC and even the caching plugin (Cachify) stores it’s values into APC, my cache is only 50% filled. Even though giving APC lots of RAM lowers the RAM usage of the Apache processes, I would save enough RAM for other processes like Apache and MySQL-

    I also wouldn’t recommend to set the max_file_size to such a high value (unless you really have 1G for APC). The largest file in the WordPress core ist only like 180K, so even the default value of 1M is more than enough.

  5. Hi Guys,

    I have centos server and php 5.4.22. I am facing some problem with the file cache info. while User Cache Information is working perfectly. as soon as I installed the apc and updated the settings, I got 100% green bar and 0% misses. Is this correct? below is the setting that I have used so far. Please help me to correct it so that file caches also start working.

    apc.enabled=1
    apc.shm_segments=1
    apc.optimization=0
    apc.canonicalize=1
    apc.serializer=DEFAULT
    apc.shm_size=64M
    apc.shm_strings_buffer=3M
    apc.ttl=0
    apc.user_ttl=7200
    apc.num_files_hint=1024
    apc.mmap_file_mask
    apc.mmap_file_hint=1024
    apc.mmap_file_mask=/tmp/XXXXXX
    apc.enable_cli=0
    apc.max_file_size=3M
    apc.cache_by_default=0
    apc.rfc1867_name=APC_UPLOAD_PROGRESS
    apc.rfc1867_freq=0
    apc.rfc1867_ttl=3600
    apc.lazy_classes=0
    apc.lazy_functions=0

  6. I am no longer using APC. We switched to zend-opcache starting from PHP 5.5 – http://rtcamp.com/tutorials/php/zend-opcache/

    However, it’s possible to have 100% hits if nothing is getting modified on your server. Like a single-user blog using 3rd party commenting system.

    You can inspect what is cached in APC using web-viewer. There are two big buttons with text “System Cache Entries” and “User Cache Entries” for file-cache and user-cache. respectively.

    You can also comment out your custom APC config and switch back to default APC config. It might make a difference as well.

  7. I am new to using APC. I have set apc.shm_size as 64M. I’m confused what could be the right value for apc.ttl value for a websies which gets hardly 200 visitors per day?

    • I think you can leave it default. Size and TTL are not dependent on each other. Size controls how “much” you can cache and TTL controls how “long” you can cache!

  8. Hi,
    where to download the apc.php web interface? The php5-apc package of Debian does not have anything but the changelog in /usr/share/doc/php5-apc/.