`
hooroger
  • 浏览: 13280 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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 <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...

 

分享到:
评论

相关推荐

    centos php memcached扩展

    php memcached扩展,内含安装手册以及源码包,libevent-2.0.22,libmemcached-1.0.18,memcached-1.4.25,memcached-2.2.0,已经在CentOS6.5上进行测试通过。

    CentOS 安装配置memcached

    CentOS 安装配置 Memcached 在本文中,我们将详细介绍如何在 CentOS 系统上安装和配置 Memcached。Memcached 是一个高性能的分布式内存对象缓存系统,广泛应用于各种 web 应用程序中,以提高应用程序的性能和响应...

    Centos6.5下安装Memcached完整示例

    在本文中,我们将深入探讨如何在CentOS 6.5操作系统上安装和配置Memcached,一个流行的、高性能的分布式内存对象缓存系统。这个过程对于优化数据库读取速度,减轻数据库负载,以及提升Web应用程序的整体性能至关重要...

    linux下 php安装memcached扩展

    - 使用包管理器安装 Memcached(例如,在 CentOS 上使用 `yum install memcached`)。 - 启动 Memcached 服务 (`systemctl start memcached`)。 #### 三、下载安装文件 根据给定的部分内容,首先我们需要下载 ...

    CentOS下Nginx0.8.52-PHP5.3.3-MySQL5.5.6-Memcached1.4.5安装配置优化

    在本篇文章中,我们将详细探讨在CentOS环境下安装和配置Nginx 0.8.52、PHP 5.3.3、MySQL 5.5.6以及Memcached 1.4.5这一系列软件的过程,同时还会对配置进行优化以达到提升性能的目的。这些技术组件共同构成了一个...

    tomcat+memcached依赖包(centos)

    4. **安装Memcached**:使用`yum install memcached`命令安装`Memcached`服务。如果需要调整默认配置,可以编辑`/etc/sysconfig/memcached`文件,比如更改监听端口、内存分配等。 5. **配置Memcached连接**:在`...

    在CentOS 5.3上安装Memcached服务及相应的PHP扩展memcache

    在这个教程中,我们将详细介绍如何在CentOS 5.3上安装Memcached服务以及相关的PHP扩展`memcache`。 Memcached是一个高性能、分布式内存对象缓存系统,它被广泛用于减轻数据库负载,提高网站的响应速度。而PHP的`...

    Centos6.0 安装 nginx memcached

    在本文中,我们将详细介绍如何在CentOS 6.0系统上安装Nginx和Memcached。这两位主角是Web服务器Nginx和内存缓存系统Memcached,它们在提高网站性能方面发挥着重要作用。 首先,确保你的系统是最小化桌面版本的...

    memcached集群linux搭建

    在Ubuntu/Debian中,配置文件通常位于`/etc/memcached.conf`,而在CentOS/RHEL中,它位于`/etc/sysconfig/memcached`。打开文件并根据需求调整参数,例如监听端口、最大内存分配等。 **3. 设置集群** Memcached...

    CentOS6.5+mysql+nginx+php+memcached安装指南

    Windows8上安装虚拟机VirtualBox,然后新建虚拟电脑,搭建CentOS6.5 + mysql + nginx + php + memcached平台环境,部署网站。文档中记录了部分FAQ。

    centos系统为php安装memcached扩展步骤

    代码如下:yum -y install memcached#安装完成后执行:memcached -h#出现memcached帮助信息说明安装成功 2. 加入启动服务 代码如下:chkconfig –level 2345 memcached on 3. 配置Memcached 代码如下:vim /etc/...

    CentOS下安装Memcached和PHP Memcached扩展

    2.0.1.tgz`,解压后使用`phpize`进行预处理,再运行`./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config`进行配置,接着执行`make && make install`安装。找到生成的`memcached.so`...

    docker-centos7-memcached:基于 Docker 的 Memcached 1.4.22 + CentOS 7.0 用于 centminmod.com

    我的第一个成功的基于 Docker 的映像用于 CentOS 7.0 上的 Memcached 1.4.22,旨在与。 我的 Docker Hub 仓库 Centmin Mod Docker 开发论坛 从 Docker Hub 抓取 docker pull centminmod/docker-centos7-memcached ...

    memcached安装包以及MemCachedClient

    sudo apt-get install memcached ``` 或者,如果你选择手动编译安装,可以从官方网站获取源代码,然后执行以下步骤: ```bash wget http://www.memcached.org/files/memcached-1.x.x.tar.gz tar -zxvf memcached-1...

    memcached安装包以及java所需的memcached架包

    1. **下载与解压**:首先,你需要从官方网站或者通过包管理器(如在Ubuntu上使用`sudo apt-get install memcached`,在CentOS上使用`sudo yum install memcached`)获取memcached的安装包,并将其解压缩。...

    linux下memcached安装

    sudo apt-get install memcached ``` 在基于RPM的系统上: ```bash sudo yum install memcached ``` **4. 配置Memcached** 默认情况下,Memcached可能不会启动自动。我们可以通过编辑配置文件来自定义设置。配置...

    缓存服务器memcached下载

    在多数操作系统上,可以通过包管理器安装Memcached,例如在Ubuntu上可以使用`apt-get install memcached`,在CentOS上使用`yum install memcached`。安装完成后,需要配置启动参数,如监听端口、最大内存大小等,...

    memcached安装包

    sudo apt-get install memcached ``` ### CentOS/RHEL: ```bash sudo yum install epel-release sudo yum install memcached ``` 安装完成后,服务通常会自动启动。如果没有,可以手动启动: ```bash sudo ...

Global site tag (gtag.js) - Google Analytics