Installing PHP APC on GNU/Linux Centos 5

Published Mon, 2008/03/24 - 13:49, Updated Wed, 2009/07/15 - 23:40

Complex PHP applications, such as Drupal, can gain a lot of performance benefits from running a PHP op-code cache/accelerators.

APC,
Alternate PHP Cache, is now the most maintained free/open source op-code cache, and is being used more and more as it emerges to be the
most stable.

The instructions here detail how to get APC running on a CentOS 5 server. The server happened to have Plesk on it as well, which initially made me hesitant to install APC "normally", since Plesk is so picky on what other software is installed on the server. However, it seems to have worked out well.

First, we need the pecl command so we can download and install APC from the repositories.

Do to so, we execute the following command:

yum install php-pear

But, this will not run on its own, we need the following package for the phpize command:

yum install php-devel

We also need the apxs command, which is installed via the following package:

yum install httpd-devel

Now we have all the software we need, so we install apc via the pecl command:

pecl install apc 

Once that finishes, we need to enable apc in Apache's configuration. the following command should do this for us.

echo "extension=apc.so" > /etc/php.d/apc.ini

Then we restart Apache:

/etc/init.d/httpd start

And we are all done. Watch for less execution time per page, and decreased memory usage per Apache process compared to what you had
before.

Links

You can find some additional tips at Setting up Alternative PHP Caching APC support on Centos server.

How do I specify what version of APC I am instaling

I need to be sure that I am installing either APC 3.0.19, 3.1.3p1 or 3.1.9.

Do I add something to this comand?

pecl install apc

Latest will be installed

You just use the command:

pecl install apc

And the latest will be installed for you.

I actually do not want the

I actually do not want the latest version. The software that I am using and need to speed up said not to install the latest version because it was quirky.

Odd ...

That is odd, but you can try:

pecl install apc-version

or

pecl install apc-version/beta

Replace "version" above by the specific version you want installed.

module issue

I'm getting this after fiddling trying to get APC up and running


$ php bf_worker.php
PHP Warning: PHP Startup: apc: Unable to initialize module
Module compiled with module API=20090626
PHP compiled with module API=20121212
These options need to match
in Unknown on line 0

Any ideas MUCH appreciated.

You don't mention that when

