Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
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 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.)
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().