How to Install APC and Memcached on Ubuntu

Published June 28th, 2011


In my previous post I covered the installation of nginx, PHP-FPM and MySQL on Ubuntu. In this post I will cover the installation of APC and Memcached onto the server. As before I kept to the use of of packages located on the official Ubuntu repository in order to keep upgrading and maintainance headaches to a minimum. Also before these are the step I took on an Ubuntu 12.04 LTS (Precise Pangolin) installation. However you should be able to follow the same steps with any older or newer release of Ubuntu.

Note that this post is meant as a follow up to my previous post on the installation of nginx, PHP-FPM and MySQL on Ubuntu which can be found here. However these steps should work fine for installing APC and Memcached in an Apache environment as well. Just be sure to replace the /etc/init.d/php-fpm restart commands with /etc/init.d/apache2 restart.

This post covers the installation of both APC and Memcached although you may not necessarily need both being that APC is generally capable of doing what Memcached does in smaller environments but depending on your websites software and infrastructure you may need Memcached in place of APC or in conjunction with APC.

First off I will cover the installation of APC which is as simple as running the following command.

sudo apt-get install php-apc

After the package has been installed you will need to restart the php-fpm process.

sudo /etc/init.d/php5-fpm restart

Once the php-fpm process has been restarted we can varify our APC setup by running the following command. You may need to install the PHP command line interface.

Install the PHP command line interface:

sudo apt-get install php5-cli

Command to check if APC has been installed correctly:

php -r "phpinfo();" | grep apc

you should receive output resembling the following.

Additional .ini files parsed => /etc/php5/cli/conf.d/apc.ini,
apc
apc.cache_by_default => On => On
apc.canonicalize => On => On
apc.coredump_unmap => Off => Off
apc.enable_cli => Off => Off
apc.enabled => On => On
apc.file_md5 => Off => Off
apc.file_update_protection => 2 => 2
<... clipped to save room ...>

That is that you now have APC installed on your server.

Next we move on to the installation of Memcached. As with APC the installation process is very simple and straight forward. First we will install the Memcached package.

sudo apt-get install memcached

After the Memcached package has been installed we will need to start the Memcached process.

sudo /etc/init.d/memcached start

After starting Memcached we will need to install the Memcache or Memcached PHP module. A simple Google Search will help you learn the difrences between the two and help you decide on which best suits your needs.

Install php5-memcache:

sudo apt-get install php5-memcache

Or install php5-memcached

sudo apt-get install php5-memcached

Now restart your PHP-FPM process.

sudo /etc/init.d/php5-fpm restart

You now have both APC and Memcached installed and ready to be utilized to increase the performance of your server when dishing out your content. In my next post I will cover the setup and configuration of WordPress running on nginx along with the plug in W3 Total Cache which will allow WordPress to fully utilize APC and/or Memcached to really liven up your blogs performance.