Installing the Alternative PHP Cache (APC)

by on June 5, 2009
in Php

APC is the Alternative PHP Cache, which is a free, open, and robust framework for caching and optimizing PHP intermediate code. What this means is that APC reads your PHP files, parses them into a more efficient binary format and then caches them in memory so that each request for your PHP files and PHP library files can be fed from the parsed cache. This will generally lead to a speed increase when serving a PHP site, especially one with a lot of library files. This post looks at how to install APC for PHP on Linux. The Linux distribution I used was CentOS 5, but it should be fairly similar for most distros.

First of all you need to download the APC code from the PHP PECL library. So change directory to somewhere like /tmp and then get the latest version like so:

$ wget

This will always get the latest version, and in my case when I installed it just now downloaded APC-3.1.2.tgz
So then you need to extract the files:

$ tar -zxf APC-3.1.2.tgz

and change into the APC directory:

$ cd APC-3.1.2

The next step is to run the “phpize” command. This requires that you have PHP development package installed. On CentOS this is php-devel (installed by running “yum install php-devel”) and it should have a similar name on other Linux distros.

$ phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20050922
Zend Extension Api No: 220051025

You then configure APC, telling it where the executable file php-config is. If you don’t know where this is, then do this:

$ whereis php-config

which will return something like:

php-config: /usr/bin/php-config /usr/local/bin/php-config

and then run the command like so:

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

and restarting the Apache web server:

service httpd restart

The APC cache will now be enabled. You can confirm this by creating a script which calls the phpinfo() command and looking for the APC section. It will have been switched on by default by adding a “” line to your /etc/php.ini file, but you may want to add more settings to configure it more. The INSTALL file suggests this (I have put the default values at the end of each line which is what is set if you don’t set anything in the php.ini file):

apc.enabled=1 # default = 1
apc.shm_segments=1 # default = 1
apc.shm_size=128 # default = 30
apc.ttl=7200 # default = 0
apc.user_ttl=7200 # default = 0
apc.num_files_hint=1024 # default = 1000
apc.mmap_file_mask=/tmp/apc.XXXXXX # default = no value
apc.enable_cli=1 # default = 0

And that’s all there is to it. There is also a monitoring script available so you can see what’s being cached and how much memory is being used etc. You can read about this in my “Displaying PHP APC Cache Information” post.



One Response to “Installing the Alternative PHP Cache (APC)”

Post a Comment