Memcached Installation Guide on CentOS

by johnsmith

Memcached is a cache system that works by temporarily storing the data you usually use in RAM. Because memory read speed is always higher than file storage, the use of memcached makes your system speed up significantly.

Memcached operation model:

Memcached Installation Guide on CentOS

In this article, I will show you how to install Memcached on CentOS 7/6/5.

Install Memcached
– Install Remi repository with CentOS 5 (CentOS 7 and 6 do not need this step)

## Remi Dependency on CentOS 5
rpm -Uvh
rpm -Uvh

– Install Memcached package:

## CentOS 7 and 6
yum install memcached

## CentOS 5
yum --enablerepo=remi install memcached

Note that two memcache packages exist: memcache and memcached (with the d at the bottom), as well as two versions of the PHP Extensions Module, php-pecl-memcache and php-pecl-memcached. We will use the second version (with the letter d) because it is stable and supports more functions.

Configure Memcached
The most important parameter to keep in mind is CACHESIZE, MB and MAXCONN. For example, underneath you use 128MB to cache (default 64MB). OPTIONS configures security only for local connect to port 11211.

# nano /etc/sysconfig/memcached
OPTIONS="-l -U 0"

Starts Memcached

chkconfig memcached on
service memcached start

To track the memcached status, you may be able to use phpMemcachedAdmin to view it right in your browser. PHPMemcachedAdmin is just downloadable, should be installed in the management directory of HocVPS Script for security. Specifically, I will install in the directory /home/

cd /home/
tar -xvzf 1.3.0.tar.gz
mv phpmemcachedadmin-1.3.0 memcached && chmod -R 777 memcached && chown -R nginx:nginx memcached
rm -f 1.3.0.tar.gz

Now you can access PHPMemcachedAdmin via domain:port/memcached/

Note: At the first time you will see the message “Error: Configuration file or folder is missing, please fix this error and try again”. You need to select Editing Configuration then Save Live Configuration to finish.
Install Memcache and Memcached PHP Module:
For Memcached to work with PHP, we need to install the PHP module.

PHP 5.5
yum --enablerepo=remi,remi-php55 install php-pecl-memcached php-pecl-memcache
PHP 5.6
yum --enablerepo=remi,remi-php56 install php-pecl-memcached php-pecl-memcache
PHP 7.0
yum --enablerepo=remi,remi-php70 install php-pecl-memcached php-pecl-memcache
PHP 7.1
yum --enablerepo=remi,remi-php71 install php-pecl-memcached php-pecl-memcache
Then restart PHP and web server
service php-fpm restart
service nginx restart

Open the Memcached port (11211) on the Iptables Firewall.
In case you use a separate server to run Memcached, you need to open port 11211 on the server cache.

Use the following command:

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
Restart Iptables Firewall
service iptables restart

Install Memcached as Session Handler

You can configure PHP to use Memcached as a session handler, instead of using files, to improve the performance of your system. Of course this should only be done with source code that uses multiple sessions, no need for WordPress.

Open file php.ini
nano /etc/php.ini
Find [Session], replace session.save_handler = files by:
session.save_handler = memcached
session.save_path = ""
Press Ctrl+O to save file, Ctrl+X to close editor.

If using phpMyAdmin, you need to customize the application’s session storage mechanism. Edit file of phpMyAdmin:

nano /home/
_Uncomment (remove //) line: //ini_set('session.save_handler', 'files');
_Add line : ini_set('session.save_path', '/tmp');
End, restart PHP again:
service php-fpm restart

Install Memcached works with WordPress.

For Memcached to work with WordPress you need to use the W3 Total Cache plugin.

In the plugin settings section, select the page cache method Memcached for the Page Cache, Database Cache, and Object Cache modules.

Now, enjoy the speed of Memcache.

Remove Memcached

If you do not use anymore and want to completely remove Memcached from the server, run the following command:

yum remove memcached php-pecl-memcached php-pecl-memcache
service php-fpm restart
service nginx restart
Good luck!

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More