JavaScript source code that changes English language to another another language easily | vk
 


 

Memcached Installation and Configuration with PHP on Debian server



In a previous post I wrote about Memcached installation on Archlinux workstation. This is a similar post for Debian server machine.

I will describe Memcached installation and configuration on a) a Debian 6 (Squeeze), the current Debian stable and b) Debian 7 RC1 (Wheezy) with systemd instead of initscripts. Concerning second case (Debian Wheezy), I have to say that systemd on Wheezy is not exactly native systemd as on Arch, as Debian's systemd still honors /etc/rc.d/. This is not necessarily bad. Debian is progressing slowly but steadily. Stability is the top priority (thankfully!).

Please, note that Debian 7 RC1 is not recommended for production use. May 5, 2013 announced as release date for Debian 7 (Wheezy).

Additionally, I suppose that this guide could be also used on Ubuntu or other Debian based Linux distributions.

Memcached installation

Using apt-get:

sudo apt-get install memcached

This is the result on Debian Squeeze:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  libcache-memcached-perl libmemcached
The following NEW packages will be installed:
  memcached
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 75.5 kB of archives.
After this operation, 188 kB of additional disk space will be used.
Get:1 http://ftp.gr.debian.org/debian/ squeeze/main memcached amd64 1.4.5-1 [75.5 kB]
Fetched 75.5 kB in 0s (737 kB/s)
Selecting previously deselected package memcached.
(Reading database ... 61684 files and directories currently installed.)
Unpacking memcached (from .../memcached_1.4.5-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up memcached (1.4.5-1) ...
Starting memcached: memcached.

Similar results on Debian Wheezy:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
libcache-memcached-perl libmemcached
The following NEW packages will be installed:
memcached
0 upgraded, 1 newly installed, 0 to remove and 43 not upgraded.
Need to get 86.7 kB of archives.
After this operation, 226 kB of additional disk space will be used.
Get:1 http://ftp.gr.debian.org/debian/ wheezy/main memcached amd64 1.4.13-0.2 [86.7 kB]
Fetched 86.7 kB in 0s (563 kB/s)
Selecting previously unselected package memcached.
(Reading database ... 64577 files and directories currently installed.)
Unpacking memcached (from .../memcached_1.4.13-0.2_amd64.deb) ...
Processing triggers for man-db ...
Setting up memcached (1.4.13-0.2) ...
[ ok ] Starting memcached (via systemctl): 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 (as usual in Debian)

Just for reference

On Debian Squeeze:

service memcached start

Using systemd on Debian Wheezy:

systemctl start memcached.service

Test Memcached is running

Using ps:

ps -eaf | grep memcached

the result is:

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

or using netstat:

netstat -tap | grep memcached

the result is:

tcp        0      0 localhost:11211         *:*                     LISTEN      2174/memcached

Inspecting Running Configuration

Using netcat:

echo "stats settings" | nc localhost 11211

the result is:

STAT maxbytes 67108864
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
END

Similar results on Debian Wheezy:

STAT maxbytes 67108864
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT num_threads_per_udp 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
STAT maxconns_fast no
STAT hashpower_init 0
STAT slab_reassign no
STAT slab_automove no
END

Memcached configuration

Memcached configuration file is /etc/memcached.conf

nano /etc/memcached.conf

By default it contains

# memcached default config file
# 2003 - Jay Bonci <jaybonci@debian.org>
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution.

# 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

Default Memcached settings (on Debian) are

  • user for Memcached server: nobody
  • -l 127.0.0.1 listen to localhost
  • number of threads to use: 4 (default) (-t 4 which is omitted)
  • default port is TCP 11211 (-p 11211)
  • 64 MB RAM assigned to Memcached (-m 64). This is the minimum RAM for Memcached

Then, modify /etc/memcached.conf according to your preferences.

Documentation is available here. See all availables options, using

memcached -h

So, to assign 128 MB RAM to Memcached

nano /etc/memcached.conf

modify -m 64 to -m 128

and restart memcached service

On Debian Squeeze:

service memcached restart

Using systemd on Debian Wheezy:

systemctl restart memcached.service

Now

echo "stats settings" | nc localhost 11211

returns the following results

STAT maxbytes 134217728
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
END

PHP memcached extension

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

On Debian Squeeze:

apt-get install php5-memcached

the result is:

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

service apache2 restart

phpinfo (PHP Version 5.3.3-7+squeeze15) must contain something like this:

On Debian Wheezy:

apt-get install php5-memcached

the result is:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libmemcached10
The following NEW packages will be installed:
  libmemcached10 php5-memcached
0 upgraded, 2 newly installed, 0 to remove and 43 not upgraded.
Need to get 157 kB of archives.
After this operation, 430 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://ftp.gr.debian.org/debian/ wheezy/main libmemcached10 amd64 1.0.8-1 [117 kB]
Get:2 http://ftp.gr.debian.org/debian/ wheezy/main php5-memcached amd64 2.0.1-6 [40.1 kB]
Fetched 157 kB in 0s (784 kB/s)
Selecting previously unselected package libmemcached10:amd64.
(Reading database ... 64596 files and directories currently installed.)
Unpacking libmemcached10:amd64 (from .../libmemcached10_1.0.8-1_amd64.deb) ...
Selecting previously unselected package php5-memcached.
Unpacking php5-memcached (from .../php5-memcached_2.0.1-6_amd64.deb) ...
Setting up libmemcached10:amd64 (1.0.8-1) ...
Setting up php5-memcached (2.0.1-6) ...

After this step, a symbolic links has been created inside /etc/php5/cond.d/, points to

20-memcached.ini -> ../mods-available/memcached.ini

The contents of memcached.ini, in order to enable memcached extension, are

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

So, just restart Apache. Using systemd on Debian Wheezy:

systemctl restart apache2.service

phpinfo (PHP Version 5.4.4-14) must contain something like this:

Simple 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");
}
?>

Remarks

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.

There is an interesting fork of Memcached, named Memcached-dd, which can dump Memcached RAM data to disk and restore them from here. Take a look here.

Related Posts

You may also be interested in

Your comments are welcomed!

Εγγραφή!
Sign-up for our free email newsletter. Get updates when new tutorials and tips are published. You can unsubscribe anytime with a click.

Your comments are welcomed!

This site actively encourages commenting on any post. Comments are not pre-moderated, but this community does not tolerate direct or indirect attacks, name-calling or insults. Please, read terms of use and Comment Policy at privacy policy.

Memcached Installation and Configuration with PHP on Arch Linux Debian 7 Wheezy Dedicated Web Server Setup Step by Step