Tutorial

How To Install Alternative PHP Cache (APC) on a Cloud Server Running Ubuntu 12.04

Published on June 29, 2013
How To Install Alternative PHP Cache (APC) on a Cloud Server Running Ubuntu 12.04

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About APC

APC is a great operation code caching system for PHP that can help speed up your site. PHP is a dynamic server-side scripting language that needs to be parsed, compiled and executed by the server with every page request. In many cases though, the requests produce exactly the same results which means that the cloud server has to unnecessarily repeat all these steps for each of them.

This is where APC comes into play. What it does is save the PHP opcode (operation code) in the RAM memory and if requested again, executes it from there. In essence, it bypasses the parsing and compiling steps and minimizes some unnecessary loads on the cloud server.

This tutorial will show you how to install and configure APC. It assumes you are already running your own VPS with root privileges and have LAMP stack installed on it. If you need help with getting you going on those, you can read this tutorial.

Installing APC

To install APC, you first need to take care of a couple of dependencies. Install the these packages with the following command:

sudo apt-get install php-pear php5-dev make libpcre3-dev

Next up, you can install APC using the pecl command:

sudo pecl install apc

You will be asked a number of questions but unless you know exactly what you are enabling, go with the defaults by hitting Enter.

The next and final step of the installation is also mentioned in the terminal window. You need to edit the php.ini file and add a line at the end. Open and edit the file:

sudo nano /etc/php5/apache2/php.ini

Add the following line to the bottom of it:

extension = apc.so

Save, exit the file, and restart Apache:

sudo service apache2 restart

To see if APC is now enabled, you can check on the PHP info page. If you don't have one, you can create an empty php file in your /var/www folder:

nano /var/www/info.php

And paste in the following code:

<?php
phpinfo();
?>

Save, exit, and open that file in the browser. There you will find all sorts of information regarding the PHP installed on your cloud server, and if APC is enabled, it should show up there. It's probably not a good idea to leave that file there in production, so make sure you delete it after you are done checking.

Configuring APC

You now have installed APC and it's running with the default options. There are at least two main configuration settings that you should know about. First, reopen the php.ini file you edited earlier:

sudo nano /etc/php5/apache2/php.ini

Below the line you pasted to enable APC, paste the following line:

apc.shm_size = 64

This will allocate 64MB from the RAM to APC for its caching purposes. Depending on your VPS's requirements but also limitations, you can increase or decrease this number.

Another line that you can paste below is the following:

apc.stat = 0

The apc.stat setting checks the script on each request to see if it was modified. If it has been modified, it will recompile it and cache the new version. This is the default behavior that comes with every APC installation. Setting it to 0 will tell APC not to check for changes in the script. It improves performance but it also means that if there are changes to the PHP script, they will not be reflected until the cloud server is restarted. Therefore setting it to 0 is only recommended on production sites where you are certain this is something you want.

Now that APC is up and running, there is a nifty little page you can use to check its status and performance. You can locate an apc.php file in the /usr/share/php/ folder. You have to move this file somewhere accessible from the browser - let's say the www folder:

cp /usr/share/php/apc.php /var/www

Now navigate to that file in the browser:

http://<IP_Address>/apc.php

You'll get some interesting statistics about APC. What you need to pay attention to is that APC has enough memory to store its information and that there is not too much fragmentation.

Additionally, a good indicator that APC it's doing its job is that the Hits rate is significantly higher than the Misses rate; the first should be somewhere over 95% after a few requests already.

Conclusion

APC is a very easy to install and manage caching system for your sites hosted on cloud servers. If you want to continue improving site performance, you can look into installing Memcache and even installing Varnish for an even greater performance.

Article Submitted by: Danny

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Easy way to install sudo apt-get install php5-apcu

Then restart Apache sudo service apache2 restart

Hello,

i was working on one of my client’s project https://www.scislides.com which loads up very very slowly. I was going through the possible fix and came across below one but not sure whether this would work or not. Can somebody will help?

How to fix - Page Cache Test (Server Side Caching) In order to pass this test you are advised to use a caching mechanism for your pages. There are three methods which can be used to caching your web pages: Alternative PHP caching

  • Alternative PHP Cache (APC) is an open source framework which caches data using intermediate PHP code. Most web programmers who are familiar with the PHP programming language can easily set up Alternative PHP Cache for your site. Quickcache
  • Quickcache is a lightweight page caching solution which was formerly known as jpcache. Quickcache caches the page output rather than compiling the PHP page, making it a superior version of page caching to the Alternative PHP caching. Quickcache can be quickly downloaded from their website and can reduce your page load time up to 80%. WP Super Cache
  • If you have a Wordpress website, WP Super Cache can be installed within seconds and without no programming knowledge.

Regards Leo warner

cp /usr/share/php/apc.php /var/www

returns “No such file or directory”

$ sudo install pecl acp give this error…

make: *** [apc_compile.lo] Error 1 ERROR: `make’ failed

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
May 30, 2014

– I have tried to install the apc as suggested, but it gave following error,

/tmp/pear/temp/APC/apc_compile.c:47:25: note: in definition of macro ‘CHECK’ #define CHECK(p) { if ((p) == NULL) return NULL; } ^ make: *** [apc_compile.lo] Error 1 ERROR: `make’ failed

– I looked all around for the solution and something like this : http://bugs.php.net/bug.php?id=65731

[2013-09-21 14:38 UTC] rasmus@php.net -Status: Open +Status: Suspended [2013-09-21 14:38 UTC] rasmus@php.net APC doesn’t support PHP 5.5. PHP 5.5 comes bundled with an opcode cache now.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
May 1, 2014

@Gerardo: How did you install APC? It sounds like there is an htaccess file that is set up to restrict access to the APC page. That page allows you to clear the cache, so often people password protect it.

Hi,

I have configured a server with Drupal and installed APC. It works fine, but when I tried to access to apc.php in order to see stats, I get an HTTP Auth dialog asking me for introducing user and password. The apc.php file is located at /sites/all/libraries/apc_admin/{privatekey}/apc.php

I have tried moving the file to /var/www and it works fine.

Anybody knows what is happening?

Thanks

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
April 21, 2014

@terence.milbourn: APC is now outdated. This article won’t work if you’re not on Ubuntu 12.04. PHP comes with its own OpCode caching extension now: <a href=“http://nl1.php.net/manual/en/book.opcache.php”>http://nl1.php.net/manual/en/book.opcache.php</a>.

After <pre>sudo apt-get install php-pear php5-dev make libpcre3-dev</pre> I got the following “make” error…

<pre>make: *** [apc_compile.lo] Error 1 ERROR: `make’ failed root@qloudpress:~#</pre>

Anyone any ideas?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more