How To Install And Configure MemCached On Centos 7
In my previous blog , I have explained How to Setup Memcached cluster in AWS
In this guide , We will see how to install and configure memcached on Centos 7.
What Is Memcached?
Memcached is an open sources distributed memory caching system.
It is used to speed up and improve the performance of the applications by storing the information temporarily in memory.
By doing so , It will also helps to reduce the connection to the database.
Lets go ahead and install Memcached on Centos.
Make sure you have sudo or root privileges to the system.
Update the Centos package repository using the command,
yum update -y
Using the below command , We can install memcached on the system.
And also we will install libmemcached , a client library.
yum install memcached libmemcached -y
Optionally , We can install Memcache module for PHP functionality.
yum install php-pecl-memcache -y
After the packages are installed , We can check whether Memcache PHP module is installed on the system, Execute the command.
php -m |grep memcache
The above command should reply as memcache
While installing memcached packages , The systemd unit file memcached.service will be created by itself , using which we can easily manage the memcached service.
To start the memcached service,
systemctl start memcached
To check the status of the service,
systemctl status memcached
To enable memcached service to automatically start on system boot up,
systemctl enable memcached
By default , Memcached runs on the Port 11211.
You can check the same using the below command.
netstat -nltp |grep memcached
At some point , If you want to change the settings of the Memcached service , We can do it by modifying /etc/sysconfig/memcached file.
We can change the Port , Allow remote connections , Memory for Caching.
In the memcached configuration file , We can modify values such as PORT , CACHESIZE , MAXCONN , OPTIONS.
If you want the memcached to listen only to localhost, Open the /etc/sysconfig/memcached file and modify the OPTIONS variable as shown below.
OPTIONS="-l 127.0.0.1 -U 0"
Save and close the file.
Restart the memcached service for the changes to take effect.
systemctl restart memcached
Now You can see that the memcached service listens only to the Localhost.
We can change other settings such as,
PORT – The port at which memcached should run.
MAXCONN – We can set the maximum simultaneous connections for the web servers to create.If the web servers gets huge traffic , Then increase this to higher value as per your requirements.
CACHESIZE – We can set the cache size memory and it can be increased if required.
OPTIONS – We can restrict servers from connecting to memcached.
Memcached has a tool to check the status of the server ,
memcached-tool localhost stats
Using Memcached With PHP
Let’s test this, By installing PHP and Apache web server to use the Memcached service.
To install Apache and PHP packages , Use the below command.
yum install httpd php -y
After the packages are installed , Start the httpd service and then we can verify the HTTP and PHP installations using the below command.
service httpd start
service httpd status
Next step is to create a sample php file – phpinfo.php under /var/www/html folder.
Add these content to the file,
Save and close the file And then restart the http service.
service httpd restart
Lets verify the installation , Go to your browser and type the URL as shown below.
You will see the information related to PHP and Memcached modules on the following page.
We have successfully installed and Configured memcached on the Centos 7.
Also We have tested the same by installing PHP module and HTTP Web server to use Memcached.
If you found it helpful.Please check out other blogs.