Installation and Configuration of Memcached with PHP on Ubuntu server

What is Memcache ?

Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.Php provides memcache module to work with memcached so here we configure Memcached with php.

The Memcache module also provides a session handler (memcache).

Installation of Memcached with PHP

We hereby install memcached through apt-get

sudo apt-get install memcached

This will automatically start memcached service.

Enable Memcached to run on system start-up and start service

After setup, memcached service enabled to run on start-up and is starting.

service memcached start

Now we can test the service if running on server –

ps -eaf | grep memcached

which will show in result –

nobody    2174     1  0 22:26 pts/0    00:00:00 /usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1
root      2227  1697  0 22:32 pts/0    00:00:00 grep memcached

Memcached configuration

Memcached configuration file is /etc/memcached.conf

vi /etc/memcached.conf
# memcached default config file
# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d
 
# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log
 
# Be verbose
# -v
 
# Be even more verbose (print client commands as well)
# -vv
 
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 64
 
# Default connection port is 11211
-p 11211
 
# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u nobody
 
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 127.0.0.1
 
# Limit the number of simultaneous incoming connections. The daemon default is 1024
# -c 1024
 
# Lock down all paged memory. Consult with the README and homepage before you do this
# -k
 
# Return error when memory is exhausted (rather than removing items)
# -M
 
# Maximize core file limit
# -r

PHP memcached extension

There are two php extensions for Memcached: php-memcache and php-memcached. The second (php-memcached) is newer and probably preferable.

apt-get install php5-memcached

Which result in

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libmemcached5
The following NEW packages will be installed:
  libmemcached5 php5-memcached
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 78.6 kB of archives.
After this operation, 303 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://ftp.gr.debian.org/debian/ squeeze/main libmemcached5 amd64 0.40-1 [50.9 kB]
Get:2 http://ftp.gr.debian.org/debian/ squeeze/main php5-memcached amd64 1.0.2-1+squeeze2 [27.7 kB]
Fetched 78.6 kB in 0s (376 kB/s)
Selecting previously deselected package libmemcached5.
(Reading database ... 61703 files and directories currently installed.)
Unpacking libmemcached5 (from .../libmemcached5_0.40-1_amd64.deb) ...
Selecting previously deselected package php5-memcached.
Unpacking php5-memcached (from .../php5-memcached_1.0.2-1+squeeze2_amd64.deb) ...
Setting up libmemcached5 (0.40-1) ...
Setting up php5-memcached (1.0.2-1+squeeze2) ...
 
Creating config file /etc/php5/conf.d/memcached.ini with new version

As mentioned, /etc/php5/conf.d/memcached.ini has been created

; uncomment the next line to enable the module
extension=memcached.so

Then, just restart Apache for load this module/extension

service apache2 restart

Test php script

This is a simple script to test basic Memcached functionality with PHP:

<?php
$mc = new Memcached();
$mc->addServer("127.0.0.1", 11211);
 
$result = $mc->get("test_key");
 
if($result) {
  echo $result;
} else {
  echo "No data on Cache. Please refresh page pressing F5";
  $mc->set("test_key", "test data pulled from Cache!") or die ("Failed to save data at Memcached server");
}
?>

Notes-

Restarting Apache web server will not affect data stored in Memcached memory.

Restarting Memcached server (moreover, on system restart) all data stored in Memcached memory will be lost.

 

 

 

 

Balvinder Singh

Hello, I am Balvinder Singh - DevOps Engineer with 2.5+ year of working experience with different server environments. Tag Line:-Linux | AWS| WHM |Monitoring | Virtualization | Optimization | Performance | Security | Release & Deployment. I love helping companies / clients to deploy their code / applicateions to well managed, optimized, secure server and can go extra mile to satisfy.

You may also like...

Leave a Reply

Your email address will not be published.