How To Install Memcached on Ubuntu 16.04
Posted on April 29, 2018 • 4 min read • 809 wordsIn this tutorial we'll learn how to install Memcached on Ubuntu 16.04 (LTS) Xenial Xerus.
Memcached (pronounced: mem-cash-dee) is a free, high performance, distributed memory object caching system.
Memcached can be use for any caching usage but mostly used by dynamic web application to reduce database load. We can also cache API calls and page rendering.
In this tutorial we’ll learn how to install Memcached on Ubuntu 16.04 (LTS) Xenial Xerus. Previously we already learn how to install memcached on Ubuntu 14.04
This tutorial assumes you have a fresh install of Ubuntu server 16.04. You can also follow this tutorial on any Ubuntu 16.04 flavors. Using Ubuntu server will give minimalist installation of Ubuntu.
Let’s update our base system to latest update using command below.
sudo apt-get update
sudo apt-get upgrade
Install memcached by running command below
```bash
sudo apt-get install memcached
That’s it, memcached installed. Now let’s check whether memcached already started and listen to specific port.
Run netstat and find memcached process.
$ sudo netstat -naptu | grep memcached
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 3125/memcached
udp 0 0 127.0.0.1:11211 0.0.0.0:* 3125/memcached
Or we can also run run netstat and find memcached default port `11211`
```bash
$ sudo netstat -naptu | grep 11211
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 3125/memcached
udp 0 0 127.0.0.1:11211 0.0.0.0:* 3125/memcached
We can also use ss as replacement of netstat to check where memcached listen.
ss -4n state listening | grep 11211
tcp 0 128 127.0.0.1:11211 *:*
We can check memcached service status by running command below. This is sysv compatible service command.
```bash
ubuntu@ubuntu-xenial:~$ sudo service memcached status
● memcached.service - memcached daemon
Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2017-10-09 02:24:57 UTC; 53s ago
Main PID: 2351 (memcached)
CGroup: /system.slice/memcached.service
└─2351 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
Oct 09 02:24:57 ubuntu-xenial systemd[1]: Started memcached daemon.
Since Ubuntu 16.04 already use systemd, we can also systemctl
command to check memcached service status.
$ sudo systemctl status memcached
● memcached.service - memcached daemon
Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2017-10-09 02:24:57 UTC; 1min 4s ago
Main PID: 2351 (memcached)
CGroup: /system.slice/memcached.service
└─2351 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
Oct 09 02:24:57 ubuntu-xenial systemd[1]: Started memcached daemon.
## Configuring Memcached
Now we have memcached running let's learn how to configure memcached. Memcached comes with two configuration files.
* `/etc/default/memcached`
* `/etc/memcached.conf`
We can enable or disable memcached on boot by changing parameter on `/etc/default/memcached` file. The default value on this file is
```bash
ENABLE_MEMCACHED=yes
To disable memcached on boot we need to change the line above to
ENABLE_MEMCACHED=no
Now let's check `/etc/memcached.conf` configuration file.
### Memcached Logging on Ubuntu 16.04
Memcached's `logging` directive is skipped by systemd-memcached-wrapper. This script located in `/usr/share/memcached/scripts/systemd-memcached-wrapper`. In this scipt you can find line:
```perl
my $ignore_directives = ("logfile");
This doesn’t mean you cannot see memcached logs. The logs will be captured by systemd and we can use journalctl
to see the log.
Let’s test by increasing the verbosity of the logs. Open /etc/memcached.conf
. Find and uncomment this line
# -vv
Now, restart our memcached installation
```bash
sudo systemctl restart memcached
We can see memcached logs by running the following command
sudo journalctl | grep memcached
By default memcached will use 64 MB of memory. Memcached doesn’t reserve the memory on start but the memory usage will grow as needed with the limit as specified in -m
option.
To increase memory limit to 1GB for example, we can change the line below
-m 64
to
-m 1024
We can configure memcached port using -p
option. By default memcached use port 11211. If you change this port to non default you need to configure your application to also pointing to the same port.
-p 11211
This options is to configure which user run memcached process. We will rarely need to change this.
-u memcache
### Memcached listen address
By default memcached will listen on all network interfaces. We can configure on which address memcached listen to.
```bash
-l 127.0.0.1
Please note that this is the only security mechanism that memcached have. If you plan to open your memcached server from another server ensure you add firewall to the memcached server.
If you use cloud service like Amazon Web Services you can use security groups.
If you use provider that doesn’t offer firewall or you install on your own infrastructure you can use iptables.
We can limit connection using -c
option. The default value is 1024 connections.
-c 1024
In this tutorial we learned how to install memcached on Ubuntu 16.04. We also learn how to do basic configuration of memcached. I hope this tutorial is useful to help you installing and configuring memcached. Introducing cache on your stack can improve the performance of your web application. Until next time.