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.

For starters, let's update the OS:

# yum update

After updating, perform a memcached installation:

# 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 - the maximum 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 it by 512 or 1 GB (1024 MB).

  • OPTIONS="-l 127.0.0.1″ - this means memcached will only listen on localhost, avoiding any external connections. It is set to INADDR_ANY by default

Memcached is running:

# systemctl enable memcached
# systemctl start memcached

Installing PHP extensions with memcached.

execute:

# yum install php-pear pecl_http php-devel

And we 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 your server has php-fpm installed, then to restart it, run:

# service php-fpm reload

If you have apache installed on your server, then to restart it, execute:

# 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 need to check if the extension is enabled and make sure 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

Configuring the 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