Categories


APC Saves the day

And no, thats not APC as in American Power Conversion, thats APC as in Alternative PHP Cache. At the office, we host a fair number of high traffic websites, sites that routinely push a few gigs of traffic a day each. Obviously, thats a lot of web traffic, and we do have to keep our servers running fairly well. We also have a few, what we refer to as “seasonal” sites, where traffic for the sites spikes during certain parts of the year.

Well, this week, as expected, our traffic spiked and our servers were getting crushed under the load. While I was searching for a solution, I happened across the APC Opcode Cache for PHP. After reading through the documentation, I was fairly confident that it would help us out, as our main bottleneck wasn’t serving the pages, or processor usage, it was hard drive accesses. I also realized it was in the PECL library, which I figured would make the install process much easier. How wrong I was. Turns out, PECL is semi-broken on our Redhat 4 system and thus I had to download and compile the module from source.

Just to maintain a record, and for reference to anyone else out there trying to do this, I’ve outlined the steps that I had to take below to get the damn thing working.

First, I started with a simple command to install it, figuring that if it worked I wouldn’t have to do anything else. If this command works, then feel free to skip the rest of the steps below:

sudo pecl install apc

Okay, well that failed, after googling around, I determined that PECL was actually broken on my system, and since I didn’t really feel like, or have the time to fix it, I decided to just compile from source.

So, I grabbed the source and extracted it:

wget http://pecl.php.net/get/APC-3.0.14.tgz
tar -zxvf APC-3.0.14.tgz
cd APC-3.0.14

After flipping through the install manual, I determined that it should be a simple matter of configure and make install. I needed to know where php-config was on the system for this to work:

sudo locate php-config
./configure –enable-apc –with-php-config=/usr/bin/php-config

Well, that sure didn’t work, as there wasn’t a configure script in the folder (I really should’ve checked first). So, I googled around a bit more, and found out that I needed to run phpize, which would give me a configure script and let me go on my merry way.

sudo /usr/bin/phpize

Oops, I didn’t have autoconf or automake installed.

sudo up2date –install autoconf automake
sudo /usr/bin/phpize

Hmmm… I could’ve sworn I’ve compiled stuff on this system before. Looks like I’m missing libtool as well:

sudo up2date –install libtool libtool-libs

Okay, lets try that one more time.

sudo /usr/bin/phpize
sudo ./configure –enable-apc
sudo make install

Hurray! It installed and the module is where it needs to be. Now its time to do some simple configuration (since I know the defaults won’t work for this system).

sudo vim /etc/php.ini

Add the following to the extensions section:

extension=”apc.so”

At the end of the file, I added this to configure APC:

; APC Configuration
[apc]
apc.enabled = 1
;apc.shm_segments = 5
apc.shm_size = 50
apc.num_files_hint = 1000
apc.user_entries_hint = 4096
apc.ttl = 1800
apc.user_ttl = 3600
apc.gc_ttl = 3600
apc.cache_by_default = On
apc.file_update_protection = 10
apc.max_file_size = 1M
apc.stat = 1
apc.write_lock = 1
apc.report_autofilter = 0
apc.localcache = 0
apc.localcache.size = 512
apc.include_once_override = 1

I originally had the apc.shm_segments defined, but that was causing warnings and to save the log files from filling up with warnings, I just commented out the line. Obviously when you’re done all this, you’ll need to restart apache:

sudo apachectl configtest
sudo /etc/init.d/httpd restart

And you’re all done. APC comes with a nice php script (apc.php), which you should put in a secure place on your web server. When you go to it, you can see various bits of information about the cache as well as its current status. Our cache has now been running for almost 23 hours, has a hit rate of 99.9% and has served up some 4,052,066 hits. Thats 4 Million less reads from our hard drives! So, for anyone out there struggling with PHP performance issues, but wary of using a web cache, like squid. I would highly recommend you install and use APC, it will save you a lot of time and money in processor usage.


Your Comment