You don't mention that when you run:
pecl install apc
you are asked a question (or maybe it's just the first of a series):

Enable internal debugging in APC [no] :

What does that mean and what are the pros and cons of replying "yes" or "no" ?

Hit enter

You just hit enter. The default is fine.

But, this was for an older version of Ubuntu. The latest version (12.04) has a php-apc package that works well, with no need to install using pecl.

The upload is working but the

The upload is working but the progress bar is remains at 0% what can i do ? :( please help

problem

Can't run the first command successfully. Here is what I get:

Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Determining fastest mirrors
Plesk | 951 B 00:00
Plesk-Third-Party | 951 B 00:00
base | 1.1 kB 00:00
extras | 2.1 kB 00:00
updates | 1.9 kB 00:00
Excluding Packages in global exclude list
Finished
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-pear.noarch 1:1.4.9-6.el5 set to be updated
--> Processing Dependency: php-devel for package: php-pear
--> Running transaction check
---> Package php-devel.x86_64 0:5.1.6-27.el5_5.3 set to be updated
--> Processing Dependency: php = 5.1.6-27.el5_5.3 for package: php-devel
--> Processing Dependency: autoconf for package: php-devel
--> Processing Dependency: automake for package: php-devel
--> Running transaction check
---> Package autoconf.noarch 0:2.59-12 set to be updated
--> Processing Dependency: imake for package: autoconf
---> Package automake.noarch 0:1.9.6-2.3.el5 set to be updated
---> Package php-devel.x86_64 0:5.1.6-27.el5_5.3 set to be updated
--> Processing Dependency: php = 5.1.6-27.el5_5.3 for package: php-devel
--> Running transaction check
---> Package imake.x86_64 0:1.0.2-3 set to be updated
---> Package php-devel.x86_64 0:5.1.6-27.el5_5.3 set to be updated
--> Processing Dependency: php = 5.1.6-27.el5_5.3 for package: php-devel
--> Finished Dependency Resolution
php-devel-5.1.6-27.el5_5.3.x86_64 from base has depsolving problems
--> Missing Dependency: php = 5.1.6-27.el5_5.3 is needed by package php-devel-5.1.6-27.el5_5.3.x86_64 (base)
Error: Missing Dependency: php = 5.1.6-27.el5_5.3 is needed by package php-devel-5.1.6-27.el5_5.3.x86_64 (base)
You could try using --skip-broken to work around the problem
You could try running: package-cleanup --problems
package-cleanup --dupes
rpm -Va --nofiles --nodigest
The program package-cleanup is found in the yum-utils package.

What do the last two errors mean and how should I fix it?

php-devel-5.1.6-27.el5_5.3.x86_64 from base has depsolving problems
--> Missing Dependency: php = 5.1.6-27.el5_5.3 is needed by package php-devel-5.1.6-27.el5_5.3.x86_64 (base)
Error: Missing Dependency: php = 5.1.6-27.el5_5.3 is needed by package php-devel-5.1.6-27.el5_5.3.x86_64 (base)

pblm make install

make: *** [apc.lo] Erreur 1
ERROR: `make' failed
[root@ks32755 ~]#

for : pecl install apc
:/

A video tutorial would be

A video tutorial would be greatly appreciated as I'm sure there are many newbies such as myself who don't have the slightest idea how to work this.

Between google, dealing with a hosting helpdesk who is losing his/her patience, and my own complete frustration with trying to figure this out, I am requesting a video tutorial please.

yum install pcre-devel did

yum install pcre-devel did the trick. APC installed.

In phpinfo() apc shows as one of the modules but none of the variables like those below show.

apc.enabled=1
apc.shm_segments=1
apc.optimization=0
apc.shm_size=32
apc.ttl=0
#apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/XXXXXX
apc.enable_cli=1
apc.cache_by_default=1

How do we know its installed and working? Also, we located apc.php but executing it in a browser gives us 500 error

Thanks a ton

This did the trick. Thank

This did the trick. Thank you!

Need to yum install

Need to

yum install pcre-devel

for centos 5

Same - Thanks. It solved my

Same - Thanks. It solved my problem

I was having problem to

I was having problem to install APC via 'pecl' cause I didn't install the 'pcre-devel'.

This was very helpful! Thank you very much guys! :)

How do you test it's working?

Assuming that all the installation runs ok and you manage to restart Apache, is there some simple way of verifying that it is working other than just empirically noting page load times?
Also, how do you go about uninstalling if it doesn't work?
Otherwise, many thanks for a useful (and easy to read!) tutorial.

Several things

There are several things that you can do to verify that APC is working.

1. Check the output of phpinfo(). There should be an APC section there with the parameters it is using.

2. Search /usr/share for a script called apc.php. Run it, and you will see hit rate, memory usage, fragmentation, ...etc.

While performance varies depending on site complexity, the minimum improvement is that you should feel that most pages are at least 200 milliseconds less.

problem in apc install on centos version 5.5

we have install pac

we have following step
1)yum install php-pear php-devel httpd-devel

2)pecl install apc

Then facing this problem:

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
/var/tmp/APC/configure: line 3457: apxs: command not found
configure: error: Aborting
ERROR: `/var/tmp/APC/configure --with-apxs' failed

Error troubleshooting

I received the following error when trying the documented steps above:

/var/tmp/APC/php_apc.c:959: error: duplicate 'static'
make: *** [php_apc.lo] Error 1
ERROR: `make' failed

I tried installing apc-beta, but got the following error:

/bin/sh /var/tmp/pear-build-root/APC-3.1.4/libtool --mode=compile cc -I. -I/var/tmp/APC -DPHP_ATOM_INC -I/var/tmp/pear-build-root/APC-3.1.4/include -I/var/tmp/pear-build-root/APC-3.1.4/main -I/var/tmp/APC -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /var/tmp/APC/apc.c -o apc.lo
libtool: compile: cc -I. -I/var/tmp/APC -DPHP_ATOM_INC -I/var/tmp/pear-build-root/APC-3.1.4/include -I/var/tmp/pear-build-root/APC-3.1.4/main -I/var/tmp/APC -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /var/tmp/APC/apc.c -fPIC -DPIC -o .libs/apc.o
In file included from /var/tmp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:29:18: error: pcre.h: No such file or directory
In file included from /var/tmp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:37: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
/usr/include/php/ext/pcre/php_pcre.h:38: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
/usr/include/php/ext/pcre/php_pcre.h:44: error: expected specifier-qualifier-list before 'pcre'
/var/tmp/APC/apc.c:356: error: expected specifier-qualifier-list before 'pcre'
/var/tmp/APC/apc.c: In function 'apc_regex_compile_array':
/var/tmp/APC/apc.c:417: error: 'apc_regex' has no member named 'preg'
/var/tmp/APC/apc.c:417: error: 'apc_regex' has no member named 'preg'
/var/tmp/APC/apc.c:418: error: 'apc_regex' has no member named 'nreg'
/var/tmp/APC/apc.c:418: error: 'apc_regex' has no member named 'nreg'
/var/tmp/APC/apc.c: In function 'apc_regex_match_array':
/var/tmp/APC/apc.c:450: error: 'apc_regex' has no member named 'preg'
/var/tmp/APC/apc.c:450: error: 'apc_regex' has no member named 'preg'
/var/tmp/APC/apc.c:451: error: 'apc_regex' has no member named 'nreg'
/var/tmp/APC/apc.c:451: error: 'apc_regex' has no member named 'nreg'
make: *** [apc.lo] Error 1
ERROR: `make' failed

After a little googling I found the solution:

yum install pcre-devel
pecl install apc-beta

Hope this helps

I was on this page and

I was on this page and looking for solution... but I was blinded to see this post... so dumb! After one hour and a lot of trail and error I finally have found the same solution you post here!

ECHO COMMAND IS WRONG

Jeez, over two years of people overwriting their php.ini files and no one has complained? Perhaps they didn't realize what happened. Or maybe they did and were too embarrassed to respond to you. Didn't test this, did ya? :)

This is 100% dead wrong:

echo "extension=apc.so" > /etc/php.d/apc.ini

It should be:

echo "extension=apc.so" >> /etc/php.d/apc.ini

That's a rookie mistake. :) Fix your instructions!

echo command is OK

That file do not exist it is not appending things to php.ini it is creating new file with contest that echo outputs

Too much noise ...

You are making too much noise without substance.

Double check your facts before making accusations that make you look like a fool in public!

I just checked the above instruction on a pristine CentOS 5.4.

The /etc/php.d/apc.ini file does not exist after pecl installs apc.


# ls -l /etc/php.d
total 4
-rw-r--r-- 1 root root 51 Mar 31 02:51 dbase.ini

Now, we create the file using the instructions I provided:

# echo "extension=apc.so" > /etc/php.d/apc.ini

And now it exists where it has not before:

# ls -l /etc/php.d
total 8
-rw-r--r-- 1 root root 17 Jul 14 10:29 apc.ini
-rw-r--r-- 1 root root 51 Mar 31 02:51 dbase.ini

Learn the difference between /etc/php.ini and /etc/php.d/apc.ini. Perhaps this is lesson will be of some benefit to you.

I still maintain that it'll

I still maintain that it'll overwrite the file if it exists. And yes, I will admit that I got the php.ini and apc.ini files mixed up. Seems to me the better place to include the extension is in the php.ini file, which is where all the other extensions are defined anyway. I believe some of the other install tutorials I found didn't mention the apc.ini file at all and edited the php.ini directly.

So, my error, no big deal. :)

