Memcached is a distributed memory caching technology that allows users to cache data inside Linux RAM memory. It’s the fastest way to cache objects and data, as RAM memory is way faster than Disk cache systems, which relay on Disk speed, which is always slower than RAM speed. That’s why today we will show you how to Install Memcached and PHP Memcache in cPanel based servers.
Web Hosting providers are also always worried about having a great uptime and keep their servers with a very low load average, with enough system resources to speed up websites, in other words, to keep the service stable and their customers happy.
One of the key points to have a great website speed is to use a cache system. So, before we go into the tutorial ‘install memcached and PHP memcache in cPanel’ tutorial, we will try to answer the question about what is a cache and the types of cache.
What is Cache?
Cache is a special high-speed storage technology that allow system users to access data way faster than traditional ways of acces direct data from the source to the final destination (ej. first time you visit a website). Cache system can use a reserved section of the system’s RAM memory, or a storage device or section of the system disk to store the cached data.
Types of Cache
The two main types of caching in personal computers and servers are: memory caching and disk caching.
Memory caching is faster than disk caching, but you need more RAM memory available for this purpose, so the amount of data cached will be limited to the amount of RAM memory that you can assign to the cache.
Disk caching is slower, and even on SSD fast disks, it still slower than RAM memory caching. But, it can store much more data than RAM memory without need to upgrade your hardware.
On the next tutorial, we will show you how to install memcached and PHP memcache on cPanel servers, so you can use Linux RAM memory to speed up your websites. But first, let’s see what is the difference between Memcached and Memcache (or PHP Memcache).
What’s the difference between Memcached & Memcache?
Memcached is the system daemon, that runs on your Linux server, the one in charge of storing the data in your RAM memory. Memcached can be configured to store certain amount of RAM memory, to allow X number of incoming connections, and other general daemon parameters from the server side point of view.
Memcache, also called PHP Memcache, is a PHP Module that allows you to use Memcached caching mechanism, inside your PHP developments. In order to speed up your website with memcached on cPanel, you will need both things (memcached system daemon and memcache php module).
Install Memcached and PHP Memcache in cPanel
Let’s install Memcached System Daemon on cPanel
yum install memcached
or
dnf install memcached
Add memcached to boot with your system start:
chkconfig memcached on
Start the Memcached service:
service memcached start
[email protected] [~]# service memcached start Starting memcached: [ OK ]
Install PHP Memcache using Pecl
Let’s use PEAR’s Pecl utility to install Memcache on cPanel
pecl install memcache
If that doens’t work, you get do it manually after all:
wget http://pecl.php.net/get/memcache tar -xvpzf memcache cd memcache-3.0.8 phpize
You will probably get something like this:
Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226
Compile and install Memcache
./configure make make install
Make sure PHP Memcache module is inside your php.ini file, if not you can add it using this command:
echo "extension=memcache.so" >> /usr/local/lib/php.ini
Restar HTTP daemon to apply changes
service httpd restart
How can I check if Memcache is installed?
Now check if it is loaded from PHP configuration using this command:
php -i | grep memcache
The output should be something like:
[[email protected]:~]php -i | grep memcache memcache memcache support => enabled memcache.allow_failover => 1 => 1 memcache.chunk_size => 8192 => 8192 memcache.default_port => 11211 => 11211 memcache.default_timeout_ms => 1000 => 1000 memcache.hash_function => crc32 => crc32 memcache.hash_strategy => standard => standard memcache.max_failover_attempts => 20 => 20 Registered save handlers => files user memcache [[email protected]:~]
How can I configure Memcached?
Memcached can be configured from /etc/sysconfig/memcached
There you can set daemon port, memory limits, as well as connection limits, as you see below:
[[email protected]:~]cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="1024" OPTIONS="-l 127.0.0.1"
Explanation of each variable:
PORT = Is the port where memcahed is listening, don’t change it unless necessary.
USER = Is the system user assigned, almost never will need to change it.
MAXCONN = Max number of connections allowed to use memcached.
CACHESIZE = The number of megabytes to use as RAM cache storage. If you have lot of free RAM, you can increase the limits.
OPTIONS “-l 127.0.0.1” = The network address where the daemon is running, only allow localhost by default.
Once you finish editing the file, you need to restart Memcached to apply changes:
service memcached restart
Conclusion
As you see, you can install memcached and php memcache in cPanel servers easily, it takes only a few minutes and you will be ready to use memcached fantastic speed to store your cache data inside Linux RAM memory. Remember to configure Memcached on cPanel, so you can specify the exact amount of RAM and incoming connections available for your users.