x
Loading
 Loading
Featured Paper: Xen Virtualization with Novell SUSE Linux
Hello, Guest | Login | Register

Optimizing PHP Install, Part Two

Learn how to install and configure the Alternative PHP Cache

Community Tools
RSS
Recommend This [?]
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Users That Liked This [?]
No one yet. Be the first.
Tags:
Tag This!
 No Comments
The May “Tech Support” column (http://www.linux-mag.com/2006-05/tech_01.html, available after July 15) 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:
$ phpize
$ ./configure ––enable-apc
––enable-apc-mmap
––with-php-config=/usr/local/bin/php-config
––with-apxs=/usr/local/apache2/bin/apxs
$ 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 apc.so now installed, it’s time to enable APC. Add the following lines to your php.ini file:
extension=apc.so
apc.enabled=1
apc.shm_segments=1
apc.optimization=0
apc.shm_size=128
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
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 (http://www.php-accelerator.co.uk/) and Turck MMCache (http://turck-mmcache.sourceforge.net/). Zend also makes a closed-source caching product, available as part of the Zend Platform. For more information see http://www.zend.com.

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 class="emailaddress">jeremy@linuxquestions.org.

Jeremy Garcia is the founder and administrator of LinuxQuestions.org, a free, friendly, and active Linux community that filters its email with SpamAssassin. Please send questions and feedback to jeremy@linuxquestions.org.

Read More
  1. Liberty Alliance Announces March 10 Santa Clara Identity Management Implementation Workshop
  2. Centrify DirectControl 4 Wins the 2008 Global Excellence Award for Best Identity Management Software
  3. SANRAD Achieves Record Sales in Q4 2007 for iSCSI Solutions Optimized for Server Virtualization and Disaster Recovery
  4. iJET Enhances Worldcue(R) Global Control Center with Employee Threat Exposure Dashboard
  5. Bloombase Technologies Accelerates Enterprise Information Security With Hifn Data Security Card

Comments on Optimizing PHP Install, Part Two

No comments yet.

Sorry, the comment form is closed at this time.

ActivSupport
Linux Magazine has chosen ActivSupport as IT consultants.
Sponsored Links