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:

  1. Download APC:

    cd /usr/local/src
    wget http://pecl.php.net/get/APC-3.0.8.tgz

  2. Extract the archive:

    tar -xvvzf APC-3.0.8.tgz

  3. Install php and apache development files:

    sudo apt-get install apache-dev php4-dev

    (if you have apache2 it’s apache2-dev)

  4. 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-config

    For apache2 the last line becomes:

    ./configure --enable-apc --enable-apc-mmap --with-apxs=/usr/bin/apxs2 --with-php-config=/usr/bin/php-config

  5. Build it!

    make
    sudo make install

  6. 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.

25 Responses to “Alternative PHP Cache (APC) on Debian”

  1. Gravatar Image

    You may also want to have a look at the APC Documentation on php.net.

  2. Gravatar Image

    Hi,

    another possibility is to use the pecl-installer:

    apt-get install apache-dev php4-dev
    pecl install apc

  3. Gravatar Image

    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-pear
    pecl install apc

    Say 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 the pecl install command didn’t work, so I created a sym-link pointing to apxs2:

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

    Then pecl install apc should run without problems. Phew. Thanks very much for the hint Jonathan! :-)

  4. Gravatar Image

    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 restart

  5. Gravatar Image

    I just installed it using the pecl installer and matthews ln fix. 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).

  6. Gravatar Image

    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 make

    How 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!

  7. Gravatar Image

    [...] 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. [...]

  8. Gravatar Image

    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.

  9. Gravatar Image

    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.ini

    Adjust 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”.

  10. Gravatar Image

    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…

  11. Gravatar Image

    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.

  12. Gravatar Image

    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

  13. Gravatar Image

    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

  14. Gravatar Image

    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!

  15. Gravatar Image

    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

  16. Gravatar Image

    @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

  17. Gravatar Image

    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.

  18. Gravatar Image

    @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.

  19. Gravatar Image

    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, :D TM.

  20. Gravatar Image

    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

  21. Gravatar Image

    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

  22. Gravatar Image

    I found this on http://www.howtoforge.com/apc-php5-apache2-debian-etch as a comment:

    apt-get install php-apc

    It 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)

  23. Gravatar Image

    Yay, install php-apc went smoth, and tested it with joomla cacheing. All fine so far :)

  24. Gravatar Image

    [...] Matthew Carroll provided the outline for the procedure above. [...]

  25. Gravatar Image

    [...] and.. it fails, because we are running apache2. [...]

Leave a Response



XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Search
  • Meta

  • Old Browser

    It seems you are using an old web browser (e.g. Internet Explorer 5 or below). This is a security risk to you, since Microsoft no longer releases updates for old versions of Internet Explorer. Also, note that this site is designed to modern internet standards, and the layout may appear strange or plain in older browsers. All the content is still accessible to you, but I strongly recommend you upgrade to a modern, safe, standards-complient browser, such as Firefox. For more information on getting the best experience surfing the web, see browsehappy.com.