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
- A SSH client like Bitvise or Putty and basic knowledge about how to use it.
- Basic knowledge about common Unix command to SSH to your server.
- A server or VPS with at least 256MB of RAM (OpenVZ) but 512MB is recommended if you are using KVM or Xen.
- Ubuntu 16.04 either 32-bit or 64-bit.
- About 15 minutes of your time.
- 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
- Provision a cloud server on Atlantic.net (if you want).
- Install full LEMP stack (latest Nginx, PHP7, and MySQL) on your Ubuntu 16.04 server.
- Setup new website on your LEMP server (create Server Blocks).
- 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).
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
Step 4 – You will also need to install required php extension for memcached :
apt install php-memcached
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 :
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.
Step 7 – Now restart both memcached and php7-fpm services :
service memcached restart service php7.0-fpm restart
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.
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.
Step 3 – Hit the Refresh button and you’ll see something like this indicating memcached works well.
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
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!
perhaps memcached support php7 soon.
ups forget to add, for centos i mean 🙂
#maafbunda
Commands:
sudo apt install memcache
or
sudo apt-get install memcache
failed:
Unable to locate package memcache
Hi Sawiyati, does this way of installing memcached works with VestaCP (with PHP7-FPM) already setup ?
Hello;
1. We should install memcached and memcache?
2. I thought you were using varnish before?
Thanks