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
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
- PHP APC home page.
- Matthew Carroll provided the outline for the procedure above.
- Darrel O'Pry on Debian and APC provides an alternative method.
- Benchmarks of various web servers with Drupal by Dries. APC makes a huge difference.
- List of PHP op code caches.
Comments
Swaminathan (not verified)
Php-apc
Thu, 2007/12/06 - 05:43Hi, Thank you very much...
The Php-apc is working fine...
thanks a lot
luv
Swami
Don Piano (not verified)
"Fatal error: Allowed memory size of 8388608 ... " - fixed
Fri, 2008/02/01 - 17:35Thank 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
.
.
.
Srini (not verified)
APC installation errors
Sat, 2008/02/09 - 05:05Hi,
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 :(!!
George Popov (not verified)
APXS error
Thu, 2008/06/26 - 04:48To 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?
joel (not verified)
Thanks
Mon, 2008/09/08 - 03:19Thanks for this wonderfull post. It really helped me!!!!! :)
Norio (not verified)
Great article
Tue, 2008/12/09 - 01:08Great article, thanks :) I've just installed APC now and it's working beautifully!
GalaxyBoss (not verified)
Usefull
Sat, 2010/01/09 - 05:08thank it was useful informaton
test and work in ubuntu 9.10
Mark Rairdon (not verified)
pecl command fails
Sun, 2010/06/20 - 00:25with 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
Anonymous (not verified)
Same error here on ubuntu
Tue, 2010/08/24 - 07:23Same 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
Tom129 (not verified)
Old/New
Thu, 2011/10/13 - 11:14Dotdeb 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