Installing and configuring Memcached
Instructions for installing and configuring the data caching service - Memcached
On a VPS or a dedicated server, installing Memcached helps speed up web applications by storing data in memory. This tool is suitable for both small websites and high-traffic projects on Linux.
Memcached is software that provides a memory-based data caching service using a hash table.
You can install this application with a one-click setup when placing your order. Detailed information is available in the One-Click-Apps section.
Memcached is a server that keeps data in memory with a set expiration time. Data is accessed by key (name). You can think of Memcached as a hash table stored on the server. It is primarily used to cache web pages, database query results, and other data that require fast access.
Installing Memcached on Debian/Ubuntu
First, update the system:
$ sudo apt-get update
Then install Memcached:
$ sudo apt-get install memcached
Install PHP with the required packages:
$ sudo apt-get install php5 php5-dev php-pear php5-memcached
Also, install the MemCache module for PHP:
$ sudo apt-get install libmemcached-dev build-essential
Next, install the Memcache PHP extension via PECL:
$ sudo pecl install memcache
Enable Memcache support in PHP. If the file already exists, skip this step:
$ echo "extension=memcache.so" > /etc/php5/apache2/conf.d/20-memcache.ini
Restart Apache:
$ sudo service apache2 restart
Installing Memcached on CentOS
Update the system:
$ sudo yum update
Install Memcached:
$ sudo yum install memcached
Configuring Memcached
Edit the configuration file:
$ sudo vim /etc/sysconfig/memcached
Make sure it contains the following lines:
PORT="11211"
USER="memcached"
MAXCONN="256"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"
PORT— default listening port for Memcached.USER— the user under which the Memcached daemon runs.MAXCONN— maximum number of connections.CACHESIZE— cache size in megabytes (64 MB is enough for most small to medium sites; for busy servers, increase to 512 MB or 1 GB).OPTIONS="-l 127.0.0.1"— Memcached will listen only on localhost. You can specify the server IP for external connections; by default,INADDR_ANYis used.
Start Memcached:
$ sudo systemctl enable memcached
$ sudo systemctl start memcached
Install PHP extensions for Memcached:
$ sudo yum install php-pear pecl_http php-devel
$ sudo pecl install memcache
If PECL asks Enable memcache session handler support? [yes], simply press ENTER.
Restart services:
If using php-fpm:
$ sudo service php-fpm reload
If using Apache:
$ sudo systemctl restart httpd
Testing Memcached
Check that the service is running:
$ echo "stats settings" | nc localhost 11211
Check the PHP extension by creating a PHP info file:
$ echo "<?php phpinfo(); ?>" > /var/www/html/php_info.php
Configuring the Firewall for Memcached
Add rules to allow connections:
$ sudo 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
$ sudo 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
Verify that the Memcached daemon is running:
$ ps -aux | grep memcached