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.
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
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:
After making changes, the service must be restarted:
[root@localhost]# systemctl restart memcached
For Ubuntu users everything is also quite trivial:
sudo apt install memcached libmemcached-tools
Along with the Memcached service, additional utilities necessary for its operation are also installed. As for the configuration, all manipulations are performed through the /etc/memcahced.conf file – it contains options that are passed to the service at the time of startup.
As for the assignment of options, you need to know the following:
-d – work as a service;
-v – displays more detailed information when working;
-vv – more information;
-m – the amount of RAM allocated to the service for operation (64MB is allocated by default, but this is often not enough, so it is recommended to increase);
-p – is the port number on which Memcached is running;
-u – the name of the user on whose behalf the service is launched;
-l is the IP address where the service will wait for the connection (it is recommended to specify 127.0.0.1, since in this case no one from the external network will have access to your data);
-c – number simultaneous connections;
-P is the path to the service's PID file in the file system.
In fact, you can leave all the values in the form in which they were set by default – it is recommended to change only the IP address and the amount of allocated RAM, since the efficiency of Memcached will directly depend on it.
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.