Sunday, March 1, 2015

Ubuntu Linux 12.04/14.04 LTS Install Memcached Server For Python and PHP Apps

I'm a new Ubuntu 12.04/14.04 server user. How do I install and use memcached on an Ubuntu Linux 14.04 LTS server? How can I my apps written in PHP or Python can use memcached on an Ubuntu LTS server?

Memcached is a general-purpose distributed memory caching system. It is usually used to speed up dynamic database-driven webapps or websites by caching objects in RAM. It is often result into reducing database load. You need to install the following packages:


  1. memcached - A high-performance memory object caching server
  2. php5-memcache - Memcache extension module for PHP5
  3. php5-memcached - Memcached extension module for PHP5, uses libmemcached
  4. python-memcache - Pure python memcached client

How to install memcached on Ubuntu 14.04

Type the following apt-get command:
 
sudo apt-get update
sudo apt-get install memcached
 
Sample outputs:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libevent-2.0-5 libsasl2-2 libsasl2-modules libsasl2-modules-db
Suggested packages:
  libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql
  libsasl2-modules-gssapi-mit libsasl2-modules-gssapi-heimdal
  libcache-memcached-perl libmemcached
The following NEW packages will be installed:
  libevent-2.0-5 libsasl2-2 libsasl2-modules libsasl2-modules-db memcached
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 328 kB of archives.
After this operation, 1,036 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libsasl2-modules-db amd64 2.1.25.dfsg1-17build1 [14.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libsasl2-2 amd64 2.1.25.dfsg1-17build1 [56.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libevent-2.0-5 amd64 2.0.21-stable-1ubuntu1.14.04.1 [126 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libsasl2-modules amd64 2.1.25.dfsg1-17build1 [64.3 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main memcached amd64 1.4.14-0ubuntu9 [66.7 kB]
Fetched 328 kB in 1s (220 kB/s)
Selecting previously unselected package libsasl2-modules-db:amd64.
(Reading database ... 14575 files and directories currently installed.)
Preparing to unpack .../libsasl2-modules-db_2.1.25.dfsg1-17build1_amd64.deb ...
Unpacking libsasl2-modules-db:amd64 (2.1.25.dfsg1-17build1) ...
Selecting previously unselected package libsasl2-2:amd64.
Preparing to unpack .../libsasl2-2_2.1.25.dfsg1-17build1_amd64.deb ...
Unpacking libsasl2-2:amd64 (2.1.25.dfsg1-17build1) ...
Selecting previously unselected package libevent-2.0-5:amd64.
Preparing to unpack .../libevent-2.0-5_2.0.21-stable-1ubuntu1.14.04.1_amd64.deb ...
Unpacking libevent-2.0-5:amd64 (2.0.21-stable-1ubuntu1.14.04.1) ...
Selecting previously unselected package libsasl2-modules:amd64.
Preparing to unpack .../libsasl2-modules_2.1.25.dfsg1-17build1_amd64.deb ...
Unpacking libsasl2-modules:amd64 (2.1.25.dfsg1-17build1) ...
Selecting previously unselected package memcached.
Preparing to unpack .../memcached_1.4.14-0ubuntu9_amd64.deb ...
Unpacking memcached (1.4.14-0ubuntu9) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libsasl2-modules-db:amd64 (2.1.25.dfsg1-17build1) ...
Setting up libsasl2-2:amd64 (2.1.25.dfsg1-17build1) ...
Setting up libevent-2.0-5:amd64 (2.0.21-stable-1ubuntu1.14.04.1) ...
Setting up libsasl2-modules:amd64 (2.1.25.dfsg1-17build1) ...
Setting up memcached (1.4.14-0ubuntu9) ...
Starting memcached: memcached.
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
Processing triggers for ureadahead (0.100.0-16) ...

How do I start / stop / restart memcached server?

The syntax is as follows:
/etc/init.d/memcached start
/etc/init.d/memcached stop
/etc/init.d/memcached restart
/etc/init.d/memcached force-reload
/etc/init.d/memcached status

Verify that memcached is install and functioning on your server

Use the pgrep command or ps command as follows to find out if memcached is running or not:
pgrep memcached
## or ##
ps aux | grep '[m]emcached'
## or ##
sudo /etc/init.d/memcached status
Sample outputs:
Fig.01: Ubuntu Linux find out if memcached running or not on a server
Fig.01: Ubuntu Linux find out if memcached running or not on a server

Memcached server configuration

You need to edit the file called /etc/memcached.conf, enter:
sudo vi /etc/memcached.conf

Setting up a memory limits

By default memcached will start with a cap of 64 MB of memory. The daemon will grow to this size, but does not start out holding this much memory. In this example, I'm setting up to 2048 MB (2GB):
-m 2048

Set an IP address to listen on

By default memcached will listen on 127.0.0.1 only. You can specify which IP address to listen on. This parameter is one of the only security measures that memcached has, so make sure it's listening on a firewalled interface. I'm going to use 127.0.0.1 only:
-l 127.0.0.1

Limit the number of simultaneous incoming connections

The daemon default is 1024 and can be increased for high trafficked websites:
-c 1024
Save and close the file. Make sure you restart the server:
sudo /etc/init.d/memcached restart
Sample outputs:
Restarting memcached: memcached.

Testing memcached server

Type the following command:
 
echo stats | nc 127.0.0.1 11211
echo "stats settings" | nc localhost 11211
 
Sample outputs:
STAT maxbytes 1073741824
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 0
END
You can use memcached storage/caching command as follows from bash shell. The syntax is:
command    [key] [flags] [exptime] [bytes] [noreply]\r\n[value]\r
 |
 |
\./
set
add
get
gets
replace
append
prepend
cas
delete
Let us add a key called foo with value BAR, enter:
echo -e 'add foo 0 300 3\r\nBAR\r' | nc localhost 11211
Sample outputs:
STORED
Where,
  1. add : Add/store data to memcached server
  2. foo : Key name
  3. 0 : 32-bit unsigned integer
  4. 300 : Expiration time in seconds
  5. 3 : Number of bytes in the data block (e.g. BAR == 3, nixCraft == 8 and so on)
  6. \r\nBAR\r : Value
To see key (i.e. get foo value), enter:
echo -e 'get foo\r' | nc localhost 11211
Sample outputs:
VALUE foo 0 3
BAR
END
To delete key, enter:
echo -e 'delete foo\r' | nc localhost 11211
Sample outputs:
DELETED

Install memcached PHP support

Type the following apt-get command:
sudo apt-get install php5-memcached php5-fpm php5
You may also install mysql server and php support:
sudo apt-get install mysql-server php5-mysql php5
Finally, make sure you restart the web-server/php-fam:
## restart php5-fpm if installed ##
sudo service php5-fpm restart
## restart Nginx if installed ##
sudo service nginx restart
## restart Apache 2 if installed ##
sudo service apache2 restart
Finally, verify that php5 memcache support is enabled, by typing the following command:
php5 -i | grep memcache
Sample outputs:
/etc/php5/cli/conf.d/20-memcache.ini,
memcache
memcache support => enabled
memcache.allow_failover => 1 => 1
memcache.chunk_size => 32768 => 32768
memcache.compress_threshold => 20000 => 20000
memcache.default_port => 11211 => 11211
memcache.hash_function => crc32 => crc32
memcache.hash_strategy => consistent => consistent
memcache.lock_timeout => 15 => 15
memcache.max_failover_attempts => 20 => 20
memcache.protocol => ascii => ascii
memcache.redundancy => 1 => 1
memcache.session_redundancy => 2 => 2
Registered save handlers => files user memcache 
You can also create a PHP test page with the following code to verify that memcached+php5 is working on the server:
sudo vi /var/www/html/test.php
Append the following code:
<?php
  phpinfo(INFO_MODULES);
?>
Fire a browser and type the following url:
http://your-server-name-or-ip-here/test.php
Sample outputs:
Fig.02: PHP Memcached Check the Installation Test Script
Fig.02: PHP Memcached Check the Installation Test Script

Install memcached Python support

Type the following apt-get command:
sudo apt-get install python-memcache

How do I see memcached memory slabs and stats from CLI?

The syntax is as follows:
 
/usr/share/memcached/scripts/memcached-tool  <host[:port] | /path/to/socket> [mode]
 

See memcached slabs

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 display
Sample outputs:
  #  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM
  1      96B        25s       1       5     yes        0        0    0
  2     120B       344s       1       2     yes        0        0    0
  3     152B    774542s       3   13945     yes        0        0    0
  4     192B    774003s      26  140491     yes        0        0    0
  5     240B    500480s       1     102     yes        0        0    0
  6     304B    483401s       1      24     yes        0        0    0
  7     384B    596752s       2    3261     yes        0        0    0
  8     480B    761228s      11   22058     yes        0        0    0
  9     600B    488482s       1     391     yes        0        0    0
 10     752B    492207s       1     720     yes        0        0    0
 11     944B    488482s       2    1135     yes        0        0    0
 12     1.2K    774542s       3    2352     yes        0        0    0
 13     1.4K    560606s       2    1277     yes        0        0    0
 14     1.8K    487974s       5    2672     yes        0        0    0
 15     2.3K    487974s      10    4384     yes        0        0    0
 16     2.8K    407204s      15    5048     yes        0        0    0
 17     3.5K    347351s      12    3175     yes        0        0    0
 18     4.4K    633802s      36    8104     yes        0        0    0
 19     5.5K    351573s      39    7101     yes        0        0    0
 20     6.9K    293697s      46    6695     yes        0        0    0
 21     8.7K    667676s      56    6597     yes        0        0    0
 22    10.8K    653196s      29    2714     yes        0        0    0
 23    13.6K     75620s      19    1138     yes        0        0    0
 24    16.9K      2580s      39     385     yes        0        0    0
 25    21.2K      3824s      92     415     yes        0        0    0
 26    26.5K       763s       2      39     yes        0        0    0
 27    33.1K       653s       1      17     yes        0        0    0
 28    41.4K       501s       1      11     yes        0        0    0
 29    51.7K       962s       1       5     yes        0        0    0
 30    64.7K       226s       1       5     yes        0        0    0
 31    80.9K       199s       1       4     yes        0        0    0
 32   101.1K         6s       1       1     yes        0        0    0
 33   126.3K       522s       1       1     yes        0        0    0
 35   197.4K       339s       1       1     yes        0        0    0

See memcached general stats

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 stats
Sample outputs:
#127.0.0.1:11211   Field       Value
         accepting_conns           1
               auth_cmds           0
             auth_errors           0
                   bytes   302290901
              bytes_read 34590998647
           bytes_written 348047507143
              cas_badval           0
                cas_hits           0
              cas_misses           0
               cmd_flush           0
                 cmd_get   167116484
                 cmd_set     5551928
               cmd_touch           0
             conn_yields           0
   connection_structures          72
        curr_connections          56
              curr_items      234265
               decr_hits           0
             decr_misses           0
             delete_hits      299319
           delete_misses     1582752
       evicted_unfetched           0
               evictions           0
       expired_unfetched      167287
                get_hits   159501058
              get_misses     7615426
              hash_bytes     2097152
       hash_is_expanding           0
        hash_power_level          18
               incr_hits     2087299
             incr_misses           0
                libevent 2.0.21-stable
          limit_maxbytes  2147483648
     listen_disabled_num           0
                     pid         801
            pointer_size          64
               reclaimed      194549
            reserved_fds          20
           rusage_system 5567.638938
             rusage_user 1467.841043
                 threads           4
                    time  1425157331
       total_connections       31733
             total_items     4255143
              touch_hits           0
            touch_misses           0
                  uptime      774819
                 version 1.4.14 (Ubuntu)

Dumps memcached keys and values (not recommended)

/usr/share/memcached/scripts/memcached-tool 127.0.0.1:11211 dump

How do I flush contents of a Memcached server using CLI?

Type the following command:
 
nc 192.168.1.10 11211<<<"flush_all"
 

Optional: Graphic stand-alone administration for memcached to monitor and debug purpose

phpMemcachedAdmin allows to see in real-time (top-like) or from the start of the server, stats for get, set, delete, increment, decrement, evictions, reclaimed, cas command, as well as server stats (network, items, server version) with googlecharts and server internal configuration.

Install phpMemcachedAdmin

Type the following commands:
cd /var/www/html/
sudo mkdir pmadm
sudo wget http://phpmemcacheadmin.googlecode.com/files/phpMemcachedAdmin-1.2.2-r262.tar.gz
sudo tar xvf phpMemcachedAdmin* -C pmadm
cd pmadm
chmod 0777 Config/Memcache.php
chmod 0777 Temp/
 
Finally, password protect /var/www/html/pmadm/ on a Nginx server:
sudo mkdir /etc/nginx/.htpasswd/
sudo htpasswd -c /etc/nginx/.htpasswd/passwd vivek
Edit nginx.conf file and update your server { ..... } block/context as follows:
location /pmadm/ {
  auth_basic  "Restricted";
  auth_basic_user_file   /etc/nginx/.htpasswd/passwd;
}
 
Restart/reload the nginx server, enter:
sudo service nginx reload
Fire a browser and type the following url:
http://your-server-name-or-ip-here/pmadm/
Sample outputs:
Fig.03: phpMemcachedAdmin in action (click to enlarge)
Fig.03: phpMemcachedAdmin in action (click to enlarge)
References:

0 comments:

Post a Comment