跳转至

php扩展模块memcache


2015-06-11 by dongnan

目标

php 安装 memcached 扩展模块,截图中的第2个项目。

适用于 php 5.6.x 版本。

条件

需要提前安装 libmemcached 软件。

步骤

内容来自LNMP自动安装脚本

# 修改php扩展目录,
# 本例:"/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226"

#!/bin/bash
#install nginx mysql php
#20120718 by http://ywwd.net
#last update 20150526
#v0.4

#variables
dir=/usr/local
ext_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"


#libmemcached
if [ ! -e "$dir/libmemcached" ];then
#  
    tar zxf libmemcached-1.0.18.tar.gz
    cd libmemcached-1.0.18
    ./configure --prefix=/usr/local/libmemcached
    make && make install
#
    if [ ! -e "$dir/libmemcached" ];then
        echo -e "\033[31merror! can't install libmemcached! \033[0m"
        exit 1
    fi
else
    echo -e "\033[31mlibmemcached is already exists! \033[0m"
fi


#php_memcached
if [ ! -e "$ext_dir/memcached.so" ];then
    tar zxf memcached-2.2.0.tgz
    cd memcached-2.2.0
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcached --with-libmemcached-dir=/usr/local/libmemcached
    make && make install
#
    sed -i 's#; extension_dir = "./"#extension_dir = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/#' /etc/php.ini
    ! grep -i 'memcached.so' /etc/php.ini && sed -i '/no-debug-non-zts-20131226/ a\extension=memcached.so' /etc/php.ini
#
    /etc/init.d/php-fpm restart
    rm -rf phpredis-develop/ && sleep 1

    if [ ! -e "$ext_dir/memcached.so" ];then
        echo -e "\033[31merror! can't install memcached.so! \033[0m"
        exit 1
    fi
else
    echo -e "\033[31mmemcached.so is already exists! \033[0m"
fi

重启PHP-FPM

/etc/init.d/php-fpm restart

验证

php -m | grep memcached
memcached

小结

libmemcached 是一个 memcached 的库客户端库,C 和 C++ 语言实现的客户端库,具有低内存占用率、线程安全、并提供对 memcached 功能的全面支持。

回到页面顶部