How to install memcached on CentOS with memcached PHP extension on CentOS

How to install memcached on CentOS with memcached PHP extension on CentOS

Updated: Wed, 06/19/2013 - 12:22am

We'll need the following:

Let's head over to our setup directory--this is where we'll be downloading and installing everything from.

cd /usr/local/src
 

Fix yum, so it only shows the packages made for our system

vi /etc/yum.conf

Add the following line:

multilib_policy=best

Basically, what this does is, if you're on x86_64 it will only show the x86_64 packages; and vice-versa.

Install memcached using yum

We need to get the latest memcached from yum, however all the regular repos have the oudated version... So we install the PowerStack repository which has the latest version memcached 1.4.14

rpm -Uvh http://powerstack.org/powerstack-release.rpm
yum update
yum install memcached

Install memcached from source

This is an alternative step. Yum way works great--everything is setup for you. However, If you want to do this manually, then continue here:

We'll first need to install libevent. Let's see if it's installed already

whereis libevent 

*** Installing via yum will install an older libevent 1.4.13 ***

yum install gcc libevent libevent-devel 

OR we can install libevent from source. You can get latest version here: http://libevent.org/ . However, the latest version of libevent may be too new for the latest version of memcached, so if something goes wrong, try a lower version.

wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar xvfz libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure
make
make install
#create a symbolic link to libevent since it's not made by default in the location that memcached is expecting it to be
ln -s /usr/local/lib/libevent-2.0.so.5 /lib64/

Now with libevent installed, we can install memcached from source.

wget http://memcached.org/latest
tar -zxvf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure
make
make install

If memcached can't start complaing about :

memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

We need to see where it's looking for the libevent library and crate a symbolic link to it.

LD_DEBUG=libs memcached - v 2>&1 > /dev/null I less 
#Example: for me memcached was looking for libevent in /lib64 
ln -s /usr/local/lib/libevent-2.0.so.5 /lib64/

Configure memcached

vi /etc/sysconfig/memcached

We set the CACHESIZE. Normally this is pretty low, so we up it to 2048MB

PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="2048"
OPTIONS="-l 127.0.0.1"

Next we make sure memcached starts automatically.

chkconfig memcached on
service memcached start

*** If you get an error saying memcached is missing when trying 'chconfig memcached on' do the following (this will likely not happen if you've installed memcached using Yum):

vi /etc/rc.d/init.d/memcached

Paste this:

#!/bin/bash
#
# memcached    This shell script takes care of starting and stopping
#              standalone memcached.
#
# chkconfig: - 80 12
# description: memcached is a high-performance, distributed memory
#              object caching system, generic in nature, but
#              intended for use in speeding up dynamic web
#              applications by alleviating database load.
# processname: memcached
# config: /etc/memcached.conf
# Source function library.
. /etc/rc.d/init.d/functions
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/memcached
DAEMONBOOTSTRAP=/usr/local/bin/start-memcached
DAEMONCONF=/etc/memcached.conf
NAME=memcached
DESC=memcached
PIDFILE=/var/run/$NAME.pid
[ -x $DAEMON ] || exit 0
[ -x $DAEMONBOOTSTRAP ] || exit 0
RETVAL=0
start() {
 echo -n $"Starting $DESC: "
 daemon $DAEMONBOOTSTRAP $DAEMONCONF
 RETVAL=$?
 [ $RETVAL -eq 0 ] && touch $PIDFILE
 echo
 return $RETVAL
}
stop() {
 echo -n $"Shutting down $DESC: "
 killproc $NAME
 RETVAL=$?
 echo
 [ $RETVAL -eq 0 ] && rm -f $PIDFILE
 return $RETVAL
}
# See how we were called.
case "$1" in
 start)
  start
  ;;
 stop)
  stop
  ;;
 restart|reload)
  stop
  start
  RETVAL=$?
  ;;
 status)
  status $prog
  RETVAL=$?
  ;;
 *)
  echo $"Usage: $0 {start|stop|restart|status}"
  exit 1
esac
exit $RETVAL

Now make it executable

chmod 755 /etc/rc.d/init.d/memcached

Create the script

