Alternative PHP Cache (APC) on Debian
[Update, March 9th: Be sure to read the comments!]
The Alternative PHP Cache is a great way to improve the performance of webservers. I just finished installing it on mokey the server that runs this site and a number of others, primarily because of our increased use of a content management system called eZpublish which is very powerful, but also very memory hungry. There is no Debian package at the moment, so here is how I installed APC:
- Download APC:
cd /usr/local/src
wget http://pecl.php.net/get/APC-3.0.8.tgz - Extract the archive:
tar -xvvzf APC-3.0.8.tgz - Install php and apache development files:
sudo apt-get install apache-dev php4-dev(if you have apache2 it’s apache2-dev)
- Run these commands:
phpize
export CPPFLAGS="-I/usr/include/apache-1.3 -DAPC_PHP4_STAT"
./configure --enable-apc --enable-apc-mmap --with-apxs --with-php-config=/usr/bin/php-configFor apache2 the last line becomes:
./configure --enable-apc --enable-apc-mmap --with-apxs=/usr/bin/apxs2 --with-php-config=/usr/bin/php-config - Build it!
make
sudo make install - Finally, add this line to the bottom of your /etc/php4/apache/php.ini (or /etc/php/apache2/php.ini)
extension=apc.so
Good luck, and leave a comment or contact me if you have any problems. If you don’t feel comforable installing apc yourself contact me and I may be able to help.

