Speed up PHP with APC

The Alternative PHP Cache (APC) is a PECL package that provides an opcode cache for PHP. Rasmus Lerdorf is the lead of the project along with a handful of other developers. What is an opcode cache? Before I can answer that, let’s discuss how PHP works. Because it is an interpreted language, every time a page is loaded your PHP script must be converted to a language the machine can understand. This is known as operations code or opcode for short. By caching the opcode periodically, you gain a boost because your code is no longer being converted with every page load. This becomes more of a benefit when you have a high volume of traffic to your site.

Outside the topic of this post, APC can be used as a caching layer for your data as well as caching the opcode. I say that it’s outside this topic because I just want to focus on the fact that simply installing APC without any code changes will be of benefit. Personally speaking, I still use Memcached for caching data, but I’ve read some great things about using APC instead of Memcached as it’s faster. The downside would be that you lose out on the distributed nature of Memcached. This is of little issue if you’re running a single web server.

As mentioned, APC is a PECL package so can install it with pecl like this:

pecl install apc

Alternatively, if you are using Ubuntu you can install it from the repository:

sudo apt-get install php-apc

Once installed, restart your web server and/or FastCGI service and you can check to see if it installed correctly by checking phpinfo() as APC will now have it’s own section. That’s all there is to it to start reaping the benefits of the Alternative PHP Cache with your existing website or web application.

Fun fact, APC will be integrated with PHP 6.x (off by default). Down the road I’ll discuss using APC as a cache for your application data. Until then, enjoy your speedier site!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.