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


  1. The best thing with learning PHP programming is having a lot of people who are always willing to share their knowledge. One of the best tips on APC configuration is this one that you have made.

  2. I disagree with this:
    “Normally it’s wise cache only “small” files and this value is good to set example to default 1 Mb.”
    Why did you say that? Caching large files is also good because you are avoiding parsing these files.
    Anyway thanks for the tips :)

    • Hi Luis,

      Some cases it might be good increase apc.max_file_size value. But…

      Let’s think very normal example, VPS servers (512Mb, 1Gb, 2Gb) where you have to limit amount of APC memory (e.g. 32Mb, 64Mb 128Mb, 256Mb), and you have hundreds of files, a lot of small files (less than 1Mb) and a few large files (like 25Mb).

      Okay, you cache all files ignoring the size (set very high apc.max_file_size value), then the cache will reach maximum capacity almost constantly and it clean forcefully any entries that haven’t been accessed in the last apc.ttl seconds. This is situation where you use APC, but finally your scripts are loading much slower than without any cache, because you end up the situation where load files from disk, add files to cache and delete files from cache. Which is much heavier than just load files from disk.

      The optimal situation is a situation where your cache is almost fully used, but newer reach maximum capacity (Cache full count number is always zero) and almost all incoming requests are hits (not misses).

      But, if you have enough memory to get all your files cached, then it’s okay to set very high apc.max_file_size and cache everything. :)

  3. I installed APC on a Debian server using apt-get.

    I can’t find the apc.php file in the location you mentioned where else is this file located ?

    • Hi A.Jesin,

      On Debian check /usr/share/doc/php-apc/ path or try following (as root):

  4. Hello JR,

    I tried everything and still I couldn’t find apc.php. I even tried

    So finally I downloaded the APC archive from PECL’s website and used the apc.php from it.

    I have uploaded a copy of it to my blog at for people who have installed it from the repository.

    • Hi again A.Jesin,

      Strange that Debian doesn’t include apc.php at all, but nice to hear that you got it working and even write helpful post to other Debian users who can’t get apc.php file from Debian’s on pakcages. Thanks!

      • It’s in /etc/php5/conf.d/20-apc.ini

        • Thanks Henrik!

        • TY for Share

  5. Hi. I am coming from windows and am on fedora 18. What do I use for domain-name here? domainname command states there is no domain name set. i.e. none

    • Hi Chandan Pednekar,

      Do you mean APC statistics url?

  6. Just came across your useful article while trying to resolve an APC problem.

    It seems a little odd and I’m wondering if you or anybody here has come across this. I’ve got APC Opcode cache installed fine on a VPS and I can check its working with apc.php. However if I use a local php.ini file for a vhost APC stops running altogether no matter the contents of the php.ini file (even if blank).

    Any idea what would be causing this and how to resolve it ?

    • Hi Nic,

      Based on this information, my guess is that you are not enabled php apc at all when you are using local php.ini. What if you check your phpinfo is APC module loaded at all, when you use local php.ini? Or what if you use your global php.ini as your local config, is it working then?

      So I guess that you are missing following, when you use your local php.ini:

      • Yes JR, as I’m running a VPS I have a core php.ini and a php.ini for each vhost and I’d assumed that the contents (of a local php.ini) would just override individual parts of the core php.ini. Turns out that the existence of a local php.ini overrides all server php.ini settings and I needed to reinclude the apc module. Thanks for taking the time to answer this and everybody elses questions.

        • Hi Nic,

          Create a phpinfo() file in your document root and have a look at it.

          The first section of the file will give you information on the location of the php.ini file that is being used.

          Take a look at the sections “Loaded Configuration File” and “Scan this dir for additional .ini files”

        • You are welcome! :) Yes, it’s true that local php.ini overrides all server php.ini settings. So do you get it fully working as you wished?

          • Yes, I just pulled over the core ini and modified specific settings and all is well :-)

            • Excellent! :)

  7. Hi,

    Thanks for the tutorial, apc installed without any trouble.

    But 2 issues –

    1. Upon visiting apc stats, only one file has been cached which is apc.php itself, total free memory 30m and ignore rest of the php files. Any reason?

    2. Couldn’t find apc.ini, tried many times to locate but no such php5 or php.d directory under /etc/

    I’m running CentOS 6


    • Hi Harry,

      First run (as root):

      Could you then post output of following commands:

      • Many thanks for quick response: Following are the results:

        1. locate apc


        2. php -i |grep apc

        apc.cache_by_default => On => On
        apc.canonicalize => On => On
        apc.coredump_unmap => Off => Off
        apc.enable_cli => Off => Off
        apc.enabled => On => On
        apc.file_md5 => Off => Off
        apc.file_update_protection => 2 => 2
        apc.filters => no value => no value
        apc.gc_ttl => 3600 => 3600
        apc.include_once_override => Off => Off
        apc.lazy_classes => Off => Off
        apc.lazy_functions => Off => Off
        apc.max_file_size => 1M => 1M
        apc.mmap_file_mask => no value => no value
        apc.num_files_hint => 1000 => 1000
        apc.preload_path => no value => no value
        apc.report_autofilter => Off => Off
        apc.rfc1867 => Off => Off
        apc.rfc1867_freq => 0 => 0
        apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS
        apc.rfc1867_prefix => upload_ => upload_
        apc.rfc1867_ttl => 3600 => 3600
        apc.serializer => default => default
        apc.shm_segments => 1 => 1
        apc.shm_size => 32M => 32M
        apc.slam_defense => On => On
        apc.stat => On => On
        apc.stat_ctime => Off => Off
        apc.ttl => 0 => 0
        apc.use_request_time => On => On
        apc.user_entries_hint => 4096 => 4096
        apc.user_ttl => 0 => 0
        apc.write_lock => On => On

        3. rpm -qa \*apc\* —– nothing appeared / no result

        4. uname -a

        Linux 2.6.32-358.11.1.el6.x86_64 #1 SMP Wed Jun 12 03:34:52 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux


        • Thanks for this info, how and where is your php installed?

          You could create apc.ini or alternatively add your configuration to php.ini. Your php apc module is currently enabled with default config.

          • Thanks for the response.

            PHP was pre-installed on the server. I guess it’s under /usr/local/lib/php (not sure, but seems to be).

            Yes, php apc module is enabled, ain’t suppose to cache php files even with default config ?

            Can I create apc.ini at the same location as ? if not where should I create and how do I relate it to php.ini.

            • Create a phpinfo file in your document root and access it via web browser.
              Search for the option which says “Scan this dir for additional .ini files” this contains a directory location inside which config files of various PHP modules exist.

              • No ini files.

                Scan this dir for additional .ini files – (none)

                Additional .ini files parsed – (none)

    • Hi Harry,

      I’m using apc on Debian and the apc.ini file is named as 20-apc.ini which is located inside

      so try using

      to locate the file.

      • Hi Jesin,

        Thanks for the response.

        1. I did checked many times under /etc/. Unfortunately, no such directory php or php5 found.

        2. Tried – find /etc -name *apc.ini

        – no result appeared


  8. Thanks for the great tutorial.. I have installed successfully in my server :)

  9. Dear A.Jesin ,
    use SSH find command is too usefu1
    for example :

    900d 1u(k

  10. Hey JR please guide on Centos.

    Here is My Centos Config of APCU.ini

    • Hi tom,

      Are looking user cache, opcode cache or both?

      With PHP 5.5, APCu is just user cache and PHP 5.5 have own module OPCache for opcode caching.

      I’m not upgrading this guide, because there is a lot of users using still PHP 5.4/5.3/5.2 with APC. but I have planned to write totally new guide to PHP 5.5 users.

  11. Thanks. I’m looking forward to it.

    • Hi clefor jhon operiano,

      Looks like it’s working, you can of course enable apc.php and check it too.

  12. i have error PHP: syntax error, unexpected TC_CONSTANT in /etc/php5/cgi/conf.d/20-apc.ini on line 5

    • Hello,

      Could you post content of 20-apc.ini file? Or at least line 5?

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



  1. Install Apache/PHP 5.4.6 on Fedora 17/16, CentOS/RHEL 6.3/5.8 - [...] More info about PHP APC from PHP APC Configuration and Usage Tips and Tricks. [...]
  2. Install Nginx/PHP-FPM on Fedora 17/16, CentOS/RHEL 6.3/5.8 - [...] More info about PHP APC from PHP APC Configuration and Usage Tips and Tricks [...]

Submit a Comment

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