Recommended VPS Complete list of best VPS hosting providers.

How to Install and Use Memcached with Nginx, PHP 7 on Ubuntu 16.04

We have several articles about Varnish, a popular technology to cache your website in server’s memory / RAM and therefore it can boost the speed of your website and enlighten your server load. Today, I will show you another in-memory caching technology which is earlier and more popular than Varnish Cache. Meet Memcached! In sort, it is a distributed in-memory caching system that stores key-value pairs in your server’s RAM. The purpose of using this technology is still the same: to turbo boost your site performance as well as reducing server heavy load by avoiding frequent queries to database. Memcached (pronounced: Mem-cash-dee), or Memcache-daemon, is a process that listens by default on port 11211.

Installing memcached will obviously be helpful for websites having tremendous traffic. Many users keep wondering how they can build a perfect server to host their websites having glorious numbers of daily visitors and unique views without having to spend hundreds dollars a month renting a dedicated server. Luckily, using content caching mechanism to certain extent can be so useful and capable to deal with such situation. Many CMS apps like WordPress have plugins like W3TC, WP Super Cache, and so on. Sometimes it can be also handy. However, script-based caching is not the most sustainable method. Hence, some people opt to combine script-based and server-side caching to come up with more sustainable (and might be unbeatable) server performance.

Again, in this guide you will see all necessary steps which are easy to install Memcached on Ubuntu 16.04 server and configure it to work side-by-side with Nginx, PHP7 (php7-fpm) and MySQL.

Requirements

  1. A SSH client like Bitvise or Putty and basic knowledge about how to use it.
  2. Basic knowledge about common Unix command to SSH to your server.
  3. A server or VPS with at least 256MB of RAM (OpenVZ) but 512MB is recommended if you are using KVM or Xen.
  4. Ubuntu 16.04 either 32-bit or 64-bit.
  5. About 15 minutes of your time.
  6. a cup of coffee or tea.

Confused on choosing which VPS provider to setup an Apache based hosting server? Read my recommended VPS providers or this list of top low end cloud server providers. Quick recommendation: Digital Ocean, Atlantic.net or Ramnode.

In this guide I am using a 512MB RAM Cloud VPS from Atlantic running Ubuntu 16.04 x64. Unfortunately, Atlantic has shut down its 512MB plan so I’ve been so lucky to keep it.

Do not forget to always replace domain.tld with your own domain name and replace x.x.x.x with your server IP address.

PREREQUISITES

  1. Provision a cloud server on Atlantic.net (if you want).
  2. Install full LEMP stack (latest Nginx, PHP7, and MySQL) on your Ubuntu 16.04 server.
  3. Setup new website on your LEMP server (create Server Blocks).
  4. Install WordPress on LEMP server if you want to (optional).

Let’s Install Memcached

Step 1 – Login to your server as root or as a user with root privilege (sudo).

login ubuntu 1604

Step 2 – Update your server apt at first. It’s optional but it’s a common practice.

apt-get update -y

Step 3 – Next, assuming you have installed full LEMP stack and you have set up Nginx Server Block for your website, you can now simply install memcached directly by typing this command :

apt install memcached

apt install memcached ubuntu 1604

Step 4 – You will also need to install required php extension for memcached :

apt install php-memcached

install php memcached extension

Step 5 – Now make sure memcache daemon is running on your server by issuing following command :

ps aux | grep memcached

You should see something like this :

memcached daemon running

Step 6 – Also check whether memcached extension is properly configured in PHP7 by creating info.php page. Assuming your website is servermom.xyz, as described in my previous guide, so the command will be

nano /etc/var/www/servermom.xyz/html/info.php

and put this simple code in there :

<?php
phpinfo();
?>

Save that file and then exit Nano editor by pressing Control+O and Control+X respectively.

php info file tes

Step 7 – Now restart both memcached and php7-fpm services :

service memcached restart
service php7.0-fpm restart

restart memcached daemon

Step 8 – Now launch up your favorite web browser and access the newly created info.php file and check whether memcached extension is fully loaded.

check memcahced extension

memcached extension check in browser

Test Memcached Daemon

Step 1 – Create a cache_test.php file. This will perform a simple object test on your server ensuring Memcached daemon is working properly.

nano /var/www/servermom.xyz/html/cache_test.php

and put this scripts in there:

<?php
$mem = new Memcached();
$mem->addServer("127.0.0.1", 11211);

$result = $mem->get("blah");

if ($result) {
    echo $result;
} else {
    echo "No matching key found yet. Let's start adding that now!";
    $mem->set("blah", "I am data!  I am held in memcached!") or die("Couldn't save anything to memcached...");
}
?>

Save that (Control+O) then exit the editor (Control+X).

Step 2 – Open up web browser again and access that file. You will see something like this at the first time.

test cache memcached

Step 3 – Hit the Refresh button and you’ll see something like this indicating memcached works well.

memcached test success

Congratulation. Memcached works!

Configuring WordPress

Assuming you have full LEMP installed, Memcached installed and running, and you have WordPress set up.

First, you have to also install memcache client in order to make plugin like W3 Total Cache can make use of memcached abilities :

issue this command :

apt install memcache

install memcache client

and do not forget to restart php-fpm service:

service php7.0-fpm restart

Now all you need to do is now to install supporting plugin like W3 Total Cache, Memcached Object Cache, Cachify, and so on. Personally, I prefer to use some simple plugins like MemcacheD is Your Friend combined with caching plugin like Cache Enabler or WP Super Cache. It works for me.

Thanks!

5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.