Memcached is a free, open-source, high-performance and distributed memory caching system used to speed up dynamic websites by caching data in RAM. Memcached also provides a library for different programming languages including, PHP, Perl, Ruby, and Python. Memcached is the only caching system available for free and used by many organizations including, YouTube, Facebook, Twitter, Reddit, Drupal and Zynga.
In this tutorial, we will show you how to install and configure Memcached on CentOS 7 server.
Requirements
- A cloud server running CentOS 7 with PHP and Apache installed.
- A root password configured on your server.
Getting Started
By default, SELinux is enabled in CentOS 8 server. So you will need to disable it first.
You can do this by editing /etc/selinux/config file:
# nano /etc/selinux/config
Change the following line:
SELINUX=disabled
Save and close the file. Then, restart your server to apply the changes.
Install Memcached
By default, Memcached is available in the CentOS 7 default repository. You can install it by just running the following command:
# yum install memcached -y
Once Memcached is installed, start the Memcached service and enable it to start after system reboot with the following command:
# systemctl start memcached
# systemctl enable memcached
You can now check the status of Memcached with the following command:
# systemctl status memcached
You should see the following output:
● memcached.service - Memcached
Loaded: loaded (/usr/lib/systemd/system/memcached.service; disabled; vendor preset:
disabled)
Active: active (running) since Sat 2019-11-09 00:09:32 EST; 4s ago
Main PID: 3450 (memcached)
CGroup: /system.slice/memcached.service
└─3450 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
Nov 09 00:09:32 localhost.localdomain systemd[1]: Started Memcached.
Configure Memcached
By default, Memcached is configured to listen on port 11211 on all interface. So it is recommended to configure Memcached to listen on the local (127.0.0.1) interface only. This will protect your server from denial of service attacks. You can do it by editing the Memcached default configuration file /etc/sysconfig/memcached:
# nano /etc/sysconfig/memcached
Make the following changes:
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"
Save and close the file when you are finished. Then, restart the Memcached service to apply the configuration changes:
# systemctl restart memcached
Now, you can check the Memcached listening interface with the following command:
# netstat -tulpn | grep :11211
You should see the following output:
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 3598/memcached
udp 0 0 127.0.0.1:11211 0.0.0.0:* 3598/memcached
You can also check the status of the Memcached server with the following command:
# memcached-tool 127.0.0.1 stats
Install Memcached PHP Extension
If you want to use Memcached as a caching database for PHP-based applications. Then, you will need to install Memcached PHP extension on your server. You can install it with the following command:
# yum install php-pecl-memcache -y
Once installed, restart the Apache web service to apply the changes:
# systemctl restart httpd
Test Memcached PHP Extension
To test Memcached PHP extension, create an info.php file in Apache web root directory:
# nano /var/www/html/info.php
Add the following lines:
<?php
phpinfo();
?>
Save and close the file when you are finished.
Next, create an Apache virtual host file with the following command:
# nano /etc/httpd/conf.d/memcache.conf
Add the following lines:
ServerAdmin admin@yourdomain.com
DocumentRoot /var/www/html/
ServerName yourdomain.com
DirectoryIndex info.php
Options FollowSymLinks
AllowOverride All
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/httpd/access_log common
Save and close the file when you are finished. Then, restart the Apache service to apply the changes:
# systemctl restart httpd
Now, open your web browser and type the URL http://yourdomain.com. You should see the following screen:
The above page indicates that Memcached is installed with PHP support.
Conclusion
In the above tutorial, you learned how to install Memcached on CentOS 7 server. You can now easily cache your PHP-based application with Memcached.