Memcached is a free and open source high-performance memory key-value database. It is mainly used for system caching to improve the response speed of the application by caching the results in the database.
In this guide, we will describe how to install and configure the latest version of Memcached on Ubuntu 18.04. The same instructions apply to Ubuntu 16.04 and any other Ubuntu-based distributions.
Before continuing this guide, make sure you have logged in to the system as a user with sudo privileges.
The Memcached package is included in the default 18.04 software source. This installation is very straightforward, just follow the steps below:
sudo apt update
sudo apt install memcached libmemcached-tools
This libmemcached-tools
package contains various command line tools to manage Memcached server.
sudo systemctl status memcached
The output will look like this:
memcached.service - memcached daemon
Loaded:loaded(/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
Active:active(running) since Tue 2019-04-3015:13:41 PDT; 37s ago
Docs: man:memcached(1)
Main PID:10753(memcached)
Tasks:10(limit:2319)
CGroup:/system.slice/memcached.service
`- 10753 /usr/bin/memcached -m 64-p 11211-u memcache -l 127.0.0.1-P /var/run/memcached/memcached.pid
That's it, at this time you have installed Memcached and it is running on your Ubuntu 18.04 server.
Memcached can be configured by editing the /etc/memcached.conf
file. The default configuration file is sufficient for most users.
By default, Memcached is configured to only listen on the local localhost. If the client and server connecting to the server are on the same host, you do not need to modify the default configuration file.
When Memcached is not properly configured, it can be used to perform denial of service attacks (DDos). If you want to allow remote access to Memcached service, you need to configure your firewall and only allow trusted clients to access Memcached through port 11211 UDP.
In the following example, suppose you want to connect to your Memcached server through a LAN. The server IP address is 192.168.100.20
, and the client IP address is 192.168.100.30
.
The firewall configuration tool that comes with Ubuntu is called UFW. By default, UFW is installed, but not enabled. Before enabling UFW, first add a rule to allow SSH connections:
sudo ufw allow 22
Allow remote client IP address access:
sudo ufw allow from192.168.100.30 to any port 11211
To enable UFW, enter:
sudo ufw status
Once the firewall is configured, the next step is to edit the Memcached configuration and set the Memcached service to listen to the server's LAN interface:
To do this, open the configuration file memcached.conf
:
sudo nano /etc/memcached.conf
Locate this line, replace 127.0.0.1
with -l 127.0.0.1
and use the server's IP address 192.168.100.20
.
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.-l 192.168.100.20
Restart the Memcached service to make the changes take effect:
sudo systemctl restart memcached
You can connect to the Memcached server from your remote location.
To connect to the Memcached server, you need to use a client in a specific language.
To use Memcached as your PHP application such as Wordpress, Drupal, Joomla or Magento as a cache database, you need to install php-memcached
extension:
sudo apt install php-memcached
There are some libraries on Python that can be used to interact with memcache, you can use pip to install your favorite libraries:
pip install pymemcache
pip install python-memcached
You have learned how to install Memcached on your Ubuntu server. For more information on this topic, read Memcached Wiki.
Recommended Posts