Memcached Installation Guide on CentOS

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 http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
- 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
PORT="11211"
USER="memcached"
MAXCONN="10240"
CACHESIZE="128"
OPTIONS="-l 127.0.0.1 -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/hoangnguyenmedia.net/private_html/:
cd /home/hoangnguyenmedia.net/private_html/
wget https://github.com/elijaa/phpmemcachedadmin/archive/1.3.0.tar.gz
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]
session.save_handler = memcached
session.save_path = "127.0.0.1:11211"
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 session.inc.php of phpMyAdmin:
nano /home/domain.com/private_html/phpmyadmin/libraries/session.inc.php
_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!

Nhận xét