Wondering what the Memcached extension is all about?
It is one of the most useful PHP extension that helps in improving web application speed.
At Bobcares, we often receive requests regarding Memcached installation and other queries as a part of our Server Management Services.
Today, let’s see how our Support Engineers install the Memcached PHP extension in the server.
What is Memcached extension?
Memcached is a high performance distributed memory caching system. And it is mainly used to speed up web applications by reducing the database load.
Memcached does this by storing the data objects in dynamic memory, due to which whenever a request is sent to the database, Memcached renders the contents stored within it. As a result, it speeds up the web application.
How does Memcached extension work?
Each item in Memcached includes key, expiration time, and raw data. Below is the working of Memcached.
1. When a client requests for data, the Memcached checks to see if it is available in its cache.
2. Here, there are two possibilities:
- If the data is available in the cache, then it will return the data from the Memcached and there is no need to check the database to fetch the contents.
- If the data is not available in the cache, it fetches the database and retrieves the data. And in this retrieval process, it saves a copy in the cache for future use.
3. Every time, there are any changes made in the contents, or the expiry value of the item expires, the Memcached updates its contents in the cache as well. By this, it ensures that the fresh updated contents deliver to the clients.
For the working of Memcached, it requires a PHP extension. This requires manual installation of Memcached extension.
Installation of Memcached extension
The installation steps of Memcached extension vary according to the type of the server. Let’s now see how our Support Engineers install the Memcache extension.
In Centos
For instance, the below commands help to install Memcached in CentOS servers.
yum -y install php-pecl-memcache
We then restart Memcached and httpd:
systemctl restart memcached
systemctl restart httpd
Moreover, to view information on the installed PHP modules:
php -m | grep memcache
In a similar way, we confirm the presence of the module from the phpinfo page on the website too.
In Ubuntu
Similarly, in Ubuntu servers, our Support Engineers install Memcached with a series of commands.
sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install zlib1g-dev
sudo apt-get -y install libmemcached-dev
sudo pecl7.X-sp install memcached
After running the above commands, there will be a prompt shown as below
libmemcached directory [no] :
Here, we set the directory as per the requirement. Then we add a line into it
no --disable-memcached-sasl
Finally, this is how it displays when we do not set a libememcached directory :
libmemcached directory [no] : no --disable-memcached-sasl
Further, after installation, we create a configuration file for the extension. Then we restart PHP using the below commands as root.
sudo bash -c "echo extension=memcached.so > /etc/php7.X-sp/conf.d/memcached.ini"
sudo service php7.X-fpm-sp restart
To verify the Memcached extension installation, we check by running this command:
php -i | grep -i "memcached support"
Memcached extensions install errors
The steps to install Memcached extension appears rather straight-forward. However, customers often face errors with Memcached enabling. A recent error looked like:
PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20160303/memcached.so' - Error loading shared library libmemcached.so.11: No su ch file or directory (needed by /usr/local/lib/php/extensions/no-debug-non-zts-20160303/memcached.so) in Unknown on line 0
Here, our Dedicated Engineers checked the server. We found that the problem happened due to missing package libmemcached-dev. Therefore, to fix the error, we added the libmemcached-dev library separately.
[Still having trouble with Memcache installation? – We’ll help you.]
Conclusion
In short, the Memcached extension stores the cache and increase the web application speed. Today, we saw how our Support Engineers install Memcached.
0 Comments