Yes and no ...

Yes, any file that a > is used for will be overwritten. That is standard UNIX shell behavior. No one has debated that.

As for php.ini being a better place, you are wrong again. The whole idea of separating out the configurations into small extension specific files is that they can cleanly be created when an extension is installed and removed when it is uninstalled, without affecting other extensions.

Most distro now do it that way, instead of having one monolithic file that needs to be parsed and edited when an extension is added or removed.

Here is an example from an Ubuntu (Debian derivative) with a few extensions enabled in PHP (they go to /etc/php5/conf.d instead of /etc/php.d, but the same underlying idea).


-rw-r--r-- 1 root root 33 Aug 24 2008 apc.ini
-rw-r--r-- 1 root root 50 Jul 3 2008 gd.ini
-rw-r--r-- 1 root root 75 Sep 26 2009 local.ini
-rw-r--r-- 1 root root 56 Jul 3 2008 mysql.ini
-rw-r--r-- 1 root root 57 Jul 3 2008 mysqli.ini
-rw-r--r-- 1 root root 52 Jul 3 2008 pdo.ini
-rw-r--r-- 1 root root 60 Jul 3 2008 pdo_mysql.ini
-rw-r--r-- 1 root root 804 Aug 8 2009 xdebug.ini

Yeah, no big deal, hence the combative tone of your original comment was unwarranted.

