Memcached is a software that implements a RAM caching service based on a hash table.

You can also install this application at one-click checkout
Detailed description of this One-Click-Apps

Memcached is a server that stores some data in RAM with a specified lifetime. The data is accessed by a key (name). You can think of Memcached as a hash table stored on the server. It's mostly used to cache web page code, database query results, etc.

Installing memcached in Debian/Ubuntu

Before installing, upgrade your system:

$ sudo apt-get update

Now use the following command to install Memcached:

$ sudo apt-get install memcached

Install PHP with a few necessary packages:

# apt-get install php5 php5-dev php5-pear php5-memcached

You should also install the MemCache module for PHP:

# apt-get install libmemcached-dev build-essential

Now let's install the php Memcache extensions using PECL:

# pecl install memcache

Now enable MemCache support in PHP. Before creating a new file, make sure it probably already exists and if the file already exists, just skip the command below:

$ echo "extension=memcache.so" > /etc/php5/apache2/conf.d/20-memcache.ini

After all the changes, restart the Apache service:

# service apache2 restart

Installing memcached in CentOS

First, let's upgrade the OS:

# yum update

After updating, install memcached:

# yum install memcached

Setting up memcached

Let's edit the file:

$ vim /etc/sysconfig/memcached

Make sure that this configuration file has the following lines:

PORT="11211"  
USER="memcached"  
MAXCONN="256"  
CACHESIZE="64"  
OPTIONS="-l 127.0.0.1"  
  • PORT 11211 is the default listening port for memcached.
  • USER is the memcached user from which the daemon will run.
  • MAXCONN - is maximal number of possible connections.
  • CACHESIZE is the number of megabytes (64 MB) to use if you have a lot of traffic, this is good for most small/medium sites. For a busy server you can increase this to 512 or 1 GB (1024 MB).
  • OPTIONS="-l 127.0.0.1″ - this means memcached will only listen on localhost, avoiding any external connections. You can set the IP address of the server. By default, this is set to INADDR_ANY. This is an important option because there is no other way to ensure that it is set. Binding to an internal or gateway interface is suggested.

Running Memcached is done by:

# systemctl enable memcached
# systemctl start memcached

Installing PHP extensions with memcached.

execute:

# yum install php-pear pecl_http php-devel

And execute:

$ pecl install memcache

If PECL asks you the question you see below when installing, just press ENTER:

"Enable memcache session handler support? [yes]"

Restarting services

If you have php-fpm installed on your server, then to restart it, run:

# service php-fpm reload

If your server has apache installed, then to restart it, execute: `` # service php-fpm reload

# systemctl restart httpd

Testing/checking Memcached

Checking MemCache configuration

Use the following command to test and verify that the Memcached service is working properly:

$ echo "stats settings" | nc localhost 11211

Now we have to check if the extension is enabled and make sure that it works correctly. Create a file in your home directory and write the following PCP code:

# echo "<?php phpinfo(); ?>" &gt; /var/www/html/php_info.php

Setting up a firewall for Memcached

Add the following rules to allow connections (for memcached to work):

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  

Check if the memcached daemon is running on the OS:
$ ps -aux | grep memcached

Updated Sept. 15, 2024