centos 7.0 安装Memcached以及配置php的Memcache扩展

因项目需求,需要在centos 7.0上安装Memcached以及配置php的Memcache扩展。所以只好通过网上资料一步一步来安装和配置了。

第一步: 准备工具

下载memcached和memcache源码

1.  memcached官网: http://memcached.org/downloads

20160517085016

2. php扩展: http://pecl.php.net/package/memcache

20160517085424

第二步: 安装

1. 安装memcached

#下载memcached源码 
wget wget http://memcached.org/files/memcached-1.4.25.tar.gz
#解压
tar -zxvf memcached-1.4.25.tar.gz
#切换到解压后的目录
cd memcached-1.4.25

20160517091628

#配置
./configure --prefix=/usr/local/memcached

20160517091648

If it’s already installed, specify its path using –with-libevent=/dir/

需要安装libevent,从http://libevent.org/下载libevent-2.0.22-stable.tar.gz,并编译安装。

#下载libevent源码
wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
#解压
tar -zxvf libevent-2.0.22-stable.tar.gz
#切换到解压后的目录
cd libevent-2.0.22-stable
#配置
./configure --prefix=/usr/local/libevent
#编译安装
make && make install

20160517092757 20160517092851

#再次配置memcache
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
#安装
make && make install

20160517093315 20160517093341

#设置环境
ln -s /usr/local/memcached/bin/memcached /usr/bin/memcached
cp scripts/memcached.sysv /etc/init.d/memcached
 
#改为监听127.0.0.1,并关闭UDP连接方式,若为远程服务调用或不需要的话请跳过此行
sed -i 's/OPTIONS=""/OPTIONS="-l 127.0.0.1 -U 0"/g' /etc/init.d/memcached 
 
sed -i 's@chown@#chown@' /etc/init.d/memcached
sed -i 's@/var/run/memcached/memcached.pid@/var/run/memcached.pid@' /etc/init.d/memcached
 
#启动并设置开机服务
chmod +x /etc/init.d/memcached
service memcached start
chkconfig --add memcached
chkconfig memcached on

2. 安装php的memcache扩展

#从github下载PHP7专用的memcached组件分支 
wget https://github.com/php-memcached-dev/php-memcached/archive/php7.zip
#从官方下载php-memcached的最新源码包
wget http://pecl.php.net/get/memcached-2.2.0.tgz
#解压
unzip php7.zip
#切换到解压的目录
cd php-memcached-php7/

20160517094814

 /usr/local/php7/bin/phpize

20160517094814

#配置
./configure --with-php-config=/usr/local/php7/bin/php-config

20160517095424

configure: error: memcached support requires libmemcached. Use –with-libmemcached-dir=<DIR> to specify the prefix where libmemcached headers and library are located

需要安装libmemcached

#下载libmemcached
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
#解压
tar -zxvf libmemcached-1.0.18.tar.gz
#切换到解压后的目录
cd libmemcached-1.0.18
#配置
./configure --with-memcached=/usr/local/memcached --prefix=/usr/local/libmemcached
#编译安装
make && make install
#再次配置 php-memcached-php7
./configure --with-php-config=/usr/local/php7/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached

20160517101250

configure: error: no, sasl.h is not available. Run configure with –disable-memcached-sasl to disable this check

yum install cyrus-sasl-devel

20160517101433

备注: 安装完之后,记得重新配置、编译、安装libmemcached

#再次配置 php-memcached-php7
./configure --with-php-config=/usr/local/php7/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached

20160517102332

#编译安装php-memcached-php7
make && make install

20160517102513

3. 开启memcache扩展

在php.ini文件中开启memcache

extension=memcached.so

重启php-fpm和nginx

20160517111852

第三步: 测试

1. 安装wordpress插件

下载https://github.com/tollmanz/wordpress-pecl-memcached-object-cache文件,解压获得object-cache.php文件,并放到wp-content目录下。

20160517115935

2. 查看效果

使用官方php的memcache扩展源码包中的memcache.php文件

20160517121536

20160517121751

修改代码如下:

20160517121922

把此文件放到自己指定目录下,然后再访问。

20160517122251

 

Author: jhonse

Jhonse技术博客: http://blog.jhonse.com

One thought on “centos 7.0 安装Memcached以及配置php的Memcache扩展”

Leave a Reply

Your email address will not be published. Required fields are marked *