How to install Memcached on CentOS?

Memcached is a cache system that works by temporarily storing the data you use in RAM. Because memory read / write speeds are always higher than file storage, the use of memcached speeds up your system dramatically.

The Memcached operation model:

In this post, 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)

– Install Memcached package

Note: There are two memcache packages available that are memcache and memcached, as well as two versions of the PHP Extensions Module, php-pecl-memcache and php-pecl-memcached. We will use the second version because it is stable and supports more functions.

Memcached configuration

The most important parameter you should keep in mind is CACHESIZE, which is MB. For example, below I use 128MB to cache (default 64MB). OPTIONS configures security only for local connect to port 11211.

Start Memcached

To track the status of memcached, you may be able to use phpMemcachedAdmin to view it right in your browser. PHPMemcachedAdmin download only is usable. Specifically, I will install in /home/coupontree.co/private_html/:

You can now access PHPMemcachedAdmin via the domain:port/memcached/

Note: On the first visit, you will see “Error: Configuration file or folder is missing, please fix this error and try again“. You go to Editing Configuration and then Save Live Configuration is done.

Install Memcache and Memcached PHP Module

For Memcached to work with PHP, we need to install the PHP module.

PHP 5.5

PHP 5.6

PHP 7.0

PHP 7.1

Finally restart PHP and web server

Open the Memcached port (11211) on the Iptables Firewall

In case you use a dedicated server to run Memcached, you need to open port 11211 on the server cache.

Use the following command

Restart Iptables Firewall

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.

Open the php.ini configuration file

Find the [Session] line, replacing session.save_handler = files with:

Press Ctrl + O to save the file, Ctrl + X to close the editor.

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

_Uncomment:leave the// in line: //ini_set(‘session.save_handler ‘,’ files’);
_Add lines: ini_set (‘session.save_path’, ‘/tmp’);

Finally restart PHP

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.

Delete Memcached

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

Good luck!!!

Leave a Reply

Your email address will not be published. Required fields are marked *