How to Install Memcached from Source on Ubuntu 20.04
Memcached is a high-performance, open-source, distributed memory object caching system. It is often used for speeding up dynamic web applications by storing frequently requested data from the results of database calls and application requests. It is an excellent solution for reducing database load and serving as a placeholder for requests that cannot be served from the database. It provides different ways to store data, such as key-value pairs or sorted sets.
This guide will install Memcached from source code and secure it on a Ubuntu 20.04 server.
Prerequisites
- Deploy a new Ubuntu 20.04 cloud server.
- Create a standard non-root user with sudo rights.
- Update the server.
Install Dependencies
You need to install some tools to compile the Memcached source package.
$ sudo apt-get install gcc make libevent-dev libc6-dev --no-install-recommends
The option --no-install-recommends
is used to install mentioned package only.
The other package descriptions are below:
gcc
is a well-known C compiler for the Memcached source files.make
is used for directing compilationlibc6-dev
is used for reference of GNU C library and header fileslibevent-dev
is a well-known development file for asynchronous event notification
All Memcached dependencies are installed to download the Memcached source package and install it into the system.
Download and Compile Memcached
Download the latest Memcached source file.
$ wget https://memcached.org/latest
Extract the archive contents.
$ tar -xvf latest
Change the working directory to the extracted files. The directory name might be different if you downloaded a newer version.
$ cd Memcached-1.6.12/
Use the --prefix=
parameter to set the directory to install the Memcached binary and libraries. For this example, use the /usr/local
directory.
$ ./configure --prefix=/usr/local/memcached
Compile the Memcached source code with make
.
$ make
Test Memcached by confirming the current version.
$ ./memcached --version
Install Memcached.
$ sudo make install
Test if Memcached is active and running on TCP port 11211.
$ netstat -tulpn | grep :11211
Memcached Service Management
After successful installation, you can manage the Memcached service with systemctl
.
To start Memcached service:
sudo systemctl start memcached
To check the status of Memcached:
sudo systemctl status memcached
To stop Memcached:
sudo systemctl stop memcached
Secure Memcached
By default, Memcached runs on the local interface with a TCP port. To restrict potential attacks, block incoming UDP connections on the Memcached port 11211 through your firewall.
$ sudo ufw deny 11211/udp
Configure Memcached
You can also restrict Memcached to the local interface through the configuration file.
Open the Memcached configuration file with your favorite editor.
sudo vi /etc/memcached.conf
Find the following line in the file, and uncomment if commented.
-l 127.0.0.1
If you want to restrict UDP also, add the following line at the end of the file.
-U 0
Save and exit the file.
Restart the service.
sudo systemctl restart memcached
Use netstat
to verify Memcached is bound to the local interface and only listening to TCP connections.
sudo netstat -plunt
You can use these other options in /etc/memcached.conf
.
- -m: Defines available memory to Memcached.
- -p: Defines the port on which Memcached will listen; the default port is 11211.
- -u: Specify the user to run the Memcached service; by default, the service will run as a root superuser.
- -c: Cap the number of concurrent connections; it allows 1024 connections by default.
Verify Memcached Setup
You can verify the Memcached setup with memcstat
from the libmemcached-tools package.
memcstat --servers="localhost"
You will get the output like:
Server: localhost (11211)
pid: 3927
uptime: 2217682
time: 1547170226
version: 1.4.25
libevent: 2.0.21-stable
pointer_size: 64
rusage_user: 75.436000
rusage_system: 57.768000
curr_connections: 1
total_connections: 53082
...
Connect to Memcached
There are several Memcached clients for different programming languages.
PHP
If you want to use Memcached as a caching database for your PHP applications such as WordPress, Magento, Joomla, or Drupal, you should use the Memcached extension, which you can install with:
sudo apt install php-memcached
Python
Use pip
to install Python libraries.
pip install pymemcache
pip install python-memcached
More Information
See the official website for more information.