What is Memcached? Quoting directly from Memcached.org:
Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
Memcached is FREE and yet has a capability to reduce server loads and improve websites’ performances. So, there is a huge benefit to have Memcached installed onto a server, and then write necessary code for your web applications to take advantage of Memcached. In this post, I’ll briefly explain and provide some instructions for you to install Memcached onto your servers.
- Assume you install Memcached as root, so let change into a root directory! Execute the command [cd /root].
- In root directory, make a new, temporary directory with whatever name you like to hold the necessary packages and dependencies so Memcached can be installed. For an example, make a new, temporary directory by executing this command [mkdir mynewtemp].
- At this point in time, Memcached needs LibEvent to be installed before Memcached could be installed successfully. You need to go to LibEvent’s official website to download its latest package, and then you need to put latest package of LibEvent into /root/mynewtemp directory. Let assume you had done that already, and so we simulate how you would go about installing LibEvent.
- tar xzvf libevent-latestversion.tar.gz
- cd libevent-latestversion
- ./configure; make; make install
- It’s now time to download Memcached and install it onto your server. Let execute the command [cd /root/mynewtemp].
- You need to download latest version of Memcached from its official website and put it into your /root/mynewtemp directory.
- tar xzvf memcached-latestversion.tar.gz
- cd memcached-latestversion
- ./configure –with-lib-event=/usr/local/; make; make install
- Let try to start Memcached right now by executing the command [memcached]. If you don’t see any error, then you don’t have to worry about anything. If you see this error: (memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory)
- We can fix this error! Perhaps you’re using 64 bit machine, and so the installation of LibEvent had not registered correctly with Memcached. Let fix this problem by registering LibEvent with Memcached using symbolic link. Execute the command [ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5].
- Let start Memcached again to see if the error is fix! Execute the command [memcached -d -u nobody]. By now your error should be fix. If not, please Google for help or ask another expert!
- Since I’m familiar with WHM/Cpanel, I’ll use only WHM/Cpanel way of loading Memcached module into PHP. Without loading Memcached module into PHP, you basically cannot use PHP with Memcached. Let us begin by log into WHM, go to Software >> Module Installers. Let pick PHP PECL >> Manage. Try to search for Memcached and then install the available package of Memcached module. If there is no error, then you’re almost done. WHM spits error in the installation process of a Memcached module for PHP?Perhaps, it’s a bug of WHM, and we need a work around for this! Let work around the problem by doing a symbolic link as below:
- ln -s /usr/lib/php/extensions/no-debug-non-zts-20090626/memcache.so /usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
- chmod 0755 /usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
- restarting Apache to have PHP works with Memcached module.
- Let edit /etc/rc.local so Memcached will start on boot!
- vim /etc/rc.local
- add this line into rc.local file on its own line memcached -d -u nobody&
- save the file /etc/rc.local and exit it.
The command that I mention of for you to start Memcached and add to /etc/rc.local is very general. You can further customize the command to make Memcached works better for your server since each server has different specifications and customizations such as amount of memory (i.e., RAM). You can always execute command [man memcached] in terminal to see more information on how to use Memcached effectively.
Disclaimer: Please carefully considering the risks of installing any new software onto your production servers. Also, you must do your own research in addition in following the tips in this post. Without understanding the risks and the technicality of installing something as Memcached onto your server, and without knowing why you even need something as Memcached, then it’s you to be blamed for crashing a server or servers of yours! This post is no mean of soliciting Memcached, but it’s merely a brief tutorial or how-to for working with Memcached!