Another website accelerator that caches MySQL queries, and PHP executions is called Memcache. Memcache decreases the load on the server, enabling you to deliver the pages to your end-users more quickly because it doesn't need to calculate queries from the database anymore, but fetches it from the memory cache instead.
Think of it this way, if you're trying to remember a particular piece of info with facts coming from different sources, it's hard to find things if you haven't written it down somewhere. People easily forget much of their vast knowledge until they remember an idea about that topic. Memcache works almost like a traditional notebook. It allows the server to store all of the information in one spot, eliminating the need to search the entire database to execute an action because the action is already in the server's memory.
Steps to add Memcache to your server (mine runs on CentOS) are:
# yum install memcache
- Depending on how much RAM your server contains, the amount of memory allocated should be at least ¼ of it. For example, if your server RAM is 4GB, then allocate 1GB of memory for memcache.
- Make changes in the config file at /etc/sysconfig/memcached and include these info:
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="1024"
OPTIONS="-l 127.0.0.1"- CACHESIZE is the allocated memory where 1024 is 1GB.
- The OPTIONS setting is to make sure memcache only listens to the localhost port in your server to avoid caching unnecessary data.
- To make your memcache more secure, add this line to your IP tables:
-A INPUT -p tcp --dport 11211 -j ACCEPT
- Once you've made changes to the setting, start up memcache by:
# service memcached start
# chkconfig memcached on - Hold up, we are not done yet. This is the part that I usually forget when installing memcache. We now need to install the Memcache PECL Extension for PHP.
# pecl install memcache - Make changes to your php.ini at /etc/php.ini or /usr/local/lib/php.ini and add these info:
[memcache]
extension=memcache.so
memcache.hash_strategy=consistent - Restart Apache to apply the changes to your server:
# /etc/init.d/httpd restart
- To verify memcache is working:
# ps aux | grep memcached
To add memcache to Drupal:
- Put site to offline mode.
- Drush and install memcache https://drupal.org/project/memcache.
- Go to your site's settings.php and add:
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['memcache_key_prefix'] = 'something_unique'; - Save, enable the memcache module and put your site back online.
To add memcache to WordPress:
- Download http://wordpress.org/plugins/memcached/installation/.
- Copy object-cache.php to /wp-content directory.
These should be it for both Drupal and WordPress. Watch your websites load much faster than before.