View and clear PHP APC cache

Of note, APC has been discontinued. Instead, check out the NGINX Cache Manager for your PHP caching needs. 

After you install APC for PHP to speed up PHP, you’ll want to be able to take a look at the APC dashboard, and to clear the APC cache at times.

In this guide I’ll cover how you can copy over the apc.php script included with APC to your website so you can see what the cache looks like. I’ll also show you how you can either manually clear the cache, or setup a cronjob to automatically clear the APC cache on a consistent basis.

Setup APC dashboard to view cache stats

If you followed along and have already installed APC, you should still have the apc.php file on your server. Using the steps below I’ll show you how to copy over this file to your website so you can view the APC cache.

  1. If you’re not already, login to your server with SSH.
  2. In this example my cPanel username is userna5. Use this command to copy the apc.php file from your APC source files to your document root:

    cp -frp /usr/local/src/APC-3.1.13/apc.php ~userna5/public_html/

  3. Now you’ll want to change the ownership of the apc.php script to your cPanel user with this code:

    chown userna5.userna5 ~userna5/public_html/apc.php

  4. Now visit https://example.com/apc.php to access the dashboard. apc dashboard showing cached files and hits
     

    You can see the number of Cached files and also how many Hits they’ve had, that is to say how many times APC saved your server from having to load up everything for a PHP script and instead grabbed some content from the APC cache.

Clear APC Cache

To clear the APC cache it’s quite simple, just make a script called something like apc_clear.php and then add the following code to it. Make sure to replace 123.123.123.123 with your own local IP address:

<?php if (in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1', '123.123.123.123'))) {   apc_clear_cache();   apc_clear_cache('user');   apc_clear_cache('opcode');   echo json_encode(array('success' => true)); } else {   die('No valid IP'); } ?>

When you access this script directly at https://example.com/apc_clear.php from your approved IP address, or if you setup a cronjob which will connect with the server’s local IP, it will clear out the APC cache for you.

This is an example cronjob setup for clearing the APC cache every 6 hours:

0 */6 * * * /usr/bin/curl –user-agent Cron https://example.com/apc_clear.php

You should now understand how to review how effective your APC cache is working for your site, and also how to clear out that cache in the event that pages aren’t showing updates because they are still cached.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

2 thoughts on “View and clear PHP APC cache

  1. I’m using file: $VERSION=’$Id: apc.php 325483 2012-05-01 00:34:04Z rasmus $’;

    If you open the apc.php file in a text editor:

    • look in the top of the document for the section just below ‘BEGIN OF DEFAULT CONFIG AREA’. 
    • Look for the snippet declaring default values for ADMIN_USERNAME & ADMIN_PASSWORD. 
    • When you change these variables, you set the password on the apc.php file. 
  2. Save the file, upload it and navigate to the page with a browser.
  3. Click the ‘Login’ button on the upper right of the page and enter the credentials you just changed.
  4. When you successfully log in, a button will appear below the login button location in the upper right that says ‘clear opcode cache’
  5. Click the button and the cache will clear.

Was this article helpful? Join the conversation!