In this tutorial we will cover the steps needed to install Memcache on your CentOS 6.4, Debian or Ubuntu platform. Memcached is a high-performance, distributed memory object caching server. It can store the content of the whole database table and query in the memory and this way drastically increase the performance of your website. Memcache is being used by some of the most popular websites such as Facebook and YouTube and it is supported by the popular content management systems such as Joomla, WordPress, Drupal etc.

When a query is executed towards the database Memcache is trying to retrieve the result from the cache. If there is a cache record related to that query the result is being returned directly without querying the database itself which speeds up the process. Otherwise the information is retrieved from the database and the results stored inside Memcache.

Requirements

  • CentOS 6.4, Debian or Ubuntu installed on your computer/server
  • SSH access (Command line access to the server)
  • root privileges
  • Basic skills for working on a Linux environment

Install Memcache

In order to install the Memcache service we will use the default package manager included inside the corresponding Linux distribution:

CentOS 6.4

sudo yum install memcached

Debian/Ubuntu

sudo apt-get install memcached

You will be prompted to confirm the installation. During the process additional packages required by the Memcache service can be installed as well.

Configure Memcache

Once the installation is completed we can go ahead and configure the service.

CentOS 6.4
The default Memcache configuration file in CentOS 6.4 is /etc/sysconfig/memcached:

Here is a sample content of that file and short explanation about the available variables:

When you are ready with the configuration you can start the service:

Debian/Ubuntu
The default Memcache configuration file in CentOS 6.4 is /etc/memcached.conf:

Inside the file you can set Memcache port, user number of maximum allowed simultaneous connections, maximum amount of memory which will be used for object storage etc. There is short explanation included for all available options.

In Debian/Ubuntu the Memcache service might be automatically started after the installation so in case of any changes made to the configuration the service should be restarted:

You can use the following commands in order to start, stop or restart the service:

Test the functionality of the Memcache service

You can use the following commands to verify that the service is running:

where 11211 is the port with which the service is configured.

The memcached-tool will ouput statistics about the Memcache service:

Install Memcache PECL extension

By default Memcache is not configured to work with PHP so we need to install extension in order for both services to be able to communicate:

CentOS 6.4

Debian/Ubuntu

Once installed you should add the extension inside the php.ini file using the following line:

You can easily check the location of the php.ini file using the command:

Start Up Memcache automatically on server boot

In order for the service to start automatically upon server boot up you can execute the following command:

CentOS 6.4

Debian/Ubuntu