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

apc.ttl=3600

apc.stat=0

apc.shm_size=64M

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!';
        else
                print 'Clearing Failed!';

        print_r(apc_cache_info());

} 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.

 

Source

http://css.dzone.com/articles/using-apc-correctly

http://ckon.wordpress.com/2012/01/02/speedup-php-opcode-cache-apc-xcache-eaccelerator/

http://linuxaria.com/howto/everything-you-need-to-know-about-apc-alternate-php-cache?lang=en

http://rtcamp.com/wordpress-nginx/tutorials/php/apc-cache-with-web-interface/

http://www.slideshare.net/benramsey/caching-with-memcached-and-apc

Follow me

Leo G

Is a linux enthusiast and hobbyist, he has over 8 years experience in IT management, support and operations. He works with opensource software and wants to see more enterprises move towards the same
Follow me

Similar Posts
Installing and Configuring Lighttpd PHP-FPM and Mariadb on a 512 MB Linux VPS
What is  a Cache? A cache is a temporary storage area  where data can be stored for rapid access. Why...
How to Install Nginx and PHP-FPM for WordPress via yum
What is  a Cache? A cache is a temporary storage area  where data can be stored for rapid access. Why...
PHP search using PostgreSQL
What is  a Cache? A cache is a temporary storage area  where data can be stored for rapid access. Why...

5 Comments

  • Marko says:

    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.

  • […] You should also read a speed up php with opcode cache […]

  • “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 says:

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

Leave a Reply


Name (required)

Email (required)

Website

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>