PHP: APC Configuration and Usage Tips and Tricks

PHP LogoPHP APC (Alternative PHP Cache / Opcode Cache) is framework that optimizes PHP intermediate code and caches data and compiled code from the PHP bytecode compiler in shared memory. APC Opcode Cache is quickly becoming the de-facto standard PHP caching mechanism.

PHP APC installation is very easy, example with Fedora / CentOS / Red Hat (RHEL) you can check following guides to see howto install it:
Install Nginx/PHP-FPM on Fedora/CentOS/Red Hat (RHEL)
Install Apache/PHP on Fedora/CentOS/Red Hat (RHEL)

This PHP APC guide is divided on four different section:
1. PHP APC Configuration
2. Enable PHP APC Statistics
3. Howto Use PHP APC User Cache
4. PHP APC Performance Testing

1. PHP APC Configuration

1.1 Find Your apc.ini File

Example location on Fedora/CentOS/RHEL /etc/php.d/apc.ini. You can use apc.ini as base and check following most important / all options.

1.2 PHP APC Most Important Configurations

Enable APC Module

Enable/Disable PHP APC

APC Number of Shared Memory Segments

If APC is configured with mmap it only uses 1 shm_segment, then you could remove apc.shm_segments or use following:

APC Size of Each Shared Memory Segments

If you use just one memory segment, then set this value to total memory what you want to use. This value depends on available RAM size and required cache size. So first this could be example 128 Mb and when you see real usage then it’s easy to increase on decrease this limit.

APC Cache Entries Time to Live (ttl)

Normal configuration it’s good to set some ttl (time to live) value for cache entries. This is number of seconds how long cache entry is allowed to idle in a slot in case this cache entry slot is needed by another entry. This possible to set 0 (zero), but when cache is full then new items can’t be added, so I recommend some other value. PHP APC have two different ttl values one for cache entries apc.ttl (for php files) and apc.user_ttl (for user entries). Also own value for cache garbage-collection apc.gc_ttl. Following is just example setup:

Max File Size on APC Cache

Normally it’s wise cache only “small” files and this value is good to set example to default 1 Mb.

APC Stat (Check) the script if it has been modified

This apc.stat could be powerful and dangerous option. Default value on, which force APC to stat (check) the script on each request to determine if it has been modified. In some cases, if you have files that are rarely modified, or if the project contains “too many” included files (per page load), such as a monstrous e-commerce project, then it’s very good to disable apc.stat. If you disable apc.stat then cache should be cleared every time you modified files.

1.3 Restart Your Web Server

When your configuration is done then just restart your web server and you have APC (Alternative PHP Cache) enabled on all PHP requests.

This PHP APC guide is divided on four different section:
1. PHP APC Configuration
2. Enable PHP APC Statistics
3. Howto Use PHP APC User Cache
4. PHP APC Performance Testing

Share this post

38 Comments

  1. thank you so much, this has been a big help!!!!

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *