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
Nick (not verified)
Memory limit
Tue, 2006/06/20 - 22:51When you say the memory limit doesn't work by changing the php.ini file, did you change the /etc/php5/cli/php.ini? My dapper system has separate ini files, and I've had to change the command line memory limit in the past to get some pear install stuff to work.
Khalid
Tried them all
Wed, 2006/06/21 - 09:22Yes, I tried them all, but still did not work:
This is a problem on amd64 Dapper only. The other two machines worked fine.
christo (not verified)
Ubuntu server
Tue, 2006/10/03 - 09:44Just did this on my ubuntu server remotely from work, great write up.
Don't forget to apt-get install make, g++ and gcc -- actually spent longer working out g++ wasn't installed on my minimalist server.
Matthew (not verified)
php.ini now uses conf.d
Tue, 2006/11/21 - 21:36For php5 + apache2 + debian etch, the APC activation "extension=apc.so" should now be placed in /etc/php5/conf.d/apc.ini instead of /etc/php5/apache2/php.ini. This is because, as of version 5.1.6-5, php in debian has been reworked to use a conf.d style configuration for extensions. This change should hit Ubuntu 7.04 "Feisty", and also applies to the latest versions of php4. More here.
Heine (not verified)
Debian Sarge
Thu, 2006/11/23 - 02:07When running Debian Sarge a few extra steps are needed:
You have to upgrade pear before pecl is available. Before:
Upgrade pear to 1.3.3 (< versions do not upgrade automatically to the latest)
Next upgrade to the latest pear (or issue pear upgrade-all)
During this process you may need to increase the memory limit as described to as high as 64M. After this, pecl is available.
Anonymous (not verified)
while installing APC on my
Thu, 2007/11/01 - 08:00while installing APC on my ubuntu machine i am getting bellow errors:
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
./configure: line 3845: apxs: command not found
configure: error: Aborting
but i installed perl also . please help me
Anonymous (not verified)
Installed on etch without a
Fri, 2007/04/20 - 03:09Installed on etch without a problem. For now works ok.
Thanks
Matt (not verified)
Possible error in install
Sat, 2007/05/12 - 20:31Excellent tutorial for installing APC!
I had issues finding the apxs program. What worked for me was installing the apache2-threaded-dev package instead of apache2-dev (which was not found in my repositories). Also, with this package I did not need to use the symbolic link instruction.
Thanks again for the guide.
Shrinivasan (not verified)
Thanks
Mon, 2007/06/25 - 06:30Hi.
A lot of thanks to u.
My web app is now working in rocket speed.
Great article.
kpk (not verified)
hai all, I am not able to
Fri, 2007/11/02 - 04:17hai all,
I am not able to finding the problem while installing apc.And I am getting below errors. please any one can help me.
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 /usr/bin/ follows
/tmp/pear/cache/APC-3.0.15/configure: line 3845: /usr/bin/: is a directory
configure: error: Aborting
ERROR: `/tmp/pear/cache/APC-3.0.15/configure --with-apxs=/usr/bin/' failed
and i installed perl like below
root@prashanth-desktop:~# which perl
perl perl5.8.8 perlbug perlcc perldoc perlivp
root@prashanth-desktop:~# which perl
/usr/bin/perl
root@prashanth-desktop:~#
then i configured /etc/php5/conf.d/apc.ini instead of /etc/php5/apache2/php.ini.
thanks
Pages