February 3rd, 2006 at 05:51
You may also want to have a look at the APC Documentation on php.net.
February 23rd, 2006 at 16:35
Hi,
another possibility is to use the pecl-installer:
apt-get install apache-dev php4-dev
pecl install apc
March 9th, 2006 at 20:43
I had trouble working out how to find the pecl-installer, but figured it out today since I needed to reinstall the latest version of APC on our server. The key package is php-pear (or php4-pear for Debian stable) since apparently pecl uses the same installation mechanisms as pear.
To summarise, it becomes as simple as this…
apt-get install apache-dev php4-dev php-pearpecl install apcSay yes to both the prompts, and then add the following line to your php.ini
extension=apc.so…restart apache, and apc should be enabled. You can check using a copy of phpinfo.php
The only proviso, is that if you’re using apache2 (which I have on my laptop, but not our web server) the build gives an error because it needs the switch
--with-apxs=/usr/bin/apxs2. Adding that to thepecl installcommand didn’t work, so I created a sym-link pointing to apxs2:ln -s /usr/bin/apxs2 /usr/bin/apxsThen
pecl install apcshould run without problems. Phew. Thanks very much for the hint Jonathan!March 12th, 2006 at 14:46
One last thing, since there doesn’t seem to be a man page for pecl. To upgrade APC:
pecl upgrade apc/etc/init.d/apache restartJune 6th, 2006 at 15:58
I just installed it using the pecl installer and matthews
lnfix. Cool. Thanks!I’m currently installing my new webbox and wanted to test this. It’s right next to my desktop and directly connected with 100mb, so I could easily hose the cpu on it. I ran a few tests agains a fairly large phpBB2 system (10000 messages, 700 users and 32 forums) and repeatedly loaded three pages using WAST. Before APC I could pull 15 pages a second and after I could pull 23. I’m running with query caching on in mysql, so apache2 was running the cpu at about 95% in both cases (with mysql and system sharing the rest).
June 28th, 2006 at 13:56
Just a quick note regarding Ubuntu – I’m just setting up a new laptop which is running Ubuntu 6.06 LTS. To set up APC on Ubuntu I also needed to install ‘make’ in addition to the packages above since it isn’t installed by default in Ubuntu’s ‘desktop’ install:
sudo apt-get install makeHow it is that ‘make’ isn’t a dependency of all the other development stuff I’m not quite sure, but there you go. Have fun!
July 22nd, 2006 at 06:53
[...] The first action I implemented was installing Alternative PHP Cache or APC. APC speeds up loading times by caching compiled WordPress PHP files so that the loading time the next time a page is loaded is much faster. This is different to WP-Cache2, which stores the HTML of a page for a user-defined period, which can cause problems if new comments or posts are made within this period i.e. if the page changes. APC doesn’t suffer from this problem and all page loads are dynamic. APC and WP-Cache2 currently can’t be installed at the same time, although Mark Jaquith is working on trying to get the best of both worlds by finding a way to get APC and WP-Cache2 to work together, and I’m pretty sure he will crack it soon. However, given that I can’t even get WP-Cache2 to work by itself I didn’t even attempt to try some of Mark’s experiments. Initially I struggled to install APC as until two weeks ago when I moved from Blogware to Wordpress I’d never even used MySQL before, much less configured a server. Luckily I found these instructions by Matthew Carroll on how to install APC which I almost managed to complete by myself, but Matthew was kind enough to finish the job for me. [...]
November 10th, 2006 at 15:22
For anyone who’s interested in that funny sym-link hack, I finally reported the problem in Debian and upstream:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=397252
http://pecl.php.net/bugs/bug.php?id=9300
…hopefully it will be fixed in APC in due course.
November 21st, 2006 at 20:40
Another update, for anyone using debian etch (currently testing but very soon to become the new stable), or when it comes along Ubuntu 7.04 “Feisty”:
PHP in Debian has now, as of 4:4.4.4-4~4 and 5.1.6-5, moved to a conf.d style configuration to make configuring individual extensions easier. Consequently the activation of APC should not be done in the main php.ini configuration file (
/etc/php4/apache[2]/php.ini) anymore. Instead the appropriate place to activate APC is in a new file, which should be created in/etc/php4/conf.d/.As root:
echo "extension=apc.so" > /etc/php4/conf.d/apc.iniAdjust accordingly for php5. Of course, any other APC specific configuration that you require can also be added to that file. It is possible to still do it the old way and it will work fine, but this is now deprecated, and I recommend sticking to “the Debian way”.
December 27th, 2006 at 11:23
Ultimate Wordpress Site Speed – Install WP-Cache2 AND APC At The Same Time…
I’ve just tweaked my server and finally managed to get WP-Cache2 AND APC (Alternative PHP Cache) to work together at the same time on my WordPress blog. The difference this has made to my page loading speeds is absolutely amazing, and it’s…
February 6th, 2007 at 17:40
Hello, thanks for this great post.
I have never heard of php cache, except mysql query cache supposed to be great aswell for systems doing the same query very fast after one another.
March 18th, 2007 at 17:26
Hey Matthew,
I have tried for roughly four days now to get APC working, and it is driving me crazy. Im on a mediatmple virtual dedicated (cent os) with the developer tools installed.
I have managed to get it built, the apc.so file is in the correct place, but still no luck. When the apc.so file is loaded, it shows an error with it in my apache log.
I think I have tracked it down to a problem with phpize. When i type phpize, I recive this error.
[root@as APC-3.0.13]# phpize
configure.in:9: warning: underquoted definition of PHP_WITH_PHP_CONFIG
run info ‘(automake)Extending aclocal’
or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
configure.in:32: warning: underquoted definition of PHP_EXT_BUILDDIR
configure.in:33: warning: underquoted definition of PHP_EXT_DIR
configure.in:34: warning: underquoted definition of PHP_EXT_SRCDIR
configure.in:35: warning: underquoted definition of PHP_ALWAYS_SHARED
acinclude.m4:19: warning: underquoted definition of PHP_PROG_RE2C
/usr/share/aclocal/libgcrypt.m4:23: warning: underquoted definition of AM_PATH_LIBGCRYPT
Configuring for:
PHP Api Version: 20020918
Zend Module Api No: 20020429
Zend Extension Api No: 20021010
I think it might be libtools or libcrypt. Any idea how I can fix this? You are my last hope :/
Many thanks for the time Matthew.
Matt
March 18th, 2007 at 17:44
Oh, sorry I should have mentioned, I did not do this line “sudo apt-get install apache-dev php4-dev”. I was not sure if I needed to do this as I have apache2 and php installed. I didn;t want to mess with this part.
Thanks again
March 18th, 2007 at 18:30
Just for update.
I just tried the sudo apt-get install apache-dev php4-dev line, and it said, apt-get: command not found :/
Sorry for the multiple comments!
May 23rd, 2007 at 06:51
Hi, Thanks for posting this. I tried to follow the instructions and got as far as
root@vps [/usr/local/src]# sudo apt-get install apache-dev php4-dev
sudo: apt-get: command not found
any ideas how i can get around this please?
Thank you
June 16th, 2007 at 16:14
@Matt: you do need to install the apache and php development packages – for apache2 the relevant package is apache2-dev
@Mentalacrobatics: If you don’t have apt-get, then you don’t have Debian Linux, and this page is irrelevant to you. You should search for an APC package for whichever distribution you are running. Alternatively, if you need help compiling it from source, contact me.
Matthew
July 13th, 2007 at 02:57
Well I have enabled the APC on my debian box by simply doing
(installation of apache)
apt-get install libapache2-mod-php5
(installation of php5)
apt-get install php5-curl php5-gd php5-imap php5-mysql php5-mcrypt php5-xmlrpc php5-xsl php5-cli php5 php5-tidy php5-apc
/etc/init.d/apache2 restart
I hope this is usefull to somebody.
November 6th, 2007 at 18:10
@marwin: you must be using non-standard repositories, because there is no php5-apc package in debian yet. There is an ITP, which followed a RFP I filed two years ago, but no package as yet.
January 29th, 2008 at 00:20
Everything had been worked perfectly with APC and eZ Publish on my Debian Etch amd64 until I issued:
pecl upgrade apc
which upgraded to APC-3.0.16 and created a new library in
/usr/lib/php5/20060613/apc.so
which replaced the previous
/usr/lib/php4/20050606/apc.so [APC-3.0.14 - shoot, I don't have a backup...]
The problem must be that I installed php5 [cgi] after php4 and apc…
If I move the new apc.so to the correct extension_dir, Apache2 can’t load it and says:
PHP Warning: Unknown(): Unable to load dynamic library ‘/usr/lib/php4/20050606/apc.so’ – /usr/lib/php4/20050606/apc.so: undefined symbol: zend_opcode_handlers in Unknown on line 0
How can I recompile apc for just PHP/4.4.4-8+etch4 and give eZ Publish a cache again?
Regards,
TM.
March 29th, 2008 at 10:03
To install APC on a minimal Ubuntu Gutsy (7.10) I just typed:
apt-get install php-pear php5-dev gawk apache2-threaded-dev make
pecl install apc
..added “extension=apc.so” to the php.ini and restarted Apache – done!
Also see http://2bits.com/articles/installing-php-apc-gnulinux-ubuntu-gutsy-gibbon-710-and-debian.html
January 10th, 2009 at 01:59
Hey, could you explain how to install in CentOS 5?, ive been trying but the yum really sucks, and im not an expert in this area, i would appreciate the help.
Thanks
March 29th, 2009 at 21:29
I found this on http://www.howtoforge.com/apc-php5-apache2-debian-etch as a comment:
apt-get install php-apcIt worked for me, even after pecl istallation.
For monitoring APC activity, copy or move /usr/share/php/apc.php to your webspace. Edit it first (user/password)
June 4th, 2009 at 01:24
Yay, install php-apc went smoth, and tested it with joomla cacheing. All fine so far
June 24th, 2009 at 09:28
[...] Matthew Carroll provided the outline for the procedure above. [...]