Review your favorite Linux distribution.
Go Back > Articles > Jeremy's Magazine Articles
User Name


By jeremy at 2006-07-24 18:31
Optimizing PHP, Part Two
Features - Power Tools
Written by Jeremy Garcia

The previous “Tech Support” column showed you how to utilize memcached, a high-performance, distributed memory object caching system, to alleviate database load and speed up your PHP applications. In this months column, let’s move down a little in the stack and look at a way that to serve your existing PHP code faster.
PHP is an interpreted language, which means the Zend Engine must parse your PHP source code into byte code every time the script is run. While interpretation offers you flexibility and convenience as compared to a compiled language, the process of producing byte code again and again is wasteful and time-consuming.

Enter the Alternative PHP Cache (APC), a free, open, and robust framework for caching and optimizing PHP intermediate code. A major part of the APC framework is a cache that allows PHP to generate the opcode the first time a script is run and then use the cached copy on subsequent executions. APC, which is licensed under the same license as PHP, is available for download at

You have two options when installing APC. The first is to install it directly from PECL. That’s as easy as running pecl install APC as root. However, this can be problematic in some situations, and you may prefer to perform the installation manually. To do that, unpack the APC tarball and run the following commands:

$ phpize
$ ./configure ––enable-apc \
  ––enable-apc-mmap \
  ––with-php-config=/usr/local/bin/php-config \
$ make
# make install
As always, you may need to adjust some of the paths to point to your copy of axps and your php-config. Additionally, if you’re using PHP 4, add the Apache include directory to CPPFLAGS and specify that you’re running PHP 4. (If you’re using PHP5 you can skip this step, it’s done automatically.)

$ export CPPFLAGS="–I/usr/local/apache2/include –DAPC_PHP4_STAT"
With the now installed, it’s time to enable APC. Add the following lines to your php.ini file:

After a Web server restart, APC should be enabled. You can confirm that by running phpinfo().
By default, APC performs a stat() on each file before it serves the corresponding code from the opcode cache. If APC detects that a file has changed since it filled the cache, it reloads the file and freshens the cache. If you need every last bit of performance out of your setup, you can skip this check by adding apc.stat=0 to your php.ini file. The downside is that you’ll need to flush the APC cache or restart your Web server after any PHP file changes.

With APC now installed and caching files, it would be useful to know exactly what your benefits you’re getting. Luckily, APC ships with a script to do this. Copy the file apc.php to your web tree. However, before this script will work, you must open it and change the ADMIN_PASSWORD parameter. Once you’ve done that, point your web browser to apc.php. This script yields a ton of information including APC/PHP version information, runtime settings, diagrams with both memory usage and cache hits& misses, along with the ability to see exactly what files are cached. With APC installed and working properly, you should notice a performance increase in almost all of your PHP scripts, without having to do any additional work.

While the main component of APC is the opcode cache, it also includes a mechanism for caching arbitrary user supplied data. This comes in extremely handy for storing large arrays. While this requires some manual recoding of your application, you’ll often find it’s well worth the trouble. For more information, look into the apc_store/apc_fetch mechanism. To boost performance even more, recode parts of your application to take advantage of apc_store() and apc_fetch().

APC is just one opcoce cache. Two other free, open source options are the PHP Acceleraor ( and Turck MMCache ( Zend also makes a closed-source caching product, available as part of the Zend Platform. For more information see


All times are GMT -5. The time now is 12:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration