Reserve the NVIDIA GH200 Grace Hopper™ Superchip at Vultr today.
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.
Deploy a new Ubuntu 20.04 cloud server.
Create a standard non-root user with sudo rights.
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
--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 compilation
libc6-dev is used for reference of GNU C library and header files
libevent-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 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/
--prefix= parameter to set the directory to install the Memcached binary and libraries. For this example, use the
$ ./configure --prefix=/usr/local/memcached
Compile the Memcached source code with
Test Memcached by confirming the current version.
$ ./memcached --version
$ sudo make install
Test if Memcached is active and running on TCP port 11211.
$ netstat -tulpn | grep :11211
After successful installation, you can manage the Memcached service with
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
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
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.
If you want to restrict UDP also, add the following line at the end of the file.
Save and exit the file.
Restart the service.
sudo systemctl restart memcached
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
-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.
You can verify the Memcached setup with
memcstat from the libmemcached-tools package.
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 ...
There are several Memcached clients for different programming languages.
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
pip to install Python libraries.
pip install pymemcache pip install python-memcached
See the official website for more information.