Installing PHP APC on Ubuntu Dapper (and Debian?)

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:

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

Running pecl

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.

Others have encountered this problem, and the workaround as provided in this page, is to adjust the memory inside the script (it does not work in the php.ini).

Edit the /usr/share/php/pearcmd.php, and add:

@ini_set('memory_limit', '16M');

Configuration

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:

extension=apc.so

Now, restart Apache using:

/etc/init.d/apache2 restart 

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

Followed by:

 /etc/init.d/apache2 restart

More info

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.

Links and Resources

Contents: 

Comments

"Fatal error: Allowed memory size of 8388608 ... " - fixed

Thank you! I was trying to find/fix the cause of the error "Fatal error: Allowed memory size of 8388608 bytes exhausted...", and the method you recommended of adding "@ini_set('memory_limit', '16M');" to the pearcmd.php file worked. For anyone interested, this is where I put it in the file:
.
.
.
@ini_set('track_errors', true);
@ini_set('html_errors', false);
@ini_set('magic_quotes_runtime', false);
@ini_set('memory_limit', '16M');//HACK to fix pecl install memory issue
.
.
.

APC installation errors

Hi,

Just wanted to paste the umpteen errors and their resolution during installation of APC. And you know what I still haven't see the end of the tunnel :(

I am installing this in Ubuntu and had configured PHP and apache using XAMPP.

Installing APC
1. Try the below command to install APC
sudo /opt/lampp/bin/pecl install apc
i. Got an error relating to Autoconf
" Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF
environment variable is set correctly and then rerun this script."

Resolved this by
sudo apt-get install autoconf

ii. This ask for something like "Use apxs" - give "yes" for this and then as 1-1, all, abort or Enter to continue. Enter here to continue.
2. Got an error here
"Configure: error: C compiler cannot create executables
See `config.log' for more details."
Resolved this using
sudo apt-get install g++

3. Got an error again

"Sorry, I was not able to successfully run APXS. Possible reasons:
1. Perl is not installed;
2. Apache was not compiled with DSO support (--enable-module=so);
3. 'apxs' is not in your path. Try to use --with-apxs=/path/to/apxs"
Resolved this using
sudo apt-get install apache2-threaded-dev

4. Got another error
error apc.h:61:17 php.h No such file or directory

Still looking for a solution to this :(!!

APXS error

To all the people who get the apxs error - Is there any chance your writing "y" instead of "yes" (or just hitting enter) when you get prompted whether or not to compile with apxs?

Usefull

thank it was useful informaton

test and work in ubuntu 9.10

pecl command fails

with this error on Ubuntu 10.04 Lucid Linx (64 bit) with the following error:

/tmp/pear/temp/APC/php_apc.c:959: error: duplicate ‘static’
make: *** [php_apc.lo] Error 1
ERROR: `make' failed

I used sudo apt-get install php-apc then restarted apache. Works fine. Hope that helps someone else with the same problem.

Mark

Same error here on ubuntu

Same error here on ubuntu 10.04 64 bit

found this bug report http://pecl.php.net/bugs/bug.php?id=16078
Sollution sudo pecl install apc-beta
worked perfectly

Old/New

Dotdeb has php4-apc and php5-apc packages of course usable only when you use php from dotdeb packages. But for me the biggest problem is that APC is not compatible with Zend Optimizer which in fact is not optimizer/cache, it enables usage of encoded php files.
So I tried to compile e accelerator and it works great. Compiling is very easy, you only need to install php{4,5}-dev.
Search for few benchmarks it seems e Accelerator is very close to APC in performance.

Pages