In previous guide, we installed APC cache on Ubuntu 18.04. Here Are The Steps On How To Install Memcached on Ubuntu Server 16.04, 18.04 And Configure WordPress To Optimize Page Loading Speed. APC is an opcode cache. APC cache chunks of PHP code and stores it in RAM. When we need to run that same piece of code again APC already cached it and runs from memory at lightning fast. An opcode cache is one that compiles the plain PHP code into machine code (opcode) and then stores it in the compiled form for future requests until it detects the original PHP file has changed. This means that PHP does not have to run this compile step on every single request, saving some time.
Memcache or Redis are in-memory data-store, used as a cache for your WordPress site data. Although both are cache system and sounds closer, they are technically not same. Everything really depends on your server. A cluster of small servers at lower cost can distribute load and make site load loading almost instantly. APC does not share the cache with other servers but possibly it is faster to cache code.
In short, memcached is a distributed caching system, whereas APC cache is a non-distributed cache and mainly an opcode cache. If your WordPress live on different webservers for loadbalancing then you have to use memcached for distributed caching. If not, you can stick to APC cache. You should always use asome opcode cache. You can/should use both of them for different purposes.
---
Steps of How To Install Memcached on Ubuntu Server
You should have installed Apache2, PHP on Ubuntu 16.04 or 18.04 in our described way. SSH as root. Run :
1 2 | apt update -y apt upgrade -y |
First install the dependencies, search apt with php72-devel
to find some php-devel
for your version of PHP :
1 | apt install make php72-devel gcc glibc-devel zlib-devel libmemcached-dev libmemcached11 git build-essential |
Install memcache :
1 2 | apt install memcached service memcached start |
Install PHP memcached (d means daemon) :
1 2 | pecl install memcached # sudo apt install php-memcached |
Add this line to php.ini
under the php version you installed memcached php (like :/etc/php/7.0/mods-available
for PHP 7.0) or under Apache’s php.ini
directory (like /etc/php/7.0/apache2/php.ini
) :
1 | extension=memcached.so |
If you have Apache2 running, then reload :
1 | service apache2 restart |
If you run :
1 | php -i | grep "memcached support" |
You’ll get :
1 | memcached support => enabled |
Run :
1 | echo "stats settings" | nc localhost 11211 |
11211
is the default port where memcached runs. Run :
1 | php -r 'phpinfo();' | grep -i 'memcached' |
You’ll get output like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /etc/php/7.2/cli/conf.d/20-memcached.ini, memcached memcached support => enabled libmemcached version => 1.0.18 memcached.compression_factor => 1.3 => 1.3 memcached.compression_threshold => 2000 => 2000 memcached.compression_type => fastlz => fastlz memcached.serializer => php => php memcached.sess_binary => 0 => 0 memcached.sess_connect_timeout => 1000 => 1000 memcached.sess_consistent_hash => 0 => 0 memcached.sess_lock_expire => 0 => 0 memcached.sess_lock_max_wait => 0 => 0 memcached.sess_lock_wait => 150000 => 150000 memcached.sess_locking => 1 => 1 memcached.sess_number_of_replicas => 0 => 0 memcached.sess_prefix => memc.sess.key. => memc.sess.key. memcached.sess_randomize_replica_read => 0 => 0 memcached.sess_remove_failed => 0 => 0 memcached.sess_sasl_password => no value => no value memcached.sess_sasl_username => no value => no value memcached.store_retry_count => 2 => 2 memcached.use_sasl => 0 => 0 Registered save handlers => files user memcache memcached |
So the effective settings file is /etc/php/7.2/cli/conf.d/20-memcached.ini
. If you run just :
1 | php -i |
You’ll receive information about memcache and memcached.
How to configure WordPress with memcached has been discussed in the article WordPress High Load Average With W3 Total Cache.
If you are not using W3 Total Cache, you just need to install WordPress plugin like Memcached Redux, instead of activating it, copy the object-cache.php
from plugin directory to wp-content
directory.