Install APCu Object Cache for PHP7 for WordPress on Ubuntu 16.04

WordPress object cache is a common way to speed up your site. PHP-APCu on Ubuntu 16.04 can help achieve your goal of supreme WordPress or WooCommerce speed.

This guide will show you how to install APCu Object Cache for PHP7 for WordPress on Ubuntu 16.04. There are three installation methods outlined here to best suit your needs. Usually installing from the repository or using PEAR is recommended, for bleeding edge users installing APCu from source it shown as well.

Install APCu Object Cache for PHP7 on WordPress on Ubuntu 16.04

Here are 3 ways to install the APCu object cache for WordPress or WooCommerce.

You should only choose one installation method.

  • Install APCu object cache for WordPress with one of the following methods
    • Install using repository
    • Install using PHP Pear
    • Build PHP-APCu from source
  • Then install the APCu WordPress object cache plugin

Install APCu Object Cache via Repository

Using this method you will not always get the latest version, it is however the easiest method.

sudo apt-get update
sudo apt-get install php7.0-apcu -y

Restart your php7.0-fpm service if you are using nginx

sudo service php7.0-fpm restart

Reload Apache if you are using it.

sudo service apache2 reload

Install APCu Using php PEAR

Another way to install is using php-pear, install it first

sudo apt-get update
sudo apt-get install php-pear -y

Running this command will install APCu.

sudo pecl install apcu

Add the extension to a custom ini file.

echo "extension = apcu.so" | sudo tee -a /etc/php/7.0/mods-available/apcu.ini

Since I use php7.0-fpm with nginx I am symlinking into the php7.0 fpm and cli folders.

sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/fpm/conf.d/30-apcu.ini
sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/cli/conf.d/30-apcu.ini

Restart php7-fpm

sudo php7.0-fpm restart

Apache2 users will want to symlink this file

sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/apache2/conf.d/30-apcu.ini

Apache users reload the Apache service

sudo service apache2 reload

Install APCu from Source Manually

The last way to install php7-apcu is to build from source. This way you get the bleeding edge version.

We need to install the php 7 development package and git first.

sudo apt-get update
sudo apt-get install php7.0-dev git -y

Enter your /tmp folder and clone the latest APCu source code from the git repository.

cd /tmp
git clone https://github.com/krakjoe/apcu

Now build the php7.0-apcu extension and install it.

cd apcu
phpize
./configure
make
sudo make install

Add the extension to the apcu.ini file

echo "extension = apcu.so" | sudo tee -a /etc/php/7.0/mods-available/apcu.ini

Since I use php7.0-fpm with nginx I am symlinking into the php-7.0 fpm and cli folders.

sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/fpm/conf.d/30-apcu.ini
sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/cli/conf.d/30-apcu.ini

Restart php7.0-fpm service

sudo php7.0-fpm restart

Apache2 users will want to symlink this file

sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/apache2/conf.d/30-apcu.ini

Reload the Apache service

sudo service apache2 reload

Install the APCu object cache plugin

We are going to install the LCache Plugin hosted on github sponsored by Pantheon.

If you do not have WP-CLI or shell access to your host then you have to create the object-cache.php file manually with these instructions.

cd /var/www/guides.wp-bullet.com
sudo -u www-data wp plugin install wp-lcache --activate && wp lcache enable

You will see this output

Success: Enabled WP LCache by creating wp-content/object-cache.php stub file.

Now you can optionally adjust the RAM for APCu and monitor the APCu cache

Adjust PHP-APCu RAM

Open the apcu.ini configuration file

nano /etc/php/7.0/mods-available/apcu.ini

Add the apc.shm_size line after the extension line. Here the APCu allocated ram is being changed to 50 MB.

extension=apcu.so
apc.shm_size = "50M"

Ctrl+X, Y and Enter to Save

Restart php7-fpm if ou are using it

sudo service php7-fpm restart

Reload Apache if that is your web server of choice

sudo service apache2 reload

Monitoring APCu Cache

Download the apc.php script

cd /var/www/guides.wp-bullet.com
wget https://raw.githubusercontent.com/krakjoe/apcu/master/apc.php

Now you can open yourdomain.com/apc.php and see the amount of cached objects, hit and miss rate.

php7-apcu-monitoring-wordpress-object-cache

Sources

Using PHP7 with APCu and W3 Total Cache
Krakjoe APCu Monitoring
Control Panel for APCu