How to install PHP APC on Ubuntu and CentOS

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP which we use within Campsite to provide better performance. This is a mini-howto that explains how to get APC running for your Ubuntu or CentOS server. After that, you will be able to install Campsite and enjoy.

1. Ubuntu

This guide has been tested successfully on dapper and edgy.

Requirements

You need the following Ubuntu packages:

  • apache2
  • php5
  • php5-dev
  • apache2-dev
  • php-pear

If they are not present in your system, please install them. The easiest way is by running:

# apt-get install apache2 apache2-dev php5 php5-dev php-pear

Installation via PECL

As of apache2, the apxs script is named differently, and pecl will fail if you do not work around this. So, you have got to provide an apxs which is just a symbolic link.

# ln -s /usr/bin/apxs2 /usr/bin/apxs

Next we use the pecl utility to download, compile and install APC.

# pecl install apc

This is a straight forward process which won’t require human intervention. At the end you will have APC installed on your system. Now you need to setting it up.

Configuration

All what you need to do is to add the following to the /etc/php5/apache2/php.ini file:

extension=apc.so

Usually you should add it at the end of the file.

Now, restart apache and you will be done.

# /etc/init.d/apache2 restart

Troubleshooting

I have added the extension=apc.so line to my php.ini file and restarted apache, however APC functions are not available for my PHP, what could i do?

Run phpinfo() and take a look at the extension_dir option, its value shows the full path to the PHP extensions directory, make sure your new apc.so module was stored in this directory, otherwise put it in there and restart apache.

2. CentOS

The installation process for CentOS is very similar. This installation was tested on CentOS 5.2

First of all, install all needed dependencies

#yum install php-devel php-pear httpd-devel

After that, you should be able to install APC using pecl

#pecl install apc

To configure PHP, create new file /etc/php.d/apc.ini and put the following line into it

extension=apc.so

Restart apache to make changes effective

#/etc/init.d/httpd graceful

That’s all, now you should be able to use APC cache. If you will need to upgrade APC in the future, just do

#pecl upgrade apc