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:
1
|
sudo nano /etc/sysconfig/memcached
|
Here is a sample content of that file and a short explanation about the available variables:
1
2
3
4
5
6
7
8
9
10
11
12
|
PORT=“11211”
USER=“memcached”
MAXCONN=“1024”
CACHESIZE=“64”
OPTIONS=“”
PORT – the port which will be used by the Memcache service
USER – the service daemon will be started with that username
MAXCONN – the number of maximum allowed simultaneous connections.
CACHESIZE – the maximum amount of memory which will be used for object storage.
OPTIONS – additional options, i.e. “–l 127.0.0.1”. This way other services installed on the server such as Apache or nginx can connect to Memcache.
|
When you are ready with the configuration you can start the service:
1
|
sudo /etc/init.d/memcached start
|
Debian/Ubuntu
The default Memcache configuration file in CentOS 6.4 is /etc/memcached.conf:
1
|
sudo nano /etc/memcached.conf
|
Inside the file you can set Memcache port, user number of maximum allowed simultaneous connections, the 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:
1
|
sudo /etc/init.d/memcached restart
|
You can use the following commands in order to start, stop or restart the service:
1
2
3
|
sudo /etc/init.d/memcached start
sudo /etc/init.d/memcached stop
sudo /etc/init.d/memcached restart
|
Test the functionality of the Memcache service
You can use the following commands to verify that the service is running:
1
|
sudo netstat –tupan | grep 11211
|
where 11211 is the port with which the service is configured.
The memcached-tool will ouput statistics about the Memcache service:
1
|
sudo memcached–tool localhost:11211 stats
|
Install Memcache PECL extension
By default Memcache is not configured to work with PHP so we need to install an extension in order for both services to be able to communicate:
CentOS 6.4
1
|
sudo yum install php–pecl–memcache
|
Debian/Ubuntu
1
2
|
sudo apt–get install php5–dev php–pear
sudo pecl install memcache
|
Once installed you should add the extension inside the php.ini file using the following line:
1
|
extension=memcache.so
|
You can easily check the location of the php.ini file using the command:
1
|
php –i | grep –i “Loaded Configuration File”
|
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
1
|
sudo chkconfig memcached on
|
Debian/Ubuntu
1
|
sudo update–rc.d memcached defaults
|