Sorry, I was not able to successfully run APXS

I try to install APC following your instruccion but when copile the module I have this error

[root@host ~]# pecl install apc
downloading APC-3.0.19.tgz ...
Starting to download APC-3.0.19.tgz (115,735 bytes)
....................done: 115,735 bytes
47 source files, building
running: phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
1. Use apxs to set compile flags (if using APC with Apache)? : yes

1-1, 'all', 'abort', or Enter to continue:
building in /var/tmp/pear-build-root/APC-3.0.19
running: /var/tmp/APC/configure --with-apxs
checking for egrep... grep -E
checking for a sed that does not truncate output... //bin/sed
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking how to run the C preprocessor... gcc -E
checking for icc... no
checking whether gcc and cc understand -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-redhat-linux-gnu
checking host system type... x86_64-redhat-linux-gnu
checking target system type... x86_64-redhat-linux-gnu
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib64/php/modules
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... 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 /usr/sbin/apxs follows
cannot open /httpd/build/config_vars.mk: No such file or directory at /usr/sbin/apxs line 201.
configure: error: Aborting
ERROR: `/var/tmp/APC/configure --with-apxs' failed

My server run on
Centos5
PHP Version 5.2.9

Note that I have no error when execute this previous command:
yum install php-pear
yum install php-devel
yum install httpd-devel

I hope your help.

Regards

Adrian

It was simple. Very-very simple.

Had same error:
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 /var/tmp/pear-build-slezhuk/APC-3.0.19/n follows
/var/tmp/APC/configure: line 3457: /var/tmp/pear-build-slezhuk/APC-3.0.19/n: No such file or directory
configure: error: Aborting
ERROR: `/var/tmp/APC/configure --with-apxs=n' failed