vi /usr/local/bin/start-memcached
chmod 755  /usr/local/bin/start-memcached
#!/usr/bin/perl -w
# start-memcached
# 2003/2004 - Jay Bonci <[email protected]>
# This script handles the parsing of the /etc/memcached.conf file
# and was originally created for the Debian distribution.
# Anyone may use this little script under the same terms as
# memcached itself.
use strict;
if ($> != 0 and $< != 0) {
 print STDERR "Only root wants to run start-memcached.\n";
 exit;
}
my $etcfile = shift || "/etc/memcached.conf";
my $params = [];
my $etchandle; 
# This script assumes that memcached is located at /usr/bin/memcached, and
# that the pidfile is writable at /var/run/memcached.pid
my $memcached = "/usr/local/bin/memcached";
my $pidfile = "/var/run/memcached.pid";
# If we don't get a valid logfile parameter in the /etc/memcached.conf file,
# we'll just throw away all of our in-daemon output. We need to re-tie it so
# that non-bash shells will not hang on logout. Thanks to Michael Renner for 
# the tip
my $fd_reopened = "/dev/null";
sub handle_logfile {
 my ($logfile) = @_;
 $fd_reopened = $logfile;
}
sub reopen_logfile {
 my ($logfile) = @_;
 open *STDERR, ">>$logfile";
 open *STDOUT, ">>$logfile";
 open *STDIN, ">>/dev/null";
 $fd_reopened = $logfile;
}
# This is set up in place here to support other non -[a-z] directives
my $conf_directives = {
 "logfile" => \&handle_logfile
};
if (open $etchandle, $etcfile) {
 foreach my $line (<$etchandle>) {
  $line =~ s/\#.*//go;
  $line = join ' ', split ' ', $line;
  next unless $line;
  next if $line =~ /^\-[dh]/o;
  if ($line =~ /^[^\-]/o) {
   my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/; 
   $conf_directives->{$directive}->($arg);
   next;
  }
  push @$params, $line;
 }
}
unshift @$params, "-u root" unless (grep $_ eq '-u', @$params);
$params = join " ", @$params;
if (-e $pidfile) {
 open PIDHANDLE, "$pidfile";
 my $localpid = <PIDHANDLE>;
 close PIDHANDLE;
 chomp $localpid;
 if (-d "/proc/$localpid") {
  print STDERR "memcached is already running.\n"; 
  exit;
 } else {
  `rm -f $localpid`;
 }
}
my $pid = fork();
if ($pid == 0) {
 reopen_logfile($fd_reopened);
 exec "$memcached $params";
 exit(0);
} elsif (open PIDHANDLE,">$pidfile") {
 print PIDHANDLE $pid;
 close PIDHANDLE;
} else {
 print STDERR "Can't write pidfile to $pidfile.\n";
}

Now we try to add it as a service again...

chkconfig memcached on
service memcached start

Cool! we now have the latest version of memcached installed.

Let's check to make sure it's working:

echo stats | nc localhost 11211

This will show something like

STAT pid 10412
STAT uptime 6353
STAT time 1346136470
STAT version 1.4.14
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.001999
STAT curr_connections 10
STAT total_connections 14
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 1
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 1
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 33
STAT bytes_written 2059
STAT limit_maxbytes 536870912
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END

Ok it works.

If you get an error saying nc: command not found, then simply do

yum install rc

Install libmemcached

This is needed for pecl/memcached PHP extension. We install the yum repo that has libmemcached and install it with all it's dependencies:

libmemcached
libmemcached-devel
cyrus-sasl-devel

Let's install libmemcached and it's dependencies.

pecl/memcached needs libmemcached 0.39+, and the regular yum repositories probably will install an older version, which won't work

For CentOS 6.x use the following

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
yum --enablerepo=remi install libmemcached\*

For CentOS 5.x use the following

wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm
yum --enablerepo=remi install libmemcached\*

If you want to permantely enable the remi repo, then do the following:

vi /etc/yum.repos.d/remi.repo

Now enable the first/top repo (only) by changing enabled=0 to:

enabled=1

Ok... libmemcached should now be installed, and we can move on to the next step.

Install pecl/memcached

We're almost done... We just now need to install the memcached PHP extension.

wget http://pecl.php.net/get/memcached # this will get the latest version.
tar -zxvf memcached-x.x.x.tgz  #fill in whatever version you just download; e.g. memcached-2.1.0.tgz
cd memcached-x.x.x
phpize
./configure
make
make install

Note the extension installation directory, because we will need it in the next step. e.g. /usr/local/lib/php/extensions/no-debug-non-zts-20100525/

locate php.ini # in centos it's located at /usr/local/lib/php.ini
vi /usr/local/lib/php.ini

now add the following line near the bottom of php.ini

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525/"
extension=memcached.so

Example: This is what your php.ini may look like with APC and memcached installed

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525/"
extension = apc.so
apc.enabled=1
apc.shm_size=256M
; or if you have less than 4GB memory try:
; apc.shm_size=128M
apc.ttl = 0
apc.mmap_file_mask=/tmp/apc.XXXXXX
 
 
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525/"
extension=memcached.so

Restart Apache.

service httpd restart

Okie dokie... Done! (I think I'm delirious writing this) Let's check to make sure it's working:

php -i | grep memcached

You should see a few lines detailing your setup. Cool! And.... One final step...

 

Open memcached port 11211 on your firewall

*** UPDATE: You can skip this step for security reasons, and manually add the IPs of your server farm to your firewall if needed. For CSF:

vi /etc/csf/csf.allow

Then add your IPs to the list. **** If you're using a server farm (load balancing) and you have a firewall installed, you'll need to open port 11211 so memcached can share resources across all servers.

vi /etc/sysconfig/iptables

add the below INPUT line:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT

Restart iptables

service iptables restart

For CSF use:

vi /etc/csf/csf.conf

under "TCP_IN" and "TCP_OUT" add port: 11211 so it looks something like this:

# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,2222,11211"
 
# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,80,110,113,443,2222,11211"

once done restart CSF

service csf restart

So that's it and that's that...

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037176&siteId=291194637