Installing Alternative PHP Cache (APC)

Enter Your Query:
Use '%' for wildcards and quotes for "exact phrases"

Top Level » CustomBuild » ./build php
Top Level » Apache Related Information » Php

Installing Alternative PHP Cache (APC)Last Modified: Dec 16, 2014, 5:27 pm
Where possible, use OpCache instead of APC:

If  you're using PHP-FPM, see the forum.
For FastCGI (mod_fcgi), use this guide, but see below for exceptions/changes.

A common optimization that can be done for php to improve performance is to install Alternative PHP Cache (APC).
What it does, is it caches php files in memory in their parsed and compiled state.
This removes the need for re-reading, parsing, and compiling for each request, thus greatly lowering your system load.
The cost of using this mosulde is increased memory usage, since that's where the cache lives.

Note - This won't work with suPhp, since each call to php is with a seperate binary call, and doesn't share the same memory.  CLI is always running in memory, hence can shared the same memory, which is why it works.

1) To install it with the default CLI version of php, use the following:

cd /usr/local/directadmin/custombuild
tar xvzf APC-3.1.13.tgz
cd APC-3.1.13
./configure --with-php-config=/usr/local/bin/php-config

make install

2) Then, we needed to edit the php.ini to add the code:

Replace the 1000M with the amount of ram you want it to use. About 40M per WordPress install, and 60M per Magento. Multiply that by the number of installs. 100 WordPress installs = 4000M.

If your server does not have enough ram to cache all PHP files, you must reduce the number of cached files. Use filters option or play with cache_by_defaut option to specify wich files to be cached. A tip is to set apc.cache_by_default=0 in php.ini, and then apc.cache_by_default=1 in the .htaccess of websites requiring APC optimisation.

3) as well as change the php.ini line from:

extension_dir = "./"

to be:

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/"

but, this may vary per system.   The "make install" command will tell you where the was installed to (Installing shared extensions:).

Note that this is a 3rd party module, and would be beyond our technical support for any issues that arise.

APC provide a tool to check memory use. It's a php page that you will find in /usr/local/directadmin/custombuild/APC-3.1.14/apc.php. Copy it to a web folder and open it in browser. In a correctly configured APC installation memory chart must remains stable over time.

Note: It's been observed that with APC enabled, php may not install on the "make install" step.  The error it produces is as follows:

Fatal error: Class 'PEAR' not found in phar://install-pear-nozlib.phar/index.php on line 24
make[1]: *** [install-pear-installer] Error 255
make: *** [install-pear] Error 2

After removing the APC options from the php.ini, it installed.  The APC could be re-enabled.

FastCGI mod_fgci.c

The install is mostly the same, but some paths and options vary:

1) The phpize and configure lines will be different, eg, if you're using 5.4 with FastCGI:

./configure --with-php-config=/usr/local/php54/bin/php-config54

2/3) In our tests, we used these options in the php.ini

extension_dir = "/usr/local/php54/lib/php/extensions/no-debug-non-zts-20100525/"

and left everything else as default.  Note that the extension_dir can vary in path and in ID (2010525) depending on your verion, type and compile instance of php.
The "make install" part of APC should spit out what this path should be.
Related Helpfiles
Installing php OpCache with CustomBuild 2.0

© 2018 JBMC Software, Suite 173  3-11 Bellerose Drive, St Albert, AB  T8N 1P7  Canada.  Mon-Fri 9AM-5PM MST