The previous “Tech Support” column (http://www.linux-mag.com/content/view/2586/)
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 http://pecl.php.net/package/APC
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:
$ ./configure ––enable-apc \
# 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 apc.so 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().
Jeremy Garcia is the founder and administrator of the recently redesigned LinuxQuestions.org, a free, friendly, and active Linux community. Please send questions and feedback to
Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62