Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
Linux

APC (Alternative PHP Cache) on Debian Etch

This is one of the best packages you can incorporate into your web development. Basically APC allows you to cache data in memory for extremely fast data access. We are exploring writing an application at work the must have extremely fast access to data, so fast that we are willing to dedicate a server with lots of RAM to this application. Are other alternative is to store the data on XML files with small yet very fast disks that spin at high RPMs, with lots of onboard disk cache, most likely a SAS or SCSI disk. This would allow for fairly quick access as the disks would spin fast and be small so seek time etc…

Honestly both servers would come out to within a few hundred dollars of each other. So the memory-based solution utilizing APC in PHP is probably the best. So tonight I installed APC on my home server, a dual core xeon 2.6 Ghz server with 3 GB of RAM, and 4 SCSI disks in RAID 5, with the OS stored on 2 18 GB disks in a mirror. Yes I am a fucking geek. Would’ve like to have done this on my beta server (geek again), but it only has 1 GB of memory and I intend on storing that much data in memory :)

So here is my experience and how to with APC

First I discovered that I couldn’t install APC easily. I ran the following command:

pecl install apc

And received an error regarding phpize. This just meant I had to install the php5-dev package. Of course when I did that it wanted to remove a bunch of packages I love so dearly, specifically apache-mod-log-sql and it wanted to downgrade my install of the dovecot mail server to a release candidate. This I think is because I install the mod-log-sql from the unstable sid repository. So I installed php5-dev from testing rather than stable, this time it wanted to remove php5-curl. I don’t use it, but that still sucked. I did it anyways. Tactful like I am I then just re-installed php5-curl and aptitude didn’t bark. Go me.

Now I try running pecl install apc again and it barks I don’t have perl installed. So I install perl. The saga continues barking about this: Apache was not compiled with DSO support (–enable-module=so); So I aptitude install apache2-threaded-dev. I swear linux can be a real bitch sometimes, it then complains that make is not found. Arggh, thats why I use debian, to avoid make. So I aptitude install make. It finally begins running and the wild compiling begins reminding me of my days using FreeBSD and SlackWare.

I then add the following two lines to my php.ini file:


extension=apc.so
apc.enabled = 1

I restart apache and get this warning: “Restarting web server: apache2[Tue Mar 25 23:02:05 2008] [warn] The Alias directive in /etc/apache2/apache2.conf at line 240 will probably never match because it overlaps an earlier Alias.”

It’s nothing important for me just some alias to an icons directory that I think apache installs by default so I just comment it out, not that the warning message will affect the operation of apache, but I don’t want to see it every time I restart apache.

Here is more information on installing APC from php.Net. I also got some helpful tidbits from this persons blog.

So now I have all these additional options for my PHP.INI file specifically for APC (see php.net entry on APC) and I’m not going to touch them unless needed.

Now unfortunately the roughest inner joining query I could write with the data on my server only took .03 seconds to execute. But it does return 18,000+ plus records. So i realize I am going to have to run a query that basically sacks my server,a query which has virtually no purpose, a query that inner joins, left joins, and whatever joins everything possible. Finally, awesomeness, the query takes 50 seconds. returning 20,000+ rows of data. So here’s how I throw it into memory permanently.


apc_add('test_apc', $array);
print_r(apc_fetch('test_apc'));

So I just looped through my mysqli result using mysqli_fetch_assoc and push the data into an array. Mind you this took 53 seconds. I then call apc_add, give it an apc key name which I call test_apc, then toss in the data represented by $array. I then call it using the apc_fetch() function and can access the data within milliseconds :)

Related posts:

  1. upgraded debian sarge to etch + 2.6 kernel
  2. linux debian apache2 installing mod-write
  3. rsync debian linux
  4. setup an easy ftp server on debian 3.1 linux
  5. setting up ssl for apache2 on debian linux

Tags: ,

Leave a Reply