How to install APC in centos for PHP better performance ( opcode)

APC for PHP better performance:

APC is a op-code caching for PHP. Once PHP code is run, APC caches the complied PHP code so for next time, time for compiling PHP will be reduce and gives faster performance.  APC caches files in memory so  performance of PHP code improves significantly.

Installing APC:

Make sure PHP is installed first 🙂 . we need to install few pre-requist for PHP.

$ yum install php-pear php-devel httpd-devel pcre-devel gcc make

$ pecl install apc


Configuring APC:

Open APC config file and make sure you have atlest following configuration:

vi /etc/php.d/apc.ini

Enable APC for php.

 extension=apc.so
 apc.enabled=1

The number of seconds a cache entry is allowed to idle in a slot before APC dumps the cache.

 apc.ttl=72000
 apc.user_ttl=72000
 apc.gc_ttl=3600

Size of memory for apc ( 1024 M)

apc.shm_size=1024M

Enable apc stats.

apc.stat=1

Enable APC for command line php operations.

apc.enable_cli=1

Allow 2 seconds after a file is created before it is cached. This will prevent premature PHP pages to get cached.

apc.file_update_protection=2

Maximum size of single file that apc can store.

apc.max_file_size=1M

Maximum number of files APC can store ( rotation).

apc.num_files_hint=200000

Maximum number of users data entries that APC can store.

apc.user_entries_hint=20000

 

You can put your configuration in php.ini file but i prefer to have separate file like above for configuration. Values mentioned above are for demonstration purpose, different values  for APC can be set  which depends on number of PHP pages, size of memory in server, number of page hits e.t.c

Restart httpd server:

service httpd restart

 

APC installation comes with apc.php file, which can be use to monitor APC performance. This file can be found inside APC package  OR can be download from http://pecl.php.net/package/APC ( unzip and look for apc.php file).

Once apc.php is downloaded copy it to your domain so that you can assess it.

Now from browser, you can go to  http://domain.com/apc.php. I prefer to wait for a day to see the APC performance so we can have clear idea how well our configuration did.

 

Sample output of APC:

apc1
Here, we clearly see that APC is not performing very well as we have 44% misses. To optimize this we can reduce the memory and also we can increase max_file_size and also TTL time so that more can be cached in APC.  Normally when missed rate is more than 10% ,  its better to reconfigure the settings of APC.

Well tuned APC will look like below: 

APC INFO  luke.savvysme.com.au   128.199.188.56-s

 

17 thoughts on “How to install APC in centos for PHP better performance ( opcode)

  1. Muhaimin December 3, 2014 / 8:08 am

    Hi

    I’m new with centOs. Is this applicable with Centos 7? I already install the Apache 2.4, PHP 5.5 and MariaDB. But now how do I get APC enable for php installed on my machine?

      • Muhaimin December 4, 2014 / 2:26 am

        Sorry Ananda, My PHP is 5.4. Is it necessary for me to upgrade it to 5.5?

      • Ananda Raj Pandey December 4, 2014 / 2:31 am

        No need to upgrade just follow above process to add apc and you will be all fine

      • Muhaimin December 4, 2014 / 2:39 am

        Thanks Ananda. I still have unconfigured php-fpm and beside I’m using MPM Prefork. Now my problem is the php apps running on the server keep time out. I am not sure whether to follow steps above or not. Or will it solve my problem. Need clarification

      • Ananda Raj Pandey December 4, 2014 / 2:52 am

        Time out is different issue, so you should fix it first. As its a configuration that will not hamper APC settings. And also many people do not recommend APC with 5.4 as there are some bugs. You can read this for more clear idea: stackoverflow.com/questions/9611676/is-apc-compatible-with-php-5-4-or-php-5-5

      • Muhaimin December 4, 2014 / 3:02 am

        sorry to ask this. Do you have any advice where to look for this timeout issue? Thanks

      • Ananda Raj Pandey December 4, 2014 / 3:14 am

        Is is script timeout, session time out ?? can you give me error you are getting so that i can have a look.

      • Muhaimin December 4, 2014 / 3:19 am

        it’s just js file timeout. The app load (too many) js file async. And I keep getting timeout on just loading these files. Sometime the css file also hit

      • Muhaimin December 4, 2014 / 3:20 am

        FYI, I’m running Apache 2.4

      • Muhaimin December 4, 2014 / 3:54 am

        The js files not processing any php script. It’s just javascript. The app use lazy loading technique to load the js files. I have increase the execution time before but it’s not working

    • alanvd December 4, 2014 / 1:20 am

      Hi Muhaimin,
      Centos 7 does not install php5.5 by default it is 5.4. I just added to a big set of centos 7 servers APC works as described above perfectly just do exactly the same. As for moving to php5.5. do not yet do that unless you test on a server if all your php stuff and everything else still will work. 5.5 seems to break left and right stuff. (as expected)
      regards

      • Muhaimin December 4, 2014 / 2:25 am

        yes you are right. Mine is php 5.4. Is it possible for me to follow steps above. Beside I have the php-fpm enabled but not sure how to configure it yet. Because I still have problem to change the MPM prefork to event. Dont know how to get it working on Centos 7

      • Ananda Raj Pandey December 4, 2014 / 2:32 am

        No need to upgrade just follow above process to add apc and you will be all fine

  2. Menomime August 4, 2015 / 6:14 am

    Could you provide the specs of your machine that this was installed on?

    Most sites just provide a way to install APC, but you take it further by setting up some tuning on the configuration file. I think it’s just apt to include the specs of the machine so we could see if it is safe to apply it to our machines and/or scale it accordingly.

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s