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 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 build-essential -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

sudo 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.0-fpm if you are using nginx or Apache with FPM.

sudo service php7.0-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

11 thoughts on “Install APCu Object Cache for PHP7 for WordPress on Ubuntu 16.04”

  1. I’ve installed this on two sites – both on their own VPSs in Digital Ocean, and in both cases we’ve had things disappear from the back end of wordpress – widgets, shortcodes, and other configuration type things.

    You wouldn’t think its related, but wp-lcache is the only thing common to these two sites.

    Ever heard of something like this?

    • Hi Simon, that is unfortunately quite common with object cache which sometimes can have these random side effects. It is usually a plugin or theme not using transients correctly, compatibility issue with the plugin or just a fault of the object-cache possible in WordPress

    • Unfortunately this does happen, although very rarely once a site has been setup and running for some time. It’s not usually a major issue though. As @blindpet:disqus indicates it’s usually a case of the data being held in cache but getting dropped before it gets written to the database.

      In my experience the best solution is to not turn on your object caching until the site has been fully developed and deployed. This way you a limiting the potential for crucial data to be lost along the long.

  2. Thanks for sharing @JdMnT:disqus but unfortunately ownCloud don’t seem to offer any evidence for their claim 🙁 so please take with a grain of salt

  3. Thank you! Very helpful!
    The option with build from Source code help me and didn’t take a lot of time.

  4. Hello thanks for article.
    You says: “apc.shm_size = “50M””
    but 32mb appears in the picture. I did 512mb too. I restarted my server. however, it still looks 32mb.?
    Regards

Comments are closed.