APC Info Page

How to Install APC (Alternative PHP Cache) on CentOS 5.6 46


There’s a lot of conflicting information out there on how to install the APC opcode cache on a CentOS 5.6 box. Here’s how I did it:

This tutorial assumes you’re running CentOS 5.6 on a dedicated server, and that you have superuser (root) access. These instructions may also worked in a VPS or shared hosting environment, but if you run into trouble, you should contact your provider’s tech support to see if they have any alternative steps you should take.

First, you need to make sure the following packages are installed:

yum install php-devel pcre-devel

Because you’re going to be compiling a package, you’ll need a C compiler (like gcc) and make. The easiest way to install all the right development tools is with:

yum groupinstall "Development Tools"

Be sure to include those quotes.

Now you’re ready to download and untar the package:

cd /usr/local/src
wget http://pecl.php.net/get/APC-3.1.9.tgz
tar -zxvf APC-3.1.9.tgz

Now it’s time to set it up!

cd APC-3.1.9
phpize
whereis php-config

Take note of where php-config is located (it’s usually in /usr/bin/php-config) as you’ll need it in the next step. If it’s in a different location, use that for the php-config path below:

./configure --enable-apc --enable-apc-mmap --with-apxs --with-php-config=/usr/bin/php-config
make
make install

Next, if you have an /etc/php.d/ directory on your server, create a file called /etc/php.d/apc.ini to store all your configuration settings (if it already exists, you can just edit the existing one). If you don’t have an /etc/php.d/ directory, you can add these configuration settings to your existing php.ini file, which is usually located at /etc/php.ini.

Here’s an example of my apc.ini file, with lots of comments for some guidance. This is a pretty straightforward configuration, so feel free to copy and paste:

; Enable the extension module
extension = apc.so

; Options for the APC module version >= 3.1.3
; See http://www.php.net/manual/en/apc.configuration.php

; 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
apc.shm_size=64M
; 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=7200
; 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=7200
; The number of seconds that a cache entry may remain on the garbage-collection list.
apc.gc_ttl=3600
; 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 setting is deprecated, and replaced with apc.write_lock, so let's set it to zero.
apc.slam_defense=0

The final step is to restart Apache with either:

service httpd restart

or

apachectl restart

To make sure APC is running, create a php file somewhere in your web root that simply contains:

then load the file and scroll down to the APC section to verify that it’s enabled.

For some cool statistics, copy the usr/local/src/APC-3.1.9/apc.php file somewhere in your web root, and then open it in your browser. Edit the file on the server if you’d like to add a password and enable login to see more details. Here’s a peek at what it looks like:

APC Info Page

Congratulations! You’ve just installed APC on CentOS 5.6!

Further Reading