Yandex
Update cookies preferences

How to install Memcached on Ubuntu 22.04

By default, most temporary data in the operating system is stored on disk. Accessing it is slower, even on modern SSDs, than if the data were stored in operating memory. One way to improve system performance is to use data caching in operating memory.

Memcached is the most common tool for this task. It helps to speed up dynamic web applications by reducing the load on the database server.

Memcached is a caching system that makes efficient use of operating memory, freeing web servers from having to directly process cache. This is especially useful for applications that deal with large amounts of real-time data: social networks, online stores and content management systems. With the ability to integrate with PHP via the php-memcached ubuntu extension, you can easily implement this tool into your application and enjoy improved performance.

In this article, we will look at how to perform a memcached installation on Ubuntu 22.04 using two different methods, as well as familiarize ourselves with its basic settings and features.

Preparing to install Memcached

To install memcached on Ubuntu 22.04 you will need:
  • Linux-based server (e.g. Ubuntu 22.04);
  • privileged access (root or sudo access);
  • optionally - a configured firewall.

Installing Memcached in Ubuntu 22.04

Method 1 - the easiest way to install memcached is through Ubuntu's standard repositories. For this we will use the apt package manager.

Open a terminal and run the following command to update the package list:
apt updateNow install memcached using the command:
apt install memcached libmemcached-toolsAfter the installation is complete, you can verify success by using the following command to display version information:
memcached --versionYou should see the following output:
20240930_l5zAIdHN

Then make sure that the memcached service is running - to do this, check its status via systemctl:
systemctl status memcachedIf you want to completely remove memcached along with its configuration files - use the command:
apt purge memcached libmemcached-tools
apt autoremove
Method 2 - alternative method - download the latest version of memcached from the official website and install it manually. This method is suitable for those who need more flexible configurations or access to the latest version of the program.

First, download the memcached archive using wget:
wget https://www.memcached.org/files/memcached-1.6.31.tar.gzExtract the downloaded package using the command:
tar xvf memcached-1.6.31.tar.gzNavigate to the directory with the extracted contents:
cd memcached-1.6.31Now install dependencies such as libevent-dev:
apt install libevent-dev build-essentialAfter that, do the configuration and installation of memcached:
./configure && make && make install && hash -rUse the command to start the service:
memcached -u memcache -dCheck if the service is running:
ss -luptn |grep memcached20240930_lS35Irja

Memcached configuration

After installing memcached you need to configure it to work correctly. Open the configuration file in a text editor, for example:
nano /etc/memcached.confIn this file you can change the IP address and port on which memcached will listen for connections, as well as set the amount of memory that the service will use.

After making the changes, restart the memcached service for the changes to take effect:
systemctl restart memcachedAnd for the second method described above, all parameters are set in the startup command itself. For example, to increase the RAM size from 64 MB to 1024 MB:
killall -9 memcached
memcached -u memcache -d -m 1024

Connecting PHP applications to Memcached

If you are using PHP applications like WordPress, Joomla or Drupal and you want to use memcached for caching, you need to install an additional module for PHP. To do this, run the command:
apt install php-memcachedThis module will allow PHP applications to communicate directly with memcached, improving their performance by using operating memory to cache data.

Improved security

When using Memcached on a server accessible from an external network, it is important to take care of additional security measures. Memcached does not use an authentication mechanism by default, which can be a vulnerability if your server is open to external connections. To improve security, you can implement simple login and password authentication, and restrict access to the service to specific IP addresses only.

One of the most effective ways to limit access is to use a firewall to allow only the traffic you want on a specific IP address and port. Memcached runs on port 11211, and you can open this port only for your server using iptables firewall:
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 11211 -j DROP
In addition, if additional protection is required, you can set up a tunnel via SSH to protect Memcached traffic from unauthorized interception. This approach is considered the best approach to ensure data privacy and security.

Checking Memcached operability

After memcached installation and security configuration is complete, you need to make sure that the service is running correctly. To do this, you can perform a status check using the systemctl command:
systemctl status memcachedIf the service is started and running without errors, you will see the corresponding status in the command output.

You can also test whether Memcached caches data. Open a command prompt and connect to Memcached using the telnet command:
telnet localhost 11211Once connected, you can use simple Memcached commands such as `stats` to check general information about the state of the service and its performance.
20240930_VNGmRkdl

The above command set allows you to monitor how Memcached uses system resources and performs its main task - caching data in operating memory to speed up request processing.

Advantages of using Memcached:

  • Increased performance. As data is cached in operating memory, query processing time is reduced, especially on highly loaded systems.
  • Reducing the load on the database. Memcached removes some of the load from the database, allowing it to process fewer requests.
  • Extensive language support. Supports various programming languages including PHP, Python, Ruby and many others.
  • Distributed cache architecture. Memcached can be scaled horizontally by adding new servers to caching clusters. This allows Memcached to be used for large distributed systems, providing caching on multiple servers that communicate with each other.

Summary

Installing memcached on Ubuntu 22.04 is a simple process that can be done in two ways: either using the apt package manager or manually by downloading the archive from the official Memcached website. Both methods of installing memcached ubuntu have their advantages, but for most users, installing via apt is enough as it is fast and convenient.

Using memcached and its extensions for PHP applications, you can significantly optimize your web services, making them faster and more stable, which is especially important for resources with high traffic and complex database architecture.
30 Sep 2024, 13:51:06