How to install APC on CentOS and DirectAdmin

cd /usr/local/src
mount -o remount,exec,suid /tmp
yum install autoconf 
yum install php-pear 
yum install php-devel 
yum install httpd-devel 
yum install pcre-devel 
wget http://pecl.php.net/get/APC
tar xvfz APC-VERSION.tgz
cd APC-VERSION
phpize
whereis php-config 

This will give you the location of php-config, which you will put after --with-php-config=

./configure --with-php-config=/usr/local/bin/php-config
# or you can try some experimental features; this worked well with PHP 5.4.6. Without, magento sites would stop responding after a while.
# ./configure --with-php-config=/usr/local/bin/php-config --enable-apc-memprotect --enable-apc-pthreadrwlocks --enable-apc-pthreadmutex
# use 'make clean' if trying a different ./configure
make
make install

Get the shared location of the apc installation files
e.g. Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20090626/

locate php.ini

For CentOS and directadmin, it's usually located in: /usr/local/lib/php.ini

vi /usr/local/lib/php.ini

Copy/paste the following code around the 'extension =' section.

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20090626/"
extension = apc.so
 
; This can be set to 0 to disable APC.
apc.enabled=1
; The number of shared memory segments to allocate for the compiler cache.
apc.shm_segments=1
; The size of each shared memory segment, with M/G suffixe use 128M if you have under 4GB memory.
apc.shm_size=256M
; A "hint" about the number of distinct source files that will be included or
; requested on your web server. Set to zero or omit if you're not sure;
apc.num_files_hint=1024
; Just like num_files_hint, a "hint" about the number of distinct user cache
; variables to store.  Set to zero or omit if you're not sure;
; apc.user_entries_hint=4096
; The number of seconds a cache entry is allowed to idle in a slot in case this
; cache entry slot is needed by another entry.
apc.ttl=60
; use the SAPI request start time for TTL
apc.use_request_time=1
; The number of seconds a user cache entry is allowed to idle in a slot in case
; this cache entry slot is needed by another entry.
apc.user_ttl=60
; The number of seconds that a cache entry may remain on the garbage-collection list.
apc.gc_ttl=30
; On by default, but can be set to off and used in conjunction with positive
; apc.filters so that files are only cached if matched by a positive filter.
apc.cache_by_default=1
; A comma-separated list of POSIX extended regular expressions.
apc.filters
; The mktemp-style file_mask to pass to the mmap module
apc.mmap_file_mask=/tmp/apc.XXXXXX
; This file_update_protection setting puts a delay on caching brand new files.
apc.file_update_protection=2
; Setting this enables APC for the CLI version of PHP (Mostly for testing and debugging).
apc.enable_cli=0
; Prevents large files from being cached
apc.max_file_size=1M
; Whether to stat the main script file and the fullpath includes.
apc.stat=1
; Vertification with ctime will avoid problems caused by programs such as svn or rsync by making
; sure inodes havn't changed since the last stat. APC will normally only check mtime.
apc.stat_ctime=0
; Whether to canonicalize paths in stat=0 mode or fall back to stat behaviour
apc.canonicalize=0
; With write_lock enabled, only one process at a time will try to compile an
; uncached script while the other processes will run uncached
apc.write_lock=1
; Logs any scripts that were automatically excluded from being cached due to early/late binding issues.
apc.report_autofilter=0

This is what I'm currently using:

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20090626/"
extension = apc.so
apc.enabled= 1
apc.shm_size=2G
apc.max_file_size=1M
apc.ttl=3600
apc.gc_ttl=3600
apc.user_ttl=7200
apc.stat=0 ; you'll have to restart apache if you make any php file changes; otherwise set this to '1', though you'll take a performance hit if you're running something with a bunch of configuration files--i.e. Magento.
apc.include_once_override=0 ;leave this to 0 to prevent drupal errors

The following were my previous settings:

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20090626/"
extension = apc.so
apc.enabled= 1
apc.shm_size=512M
apc.max_file_size=5M
;apc.ttl=3600
;apc.user_ttl=3600
apc.num_files_hint=10000
apc.user_entries_hint=10000
;apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.stat= 0
apc.optimization=0
apc.shm_segments=1
apc.stat_ctime = 0
;apc.file_update_protection = 2
apc.enable_cli=1
apc.cache_by_default=1
apc.include_once_override=0 ;leave this to 0 to prevent drupal errors
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525/"
extension = apc.so
apc.enabled=1
apc.shm_size=512M
apc.max_file_size=5M
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=10000
apc.user_entries_hint=10000
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.stat= 0
apc.stat_ctime = 0
apc.file_update_protection = 2
apc.enable_cli=1

Now restart and check to see it was installed

service httpd restart
php -i | grep apc

Finally set back the /tmp dir to noexec

mount -o remount,noexec,suid /tmp

Or a quicker method, which I haven't tried... so not sure if you have to mess with php.ini, but you'll likely want to check to make sure apc.so exists:

cd /usr/local/src
mount -o remount,exec,suid /tmp
yum install autoconf 
yum install php-pear 
yum install php-devel 
yum install httpd-devel 
yum install pcre-devel 
pecl install apc
mount -o remount,noexec,suid /tmp 

Troubleshooting

If you're on drupal, and keep getting "Unable to allocate memory for pool" errors--even after increasing 'apc.shm_size'--make sure you flush drupal's cache, cause drupal will cache the errors (noticed this on drupal 6) .

BONUS: How to set Magento to use APC

Go into your magento directory

vi app/etc/local.xml

Then paste this in between

<global></global>

like so...

    <global>
    ... other stuff above ...
      <cache>
            <backend>apc</backend>
            <slow_backend>database</slow_backend>
            <prefix>STORENAME_</prefix>
       </cache>
    </global>

Name "STORENAME_" whatever you want... just make it unique, e.g. "mystore_"

How to fix Drupal APC errors

If you're getting strange Drupal errors after enabling APC, put this in your php.ini APC section.

apc.include_once_override=0 ;leave this to 0 to prevent drupal errors
Tags: Server PHP APC