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.
- If you’re not already, login to your server with SSH.
- 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/
- 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
- Now visit https://example.com/apc.php to access the dashboard.
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.
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: