This site is now 100% read-only, and retired.

Installing the Alternative PHP Cache (APC)

Posted by tuoermin on Fri 11 Jan 2008 at 12:56

The Alternative PHP Cache (APC) is a free, open, and robust framework for caching and optimizing PHP intermediate code. It's an PECL extension which shares the packaging and distribution system with its sister, PEAR.

Provided that you have PEAR (php5-pear) package installed on your system, the procedure is as simple as pecl install apc. Alternatives.

meglohvat:# pecl install apc
downloading APC-3.0.16.tgz ...
Starting to download APC-3.0.16.tgz (114,298 bytes)
.............done: 114,298 bytes
45 source files, building
running: phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
Use apxs to set compile flags (if using APC with Apache)? [yes] :

As probably corectly guessed by PECL package managers, you are runing an Apache web server so you should confirm by typing yes or hitting Enter key.

Errors which you may (I did) encounter

checking for re2c... no
configure: WARNING: You will need re2c 0.9.11 or later if you want to \
regenerate PHP parsers.

The re2c package was missing on my system ›› apt-get install re2c.

checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no

I had no gawk or nawk but mawk pattern scanning and processing language installed on system ›› apt-get install gawk.

checking whether apc needs to get compiler flags from apxs...

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
The output of apxs follows
/tmp/tmpJQuZdD/APC-3.0.16/configure: line 3846: apxs: command not found
configure: error: Aborting
ERROR: `/tmp/tmpJQuZdD/APC-3.0.16/configure --with-apxs' failed

Missing APache eXtenSion (APXS) tool for building and installing extension modules for the Apache HyperText Transfer Protocol (HTTP) server. APXS is found in apache2-threaded-dev package. The command apt-get install apache2-threaded-dev will also install a long chain of dependencies.

Setting up APC

A good place to start is Alternative PHP Cache chapter in PHP manual. There it's written that the default configuration is sutable for most instalations, but they single out two settings: apc.shm_size and apc.stat.

apc.shm_size
Here you should enter the size of each shared memory segment in MB, the default value being 30MB. By default, the maximum size of shared memory segment in Debian is 33554432 or 33MB. You can check it by cat /proc/sys/kernel/shmmax command. If you want to know more about shared memory segments, read this forum thread.
apc.stat
From PHP manual:
Be careful if you change this setting. The default is for this to be On which means that APC will stat (check) the script on each request to see if it has been modified. If it has been modified it will recompile and cache the new version. If you turn this setting off, it will not check. That means that in order to have changes become active you need to restart your web server. On a production server where you rarely change the code, turning stats off can produce a significant performance boost.
For included/required files this option applies as well, but note that if you are using relative path includes (any path that doesn't start with / on Unix) APC has to check in order to uniquely identify the file. If you use absolute path includes APC can skip the stat and use that absolute path as the unique identifier for the file.

If you change these settings or not, the next step is to move the /usr/share/php/apc.php script into your webserver path and access it with browser. It provides a detailed look at what is happening with your cache and, with GD enabled in PHP, also shows you graphs of the situation of your cache.

After checking if caching actualy works, you should point your eyes to the Cache full count value (on the left tables under File Cache Information). Tne number tells you how many times the cache filled up the allocated memory and had to be cleared of entries not accessed within number of seconds set up with apc.ttl setting. "You should configure your cache to minimize this number if not the resulting cache churn is going to hurt performance. You should either set more memory aside for APC, or use apc.filters to cache fewer scripts."*

 

 


Re: Installing the Alternative PHP Cache (APC)
Posted by undefined (192.91.xx.xx) on Tue 15 Jan 2008 at 17:56
on sarge i used apc, but since migrating to etch i use xcache.

i initiated using apc back when turk-mmcache was finally being written off as dead, eaccelerator was a licensing nightmare (as far as debian was concerned), and zend was/is closed source. i had to debug compilation issues with apc to get it to compile against apache2 on x86-64. i didn't feel my bug/patch was handled well by upstream, so i was prepared to go elsewhere when i migrated to etch.

i had heard of xcache and already seen it enter the debian archive here, so when i was preparing to migrate to etch i figured i would just backport it. originally there were both php4 & php5 packages, but the php4 package disappeared when php4 was dropped from unstable. doesn't really matter because it probably would have been just as easy to drop the php5 half of the build as it was to s/php5/php4/ the current source package. so over the holidays i migrated from sarge & apc to etch & xcache 1.2.1-3 (as that's what was in testing at that time).

anyways, in my admittedly crude benchmarking (though my approach was similar to what i saw published elsewhere) xcache was a little faster than apc, but it's not discernable during use (where both are ~4x faster than plain php). but, then again, based on the response to my previous apc bug report, <sarcasm>what do i know about benchmarking</sarcasm>.

[ Parent ]

Re: Installing the Alternative PHP Cache (APC)
Posted by marki (15.195.xx.xx) on Fri 18 Jan 2008 at 12:27
If you are using etch, 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 eaccelerator (last version is only few months old) and it works great (also with Zend Optimizer). No problem with sarge+php4, sarge+php5 and etch+php4 (all php from dotdeb). Compiling is very easy, you only need to install php{4,5}-dev (which has few dependencies like m4 or autoconf).
Search for few benchmarks it seems eAccelerator is very close to APC in performance.

[ Parent ]

Re: Installing the Alternative PHP Cache (APC) etch
Posted by Anonymous (87.12.xx.xx) on Mon 25 Feb 2008 at 07:46
remember install this packges because apxs can give you
some errors when compile

#apt-get install php5-dev apache2-threaded-dev



Ricardo C.
www.auyama.it

[ Parent ]

Re: Installing the Alternative PHP Cache (APC) etch
Posted by Anonymous (68.190.xx.xx) on Thu 22 May 2008 at 08:15
Aren't we supposed to use apache2-prefork-dev because php5 is supposed to be used with the prefork one?

[ Parent ]

Re: Installing the Alternative PHP Cache (APC)
Posted by Anonymous (77.24.xx.xx) on Mon 30 Mar 2009 at 02:33
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)

[ Parent ]

Re: Installing the Alternative PHP Cache (APC)
Posted by tuoermin (193.2.xx.xx) on Mon 30 Mar 2009 at 07:47
Great, I'll try this when I upgrade my servers to Lenny. The php-apc is not available for Etch.

[ Parent ]

Re: Installing the Alternative PHP Cache (APC)
Posted by Anonymous (67.188.xx.xx) on Wed 8 Sep 2010 at 21:20
yum install httpd-devel for CentOS users

[ Parent ]