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)
1 2 3 |
## 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
1 2 3 4 5 |
## CentOS 7 and 6 yum install memcached ## CentOS 5 yum --enablerepo=remi install memcached |
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.
1 2 3 4 5 6 7 |
nano /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="10240" CACHESIZE="128" OPTIONS="-l 127.0.0.1" |
Start Memcached
1 2 |
chkconfig memcached on service memcached start |
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/:
1 2 3 4 5 |
cd /home/coupontree.co/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/Temp/ memcached/Config/ rm -f 1.3.0.tar.gz |
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
1 |
yum --enablerepo=remi,remi-php55 install php-pecl-memcached php-pecl-memcache |
PHP 5.6
1 |
yum --enablerepo=remi,remi-php56 install php-pecl-memcached php-pecl-memcache |
PHP 7.0
1 |
yum --enablerepo=remi,remi-php70 install php-pecl-memcached php-pecl-memcache |
PHP 7.1
1 |
yum --enablerepo=remi,remi-php71 install php-pecl-memcached php-pecl-memcache |
Finally restart PHP and web server
1 2 |
service php-fpm restart service nginx restart |
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
1 |
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT |
Restart Iptables Firewall
1 |
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.
Open the php.ini configuration file
1 |
nano /etc/php.ini |
Find the [Session] line, replacing session.save_handler = files with:
1 2 3 |
[Session] session.save_handler = memcached session.save_path = "127.0.0.1:11211" |
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:
1 |
nano /home/main_domain/private_html/phpmyadmin/libraries/session.inc.php |
_Uncomment:leave the// in line: //ini_set(‘session.save_handler ‘,’ files’);
_Add lines: ini_set (‘session.save_path’, ‘/tmp’);
Finally restart PHP
1 |
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.
Delete Memcached
If you do not use anymore and want to completely remove Memcached from the server, run the following command:
1 2 3 |
yum remove memcached php-pecl-memcached php-pecl-memcache service php-fpm restart service nginx restart |
Good luck!!!