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: