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:
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:
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?
You dont need to install APC for php5.5 as php 5.5 has opcache added. You need compile php with opcache. You can find instruction here:
http://php.net/manual/en/opcache.installation.php
Sorry Ananda, My PHP is 5.4. Is it necessary for me to upgrade it to 5.5?
No need to upgrade just follow above process to add apc and you will be all fine
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
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
sorry to ask this. Do you have any advice where to look for this timeout issue? Thanks
Is is script timeout, session time out ?? can you give me error you are getting so that i can have a look.
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
FYI, I’m running Apache 2.4
its hard to answer just with little information, but you can try few things:
http://stackoverflow.com/questions/9629566/how-to-increase-apache-timeout-directive-in-htaccess
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
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
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
No need to upgrade just follow above process to add apc and you will be all fine
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!