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.
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.
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.
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.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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!
According to the intro paragraph, above, this tutorial “assumes you … have LAMP stack installed…”
Does this mean that APC does not work w/a LEMP stack (or, at a minimum, that we should not utilize this tutorial if we have a LEMP stack)?
@Pablo: APC will work with a LEMP stack since it’s a php extension. Some paths/commands will be different though:
“sudo service apache2 restart” would be “sudo service nginx restart” “/etc/php5/apache2/php.ini” would be “/etc/php5/fpm/php.ini”
Other than that, everything should be the same.
On my LEMP stack, I also had to run “/etc/init.d/php5-fpm reload” before the change was recognized.
Check this comment on a similar article, http://curltable.the-yard.net/blog/install-apc/comment-page-1#comment-325 It says, “If you have a newer version of PHP use an M after the apc.shm_size=64”
Should i consider doing that?
Yes, you should.
Does php 5.4.x count as a newer version?
@KiwoT: I believe yes.
These instructions worked fine for me, thanks.
One small point. Instead of editing php.ini, I suggest creating a file
/etc/php5/conf.d/apc.ini
Put the extension = apc.so line and any other apc configuration in there. That’s really what the conf.d directory is for.
I used the one-click install of Wordpress on Ubuntu 12.10. I have been having site performance issues with slow First Byte Time, Caching, and Compress Images. I was recommended APC by the Wordpress/Buddypress site forum. Can I follow these directions above if I am on Ubuntu 12.10?
@hughdshields: Yes, this article should work fine on the Wordpress on Ubuntu 12.10 application image.