How to speed up PHP with Opcode cache like APC

How to speed up PHP with Opcode cache like APC

What is  a Cache?

A cache is a temporary storage area  where data can be stored for rapid access.

Why Cache PHP?

Compiling PHP every time a request comes in is a very resource intensive process and it is better to cache code rather than compile it every time. This not only reduces your  ram memory usage but also increases the speed of loading your php pages.

What are the Types of Cache available for PHP?

There are many but some of the prominent one’s are Memcache, APC ( alternative PHP cache) , Zend cache.

Why APC?

Well cause it’s an opcode cache that means it caches the compiled code, It is going to be included in PHP 6 and it is easy to configure.

How can I install?

Before you do add the below code to your php script to measure page load time

<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds

You can add it to footer.php if you are using wordpress

You have to have PHP up and running if not then refer to my tutorial here and then install via yum package manager

$ yum install php-apc

How to configure?

There are two primary decisions to be made configuring APC. First, how much memory is going to be allocated to APC; and second, whether APC will check if a file has been modified on every request. The two ini directives that control these settings are apc.shm_size and apc.stat. You should also set apc.ttl low as that is time to live for the cache entries. The lower the better as when you cache is full the older entries will be flushed to make space for new, i f you have a higher ttl or a 0 ttl and you cache is full then the entire cache is flushed.

Open up the apc.ini config file and make the following changes

vim /etc/php/apc.ini




cp /usr/share/doc/php-pecl-apc-3.1.15/apc.php  /var/www/html/

How can I clear the cache?

Add a file called apc_clear.php in your web root directory with the below code

 if (function_exists('apc_clear_cache') && $_GET['pass'] == 'secret') {
        if (apc_clear_cache() && apc_clear_cache('user'))
                print 'All Clear!';
                print 'Clearing Failed!';


} else {
        print 'Authenticate, please!';

What is the meaning of hits and misses?
PHP cache APC
Hits means the request was served from the cache and misses means it was not. You need to increase your cache memory incase if it get’s  full often.



Follow me

Leo G

Is a Linux Hobbyist and Enthusiast. He Strongly believes in OpenSource Software and would like you to view and download his software at
Follow me

  • http://guter-webhoster Marko

    Thanks a lot Leo, what I did to speed up WP with APC was edititing my functions.php for example at the point where I fetch my amount of facebook likes.

    I edited it in a way that it uses the object cache of wordpress.

    There also exists an object apc chache dropin file for wordpress. Just google for apc object cache wordpress.

  • Pingback: How to Install Nginx and PHP-FPM for Wordpress via yum »()

  • Phil Sturgeon

    “Well cause it’s an opcode cache that means it caches the compiled code, It is going to be included in PHP 6 and it is easy to configure.”

    There are several things wrong with this statement .

    APC has essentially been deprecated at this point. PHP 5.5 saw Zend Optimizer rolled into the core of PHP, so APC became somewhat irrelevant.

    PHP 6 is not going to exist, and will be called PHP 7.

    Seeing as Zend Optimizer is installed for free in PHP 5.5, the only advice you need to use an opcache is to upgrade, and make sure its enabled in your PHP ini. :)

  • Frederic

    Why talking about “APC” which is no longer supported ?
    APC is dead, better thing about OPCache or XCache, no ?

    • Leo G

      ok wrote it sometime back