Skip to main content

How to Install Memcached Server (Caching) on CentOS 7(CWP7)

It is important to secure your Memcached servers, Because systems like Memcached can contribute to denial of service attacks if improperly configured. We will explain how to install and secure your Memcached server on CentOS 7 Linux distribution. These given instructions also work on RHEL and Fedora Linux. First, update the system:
# yum update -y
Next, install the official Memcached package memcached; as well as libmemcached, which provides several utilities to work with Memcached:
# yum install memcached libmemcached
Note: libmemcached – a client library that offers a couple of tools to manage your Memcached server.
Securing Memcached Configuration Settings for Local Access Only: 
To make assure that installed Memcached service is listening on the 127.0.0.1 local interface, We will also disable the UDP listener. Both of these actions will protect our server from denial of service To do this, we will add the -U 0 parameter to our OPTIONS variable. The file in full should look like this:
# vi /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1 -U 0" 
Save and close the file when you are done. Restart and enable your Memcached service to apply your configuration changes:
# systemctl restart memcached
# systemctl enable memcached
# systemctl status memcached

Let’s discuss each of the above parameters in detail. 

  • PORT : The port used by Memcached to run. 
  • USER : The start-up daemon for Memcached service. 
  • MAXCONN : The value used to set max simultaneous connections to 1024. For busy web servers, you can increase to any number based on your requirements. 
  • CACHESIZE : Set cache size memory to 1048. For busy servers, you can increase up to 4GB. 
  • OPTIONS : Set the IP address of the server, so that Apache or Nginx web servers can connect to it. 
You can verify Memcached is bound only to the local interface and listening only to TCP connections with the ss command:
# ss -plunt | grep memcached
# netstat -plunt
# netstat -plunt | grep memcached
# echo "stats settings" | nc localhost 11211
To check that Memcached is up and running, type the following:
# memstat --servers="localhost"
# memstat --servers="127.0.0.1"
# memcached-tool 127.0.0.1 stats
Next, check the Memcached log to check whether SASL support has been enabled or not:
 # journalctl -u memcached
To connect to the Memcached server you need to use a language-specific client. 

PHP 

To use Memcached as a caching database for your PHP application such as WordPress , Drupal or Magento , you need to install the php-pecl-memcached extension:
# yum install php-pecl-memcache
# yum install php-pecl-memcached ( prefer)

Perl 

Install Memcached Perl Library
# yum install perl-Cache-Memcached

Python 

There are several Python libraries for interacting with memcache. You can install your preferred library using pip :
# pip install pymemcache
# pip install python-memcached
OR
# yum install python-memcached

Where and where not to use Memcached? 

Memcached is best suited for larger websites with heavy traffic. That is servers that get frequent queries. If the most frequent queries are cached, then the website load is considerably reduced. And this speed up the website. 

Even though, caching speed up websites, there are certain situations where it is not preferred to use Memcached.

For instance, if a website has more frequent updates, then caching will not speed it up. Because the cache needs to refresh itself always. Memcached has some limitations too. It cannot store objects larger than 1MB and keys longer than 250 characters.

Comments