Update: this article is for Ubuntu Dapper Drake (6.06). If you are using later versions, or other Linux distributions, then visit the following links:
- Installing PHP APC on GNU/Linux Gusty Gibbon 7.10 and (Debian).
- Installing PHP APC on GNU/Linux CentOS 5.
APC is a op-code cache for PHP that eliminates the parsing and compiling of script(s) for every page hit. Unlike Zend, it is truly free, and seems to have a lot of commuity momentum behind it lately.
As with all optimizations, there is a trade off. APC can use more memory on your server than without it.
The instructions below assumes that you have full control of your server, whether a physical dedicated server or a virtual private server.
Although one can always compile from source, this is more time consuming than using Debian's excellent apt-get.
Here is how I did it on two machines, with one more to go.
Getting the packages
First, we run apt-get to get what we need, if it is already not there.
apt-get update apt-get install apache2-dev php5-dev php-pear make
Preparing for pecl
I am using Apache2 and PHP5 on the development server. Adjust the versions above if necessary to match your environment.
Since I am using Apache2, the apxs is named differently, and pecl will fail if I do not work around this. So, I had to provide an apxs which is just a symbolic link.
ln -s /usr/bin/apxs2 /usr/bin/apxs
Next we use pecl to download, compile and install APC.
pecl install apc
My development server is now an AMD Athlon 64, using a 64-bit kernel. It had a strange problem running the above pecl command, with an error saying:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 143 bytes) in /usr/share/php/PEAR/PackageFile/v2.php on line 1772
The same Ubuntu version on my Pentium III test server does not have this error at all. It has the same Apache and PHP versions as well.
Edit the /usr/share/php/pearcmd.php, and add:
Now you can run the above pecl command once more.
Now, all you need to do is to add the following to the /etc/php5/apache2/php.ini file:
Now, restart Apache using:
Now you have a code cache that should speed up Drupal, and you can benefit.
To upgrade APC in the future, use the commands:
pecl upgrade apc
APC is not the only op-code cache / accelerator.
You can read more in PHP op-code caches / accelerators Drupal big site study.
There is also a benchmark of APC and eAccelerator using Drupal.