Verification: a143cc29221c9be0

Php 7 memcached centos 7

Introduction

Memcached is a distributed, high-performance, in-memory caching system that allows you to improve and speed up the performance of dynamic web applications by caching data and objects in memory. It is primarily used to speed up sites that make heavy use of databases.

Memcached is also used to cache entire database tables and queries to improve the performance of the database. Memcached is free and is used by many sites like Facebook, YouTube and Twitter.

This tutorial shows you how to install and configure Memcached on CentOS 7.

Requirements

  • A server running CentOS 7 with Apache and PHP installed

Installing Memcached

First make sure all packages are up to date:

sudo yum -y update

Now execute the following commands to install Memcached:

sudo yum -y install memcached

Start Memcached services and add it to automatically start on your system start-up with:

sudo systemctl enable memcached
sudo systemctl start memcached

Configure Memcached

The default Memcached configuration file is located in the /etc/sysconfig directory.

To configure Memcached, edit the /etc/sysconfig/memcached file:

sudo nano /etc/sysconfig/memcached

Set or update parameters as follows, customizing them to your needs:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

This is a brief description of the parameters:

**PORT**: The default port used by Memcached to run.
**USER**: The user Memcached runs as.
**MAXCONN**: The maximum number of allowed connections to Memcached. You can increase to any number based on your requirements.
**CACHESIZE**: The cache size for memory.
**OPTIONS**: Set IP address of server, so that Apache or Nginx web servers can connect to it.

Exit and save the configuration file, and then restart Memcached:

sudo systemctl restart memcached

Run the following command to verify that Memcached is running:

sudo netstat -tulpn | grep :11211

You will see the following output:

To check the stats of the server using memcached-tool:

sudo memcached-tool 127.0.0.1 stats

Installing the Memcached PHP Extension

You can install PHP extension to work with Memcached daemon with the command:

sudo yum install php-pecl-memcache

Now, restart Apache so that the changes take place:

sudo systemctl restart memcached
sudo systemctl restart httpd

To test Memcached PHP extension, create an info.php file in apache web root directory.

sudo nano /var/www/html/info.php

Add the following content:

Save the file and access it at http://your_server_ip/info.php in a web browser.

When the PHP info page is rendered in your browser look for this section:

Install Memcached Server

First we will install Memcached server on our system. Use following command to install memcached using yum package manager.

yum install memcached

Now start Memcached service using following commands.

For CentOS/RHEL 7systemctl enable memcached
systemctl start memcached

For CentOS/RHEL 6/5service memcached start
chkconfig memcached on

Install PHP with Memcache PHP Extension

Now we have installed Memcached server on our system. But to use Memcached service using php scripts we need to install Memcache php extension. So first install php and other required php modules and then use PECL to install PHP Memcache extension using following commands.

commentyum groupinstall "Development tools"
yum install zlib zlib-devel
yum install php php-devel php-pear

After installing php-pear package install PECL extension using command below.

pecl install memcache

Now we need to enable memcache module in php. Add following line at end of php.ini file.

echo "extension=memcache.so" >> /etc/php.d/memcache.ini

Restart Apache service to enable extension

For CentOS/RHEL 7systemctl restart httpd

For CentOS/RHEL 6/5service httpd restart