设为首页 收藏本站
查看: 509|回复: 0

[经验分享] How to install memcached on CentOS with memcached PHP extension on CentOS

[复制链接]

尚未签到

发表于 2016-5-10 11:08:30 | 显示全部楼层 |阅读模式
How to install memcached on CentOS with memcached PHP extension on CentOS

Published: Wed, 08/29/2012 - 8:46am
Updated: Wed, 06/19/2013 - 12:22am
  We'll need the following:



  • memcached ( http://memcached.org/ ) Current Version at time of writing: memcached v1.4.15 date: 2012-9-3


    • libevent ( http://libevent.org/ ) Current Version at time of writing: libevent-2.0.21 date: 2012-11-18 **If we're going to build memcached from source, we'll need this



  • pecl/memcached ( http://pecl.php.net/package/memcached ) Current version at time of writing: pecl/memcached 2.1.0 date: 2012-08-07


    • libmemcached ( http://libmemcached.org ; needed by pecl/memcached) Current version at time of writing: libmemcached 1.0.10 date:2012-07-31
      ** NOTE: we'll be installing this using yum , since doing from source failed for me as it required other dependencies **


  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 <jaybonci@debian.org>
# 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...














  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-215119-1-1.html 上篇帖子: 如何在CentOS 7.x中安装OpenERP(Odoo) 下篇帖子: 在Thoshiba PR100上装CentOS 5的成果汇报
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表