The answer was(for me)... to answer on question "Use apxs to set compile flags (if using APC with Apache)? [yes]" not "y", but "yes". Yes, it matters. Killed 1,5 hours on this. Yes, i also added /usr/sbin to $PATH (it wasn't there for me). Hope it helps somebody!

Note these error 1. Perl is

Note these error

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/sbin/apxs follows
cannot open /httpd/build/config_vars.mk: No such file or directory at /usr/sbin/apxs line 201.
configure: error: Aborting
ERROR: `/var/tmp/APC/configure --with-apxs' failed

install php perl
yum install php-perl
and also install apxs

php-apc installation

How to install the PHP APC Extension:

There are two ways to install this extension:

A. Install from Source:

wget http://pecl.php.net/get/APC-3.0.14.tgz
gzip -d APC-3.0.14.tgz
tar -xf APC-3.0.14.tar
cd APC-3.0.14
locate php-config
it is in /usr/bin/php-cong
Create configuration files
Phpize
Compile APC
./configure –enable-apc –enable-apc-mmap –with-apxs –with-php-config=/usr/bin/php-cong
make
make install
Edit pico /etc/php.ini and add this
extension=”” apc.so”
Restart the apache

B. Installing from Yum
yum install php-apc
Go to this path /usr/share/doc/php-pecl-apc-3.0.14/apc.php and copy apc.php into your Document root for access from web
For changing the settings got to this patch
/etc/php.d/acp.ini
Setting parameter
; Enable apc extension module
extension = apc.so
; Options for the apc module
apc.enabled=1
apc.shm_segments=1
apc.optimization=0
apc.shm_size=32
apc.ttl=0
#apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/XXXXXX
apc.enable_cli=1
apc.cache_by_default=1

Fixed!

On CentOS 5.2 you need to do this to get APXS running:

yum install apr-devel.x86_64

Me too

Same for me - same message. APSX not available, also CentOS 5.2. Installed without.

Pecl may not be able to perform the installation

Due to pecl following an 8 MB restriction that disregards php.ini, pecl may bomb out with a fatal error if you attempt to install apc with it. You can solve this by using the command 'pear install pecl/acp' instead.

PECL memory limit change

This didn't work for me, but I found this post by Phase2 which tells you how to increase the memory PECL can use:
http://www.agileapproach.com/blog-entry/howto-install-pecl-apc-cache-centos-without-xampp

Got this working on Ubuntu 7.04 Feisty

I already had PEAR/PECL installed on a system with Apache2 and PHP5, running under Ubuntu 7.04 Feisty.


$ sudo su
# apt-get update
# apt-get install php5-dev
# apt-get install apache2-prefork-dev
# apt-get install apache-dev
# pecl install apc

answer yes when prompted


# echo "extension=apc.so" >> /etc/php5/apache2/php.ini
# /etc/init.d/apache2 stop
# apt-get remove apache2
# apt-get install apache2
# /etc/init.d/apache2 stop
# /etc/init.d/apache2 start
# exit
$

At least I hope this is working. I wish I knew how to test if the performance actually increased.

Trouble

The installing with your instructions goes just fine, but APC doesnt want to start for me. Apache / PHP gives the following error:

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/modules/apc.so' - /usr/lib/php/modules/apc.so: cannot open shared object file: Permission denied in Unknown on line 0

Any help would really be appreciated

Thanks, Khalid. I asked my

Thanks, Khalid.

I asked my host to do it for me and immediately from then onwards, all my drupal pages are showing an internal 500 server error.

Does APC have a problem with php running as suphp (like suexec)?

Thanks,
Venkat

Doesn't work

Actually I recall reading specifically that suexec is known not to work with APC. Perhaps this is because it is CGI (new process every time a request comes in). The only versions that work with APC and other accelerators are the mod_php (Apache) or Fast CGI.

In your case, suexec is a trade off (security vs. performance).
--
2bits -- Drupal consulting

well, it seems to work:-) My

well, it seems to work:-)

My host had simply given incorrect permissions on the temporary APC script. I don't know the intricacies of it, but they said setup would be slightly different on a cPanel VPS and enabled it.

Venkat

suphp

Thanks for the email.

Your site is running suphp (which is still CGI), and suhosin which some say is incompatible with op-code caches (not sure).

Please try with and without APC using devel and see if the pages execute faster with APC on suphp. I doubt they would.
--
2bits -- Drupal consulting

suggestion

Nice and simple tutorial.

One question:
Why APC, and not eAccelerator?
In some of your previous articles (benchmarking APC vs eAccelerator using drupal and PHP op code caches/accelerators: a must for a large site) you wrote that eAccelerator has better performance over APC.

Stability

eAccelerator is definitely better than APC for speed (faster) and memory utilization (uses less memory).

However, with newer versions of PHP and Drupal, APC now has the edge in stability. Previously, we had maybe one or two seg faults with eAccelerator per week, which was acceptable, and the logwatcher script restarted things automatically.

After upgrading, it was seg faulting every 2 hours. Once we switched to APC, it was stable and rock solid.

There is also xcache, which we experimented with, but not used in production work.

I plan to publish updated benchmarks of APC, eAccelerator and xcache in the near future.
--
2bits -- Drupal consulting

I have eAccelerator running.

I have eAccelerator running. Will it conflit if I install APC too?

APC for data cache or code?

If you are using both as code cache, then it is not a good idea. They both do the same thing. See which one is more stable and use it and uninstall the other.

If you are using APC as a data cache, e.g. for cache router module, then consider using memcache, which is designed for this type of work.

In certain cases (e.g. Apache is mod_php, single server), APC is a good data cache. You can then configure APC not to act as a code cache and use eAccelerator instead, or use APC for both and have less components to configure and maintain.

Not found apc.php

Hi all.

I have the problem that once installed APC on my server I can not monitor it because it can not find the file apc.php

Using Centos 6.3 and Magento 1.7.0.2, if someone has this happened and know where the file has been renamed or if you appreciate to help me, because I really do not know how far is working well.

The tutorial I followed is this: http://www.occentus.net/documentacion/item/instalacion-del-apc-cache-para-usuarios-de-plesk

and the file is not in / usr / share / php / apc.php

Greetings.

Is your Drupal or WordPress site slow?
Is it suffering from server resources shortages?
Is it experiencing outages?
Contact us for Drupal and WordPress Performance Optimization and Tuning Consulting