Chris Tankersley

· PHP Jack of All Trades ·

Speed up PHP with APC

A downside to any scripted language is that it is recompiled every time that it is run. CPU cycles are wasted compiling both simple and complicated scripts over and over, regardless if the actual output even changes. What can you do to stop this kind of waste? If you are using PHP, there are quite a few caching mechanisms. I spent most of today trying to get a few of them to work with OpenBSD 4.1 with very little success. One that I did get working was APC.

What is APC? APC literally stands for 'Another PHP Cache.' What it does is keep a compiled version of your PHP code on the server so that your web server does not waste time doing the same compile over and over. APC itself runs as a PHP module for PHP 4.3.x and higher. If you are running your own server, APC is very easy to install.

Installing APC These instructions are for OpenBSD 4.1, but should work for any flavor of *nix.

What you need installed:

  • autoconf

  • PHP 4.3.x or higher

  • GD library for PHP

  • PEAR Libraries for PHP

  • Apache 1.3 or higher

Head on over to http://pecl.php.net/package/APC and download the latest package to your server. Uncompress the file with 'tar -zxf APC-X.X.X.tgz' where the X's correspond to the version that you downloaded (3.0.14 as of today). Head into the new folder with 'cd APC-3.0.14/APC-3.0.14'. We will need to compile this module with the following commands:

phpize
./configure --enable-apc-mmap --with-apxs --with-php-config=/usr/local/php/bin/php-config
make
make install

If everything goes well it should install itself into /var/www/lib/php/modules/apc.so . Enable APC with the following addition to your /var/www/conf/php.ini file:

extension=apc.so
apc.enabled=1
apc.shm_segments=1
apc.shm_size=128
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1

Stop and start httpd and make sure that PHP files still work. If everything goes well, move the apc.php file from the downloaded files to your DocumentRoot. Edit the file and change the line:

defaults('ADMIN_PASSWORD','password');

and replace 'password' with whatever password you want to log in with. You can also change the line right above that to change the username that you will use to log in.

Using APC Visit some of your PHP pages, and then head over to the APC control page in your browser (for example, http://localhost/apc.php), log in, and look at the stats. APC keeps track what pages are being cached, what pages are being missed, how much memory it is taking up, etc. If you have GD installed it will draw graphs for instead of giving just text information. You can clear out the cache by clicking on the 'Clear opcode Cache' button, but other than that APC runs all by itself.

Does It Really Help? I used the 'ab' Apache Benchmarking tool from a machine on the local network of the webserver. I ran it with the following command:

ab -kc 50 -n 1000 http://gir/index.php

with and without APC installed. Overall I netted about 10 extra page returns per second with a 50ms decrease in overall response time. The numbers aren't huge, but over time those numbers add up. APC also did not cause any extra CPU load. APC and other PHP caching mechanisms aren't the only thing that can be done to speed up a website, but for those that have access to their web server if can hel


Comments

Categories: Servers

Tags: Performance, PHP