Install and Configure Memcached for Faster Web Apps
When you deploy your web applications, you want to ensure they perform as well as possible. To that end, you take great effort to create code that is efficient, effective, and optimized.
But when you deploy those web apps to a server that isn’t as optimized as your code, those apps might not perform to your expectations. Fortunately, the Linux operating system offers plenty of ways to optimize a server to meet the needs of your applications. One such optimization is installing and configuring memcached.
What Is Memcached?
Memcached is a free, open source distributed memory object caching system. The goal of this system is to speed up dynamic web applications by alleviating the database load. It works as an in-memory, key-value store for small chunks of data that result from database calls, APIs, or page rendering. Because it stores frequently accessed data in memory, the retrieval of that information is significantly faster than if it was accessed via a drive.
Memcached is a volatile cache, which means data is not persistent. In other words, if the server goes down or is rebooted, or loses its source of power, the data stored in memory is lost. This was done to further maximize speed. Memcached can is horizontally scalable (by adding more servers) and load balancing is achieved with hashing and other methods.
I want to walk you through the process of installing and configuring Memcached on Linux. I’ll demonstrate this process on Ubuntu Server 24.04, but you can accomplish the same task on older releases (preferably an LTS release) or other distributions (such as Fedora, AlmaLinux, etc.).
With that said, let’s get to it.
What You’ll Need
The only things you’ll need are a running instance of Ubuntu Server and a user with sudo privileges. If Ubuntu isn’t your server distribution of choice, you’ll need to only alter the installation command.
Installing Memcached
The first thing we’ll do is log in to the server and then run an update/upgrade. This is accomplished from the terminal window with the command:
sudo apt-get update && sudo apt-get upgrade -y
One thing to keep in mind is that if the kernel is upgraded, you’ll need to reboot the server. Because of this, you should run the above command at a time when a reboot is possible. Or, you could first test this out on a non-production machine (which I would recommend anyway).
To install Memcached, issue the command:
sudo apt-get install memcached libmemcached-tools -y
Once that completes, you can verify the installation by issuing the command:
memcached --version
You should see the release number in the output.
With Memcached installed, you’ll want to start and enable the service, which can be done with the command:
sudo systemctl enable --now memcached
The above command will not only start the service but make sure it’s enabled so that it’ll automatically start at the boot of the server. You can verify the service is running with the command:
systemctl status memcached
The service should be listed as active|running.
Memcache is running and listens to port 11211 by default.
If you know you’ll need the Memcached PHP extension, you can install it with:
sudo apt-get install -y php-memcached
You then have to enable the extension with:
sudo phpenmod memcached
Restart Apache (to ensure the changes are applied) with the command:
sudo systemctl restart httpd
You could also add Python support by installing the necessary software like this:
sudo apt install python3-pymemcache -y
If you need Perl support, the command is:
sudo apt install libcache-memcached-libmemcached-perl -y
Configuring Memcached
The next step is to configure Memcached. You shouldn’t have to change much in the configuration file. Open the config file with:
sudo nano /etc/memcached.conf
The first line you might want to change is this:
-l 127.0.0.1
The above line configures Memcached to listen to all IP addresses. Keep in mind this is the only security measure built into the system. Because of that, you want to make sure Memcached is only listening on an interface that is protected by the firewall. You can also add the IPv6 interface (if applicable) by adding the following below that line:
-l ::1
You can also define the amount of memory the daemon can grow to reach. By default, it’s set to 64 MB. If you know you’ll need more memory than that, increase the maximum to meet the requirements of your app.
You might also need to enable UDP sockets. For that, you’ll need to add the line (to the bottom of the file):
-U 11211
If you don’t need TCP connections, you can change that line to:
-p 0
Save and close the file.
Restart the service with:
sudo systemctl restart memcachd
Accessing the Memcached Console
To access the memcached console (the CLI) you’ll use telnet (talk about a flashback). If telnet isn’t installed, you can achieve that with the command:
sudo apt-get install telnet -y
Access the console with:
telnet localhost 11211
Check the status of the Memcached service with:
stats
You should see a long list of lines, each of which starts with STAT and lists various statuses.
Exit the console with:
quit
Enhancing the Security of Memcached
Since Memcached doesn’t include much in the way of security, you can beef it up with SASL (Simple Authentication Authentication and Security Layer). To do this, you must first install a piece of software with the command:
sudo apt-get install sasl2-bin -y
Create a new SASL directory with:
sudo mkdir /etc/sasl2
Create a configuration file with:
sudo nano /etc/sasl2/memcached.conf
Add the following three lines to the new file:
log_level: 5 mech_list: plain sasldb_path: /etc/sasl2/memcached-sasldb2
- log_level indicates the level of logging you want to use
- mech_list is a list of whitespace-separated authentication methods to be used
- sasldb_path is the path of the SASL database file
Save and close the file.
Create a new use entry within the SASL database with:
sudo saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 USERNAME
Where USERNAME is the name of the new user.
You’ll be prompted to type and verify a password for the new user.
Give the new owner permission for the Memcached database with:
sudo chown memcached:memcached /etc/sasl2/memcached-sasldb2
Verify the connection with:
nc -u 127.0.0.1 11211 -vz
You should see success in the output.
To enable SASL, open the memcached.conf file with:
sudo nano /etc/memcached.conf
Add the following line at the bottom of the file:
-S
Save and close the file. Restart the service with:
sudo systemctl restart memcached
If you issue the command ss -plunt you’ll see the Memcached is listening on port 11211.
tcp LISTEN 0 1024 127.0.0.1:11211 0.0.0.0:*
You can then verify it’s working with the command:
memcstat --servers="127.0.0.1" --username="USERNAME" --password=PASSWORD
Where USERNAME is the username you created earlier and PASSWORD is the password you set when creating that username.
And that’s all there is to installing Memcached on Linux. At this point you can develop and deploy your apps to include Memcached for improved performance.