How to Install APC (Alternative PHP Cache) For Drupal 7

Following up from my last article, this is going to be a shorter article that deals directly with installing APC (Alternative PHP Cache) for Drupal 7. As the name implies, APC is a caching system for PHP that can dramatically increase your sites performance. In order to complete this process you’ll need to have SSH access to your server and feel comfortable working with Linux commands. It’s also worth noting that I’m working within Ubuntu 10.04.4 LTS (Lucid). If you’re working on a different Linux distro these command lines might not work for you.

Before we begin I think it’s a good idea to set a performance benchmark to gain a better sense of the effect APC will have on site performance. One way to do this is use the Apache Benchmark (ab) tool (substituting your own website of course):

$ ab -n 10 -c 5 http://www.somewhere.com/

This will hit the server with 10 requests with a maximum of 5 requests running concurrently. Repeat the test a couple times then average out your results to get a sense of where your performance stands before we install APC. These were my results:

Concurrency Level:  5
Time taken for tests:  0.740 seconds
Complete requests:  10
Failed requests:  0
Write errors:  0
Total transferred:  116120 bytes
HTML transferred:  111650 bytes
Requests per second:  4.52 [#/sec] (mean)
Time per request:  669.862 [ms] (mean)
Time per request:  173.972 [ms] (mean, across all concurrent requests)
Transfer rate:  153.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      104  296 136.6    647     756
Processing:     0   90 143.1      2     698
Waiting:        0   60  96.9      0     401
Total:        636  686  36.7    702     758

Percentage of the requests served within a certain time (ms)
  50%    602
  66%    602
  75%    602
  80%    603
  90%    758
  95%    758
  98%    758
  99%    758
 100%    758 (longest request)

Now let’s see how much APC will help us.

Step 1:

First we have to install PECL and the latest PHP-dev packages. You might already have these packages installed, but it doesn’t hurt to run them again to make sure.

$ sudo aptitude install php-pear
$ sudo aptitude install php5-dev
$ sudo aptitude install apache2-dev

Step 2:

That covers our dependencies. Now let’s install APC:

$ sudo pecl install apc

Step 3:

To enable in APC we need to edit the php.ini configuration file:

$ sudo nano /etc/php5/apache2/php.ini
# Add this to the bottom of the file:
extension=apc.so
apc.enabled = 1
apc.shm_size = 48

Once we restart Apache APC will be enabled with a memory limit of 48MB. This should be enough for most Drupal installs, but if you have a lot of Drupal modules enabled you may want to come back and increase this number later. If there is not enough memory in the APC cache you may end up hurting your performance instead of enhancing it.

Restart Apache to finish the installation:

$ sudo /etc/init.d/apache2 restart

And that’s it! APC is now installed and running.

Bonus Step:

This last step isn’t necessary for APC to run correctly, but it is useful to monitor the effect APC cache is having. There is an apc.php file located here in your system root (/usr/share/doc/php-apc/apc.php.gz.) which you can unpack and move to the root folder of your Drupal installation. You can now navigate to domain.com/apc.php to see if APC is working correctly and how well it’s performing.

APC Graph

Now let’s run ab again to see what kind of performance effect APC has had:

Concurrency Level:  5
Time taken for tests:  0.790 seconds
Complete requests:  10
Failed requests:  0
Write errors:  0
Total transferred:  135560 bytes
HTML transferred:  131090 bytes
Requests per second:  12.65 [#/sec] (mean)
Time per request:  395.170 [ms] (mean)
Time per request:  79.034 [ms] (mean, across all concurrent requests)
Transfer rate:  167.50 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       89  266 152.8    383     399
Processing:     0  121 149.5     11     300
Waiting:        0   68  88.3      0     172
Total:        357  387  14.5    390     401

Percentage of the requests served within a certain time (ms)
  50%    390
  66%    392
  75%    399
  80%    400
  90%    401
  95%    401
  98%    401
  99%    401
 100%    401 (longest request)

Not bad, but let’s get things really humming by installing Memcached in my next article.

UPDATE:

I’ve been playing around with different APC settings to see what works best (in particular, setting apc.stat and apc.ttl = 0. This should only be done in a production setting, but can dramatically improve the performance of the APC cache).

Right now I’m running APC 3.1.9, which is the current stable release of the software, but if you need to upgrade just run these commands:

$ sudo apt-get install libpcre3-dev
$ sudo pecl upgrade APC

Resources