Memcached is a distributed, high-performance, in-memory caching system that is used to speed up your sites that make heavy use of databases. Memcached is a caching system that works by temporarily storing information in memory that would usually be retrieved from a databases.
If your websites is growing and you see an increase in traffic, one of the components that shows stress the fastest is the back-end database. If your database is not configured to handle high load it can easily be overwhelmed by relatively modest increase in traffic.
In this article I will describe how you can install memcached and configure on Ubuntu and CentOS.
Installing Memcached On Ubuntu
To install memcached on Ubuntu first you will need to update apt with the following command:
$ sudo apt-get update
Once Updated completed, follow the below command to install memcached and all of its necessary dependencies:
$ sudo apt-get install php-memcached memcached
Above one command will install everything you need. Now restart the memcached service.
$ /etc/init.d/memcached start
Installing Memcached On CentOS
To install memcached on CentOS machine, first you will need to run yum update command than install it using following commands:
# yum clean all # yum update -y # yum install memcached php-memcached -y
Now restart memcached service.
# service memcached start
To enable the memcached service to start boot time you will need to run below command:
# chkconfig memcached on
Verify the Installation
You can verify the memcached installation with different ways.
First way, you can ask PHP if it knows about your memcached extension and whether it is enable or not. To do this you will need to create a PHP info page. You will need to create a file called phpinfo.php in your document root directory. In Apache on Ubuntu default document root is /var/www/html/.
# vim /var/www/html/phpinfo.php
In this file, type this out. This basically just calls a PHP function that collects and prints information about our server into a web-friendly layout.
<?php phpinfo(); ?>
Now, access this page in your web browser with your server’s domain name or public IP address followed by /phpinfo.php.
Now search for the “memcached” section header and you will get something that looks like this:
This means that the memcached extension is enabled and being found by the web server.
You can also check whether the memcached service is running or not typing below command:
# ps aux | grep memcached memcache 6586 0.0 0.0 327448 3004 ? Sl 14:07 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 demouser 6636 0.0 0.0 11744 904 pts/0 S+ 14:29 0:00 grep --color=auto memcached
You can also query the service for stats by typing:
echo "stats settings" | nc localhost 11211
You can follow the below command to stop, start or restart memcached service.
# service memcached stop # service memcached start # service memcached restart