Installing and Configuring Memcached
14:24, 16.08.2022
Memcached is a system for caching objects located in RAM. Installing and configuring Memcached is necessary in order to reduce the load on the file system and database, thereby speeding up the work of the entire website. Caching allows you to reduce the number of database requests and, as a result, increase the stability and fault tolerance of your entire network infrastructure. Memcached can store objects of various types, but this technology is most useful in relation to frequently requested data. The same files that are requested once every 2-3 days or less are better stored in the file cache, but here everything depends on the architecture of your project.
In this article, we will tell you how to install Memcached on the server and prepare it to work.
Installing and Configuring Memcached in CentOS 7
On this OS, everything is extremely simple. Installation first:
[root@localhost]# yum -y install Memcached
After that, it remains to launch the service and add it to the startup:
[root@localhost]# systemctl start memcached [root@localhost]# systemctl enable memcached
If you do not have a PHP module on your server, you also need to install it. For example, from the Remi repository:
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm sudo rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm sudo yum install php php-fpm php-gd php-mysql
If Memcached will be used in TCP mode, which is most common, then you need to edit the configuration file:
[root@localhost]# nano /etc/sysconfig/memcached USER="memcached" PORT="11211" MAXCONN="1024" CACHESIZE="1024" OPTIONS="-t 8 -l 127.0.0.1 -U 0"
The default values are listed above, but they can be edited. The parameters themselves mean the following:
- MAXCONN – number of simultaneous connections;
- CACHESIZE – the amount of RAM that is allocated for the cache;
- OPTIONS – the number of threads.
After making changes, the service must be restarted:
[root@localhost]# systemctl restart memcached
Configuring a firewall for Memcached
For Memcached to work correctly, you need to add the following rules for connection resolution:
iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT
After that, check if the service works on your OS:
$ ps -aux | grep memcached
This completes the firewall setup, just like our article. If you have any questions regarding the installation and configuration of the object caching system, please contact our specialists via Livechat.