上一篇说了zookeeper的搭建,现在就得在php环境中使用上它,"ZooKeeper"是Java开发的对java支持比较好,如果php想使用上必须安装其c扩展才可以使用

一、基础环境软件包准备

  1. Lib Zookeeper
    包下载地址: http://zookeeper.apache.org/releases.html
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
  1. PHP Zookeeper

包下载页:http://pecl.php.net/package/zookeeper

wget http://pecl.php.net/get/zookeeper-0.5.0.tgz

二、 环境安装

  1. libZookeeper安装
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
tar zxvf zookeeper-3.4.13.tar.gz
cd zookeeper-3.4.13/src/c
./configure prefix=/usr/local/zookeeper-lib
//libtool: warning: remember to run 'libtool --finish /usr/local/lib'
make && make install
  1. php Zookeeper安装
wget http://pecl.php.net/get/zookeeper-0.5.0.tgz
tar zxvf zookeeper-0.5.0.tgz
cd zookeeper-0.5.0
phpize
./configure --with-php-config=/usr/bin/php-config --with-libzookeeper-dir=/usr/local/zookeeper-lib/
make && make install

三、shell安装脚本编写

#!/bin/bash
# Author: yuekang <iyuekang AT gmail.com>
# Notes: 此脚本专用用于php安装zookeeper安装
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
clear
printf "
#######################################################################
# zookeeper Extension Install shell #
# For online use only #
#######################################################################
"
# Check if user is root
[ $(id -u) != '0' ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
php_install_dir="/usr/local/php"
# Check PHP
if [ -e "${php_install_dir}/bin/phpize" ]; then
phpExtensionDir=$(${php_install_dir}/bin/php-config --extension-dir)
PHP_detail_ver=$(${php_install_dir}/bin/php -r 'echo PHP_VERSION;')
PHP_main_ver=${PHP_detail_ver%.*}
fi
# 安装zookeeper Lib库
install_Zookeeper_lib() {
src_url="http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz" && Download_src
tar zxvf zookeeper-3.4.13.tar.gz
cd zookeeper-3.4.13/src/c
./configure prefix=/usr/local/zookeeper-lib
make && make install
cd
rm -r zookeeper-3.4.13.tar.gz && rm -rf zookeeper-3.4.13
}
# 安装zookeeper扩展
install_Zookeeper_extension() {
PHP_extension="zookeeper"
src_url="http://pecl.php.net/get/zookeeper-0.5.0.tgz" && Download_src
tar zxvf zookeeper-0.5.0.tgz
cd zookeeper-0.5.0
${php_install_dir}/bin/phpize
./configure --with-php-config="${php_install_dir}/bin/php-config" --with-libzookeeper-dir=/usr/local/zookeeper-lib/
make && make install
# 加载so组件
echo "extension=${PHP_extension}.so" > ${php_install_dir}/etc/php.d/${PHP_extension}.ini
cd
rm -r zookeeper-0.5.0.tgz && rm -rf zookeeper-0.5.0
}
Download_src() {
[ -s "${src_url##*/}" ] && echo "[${CMSG}${src_url##*/}${CEND}] found" || { wget --limit-rate=10M -4 --tries=6 -c --no-check-certificate ${src_url}; sleep 1; }
if [ ! -e "${src_url##*/}" ]; then
echo "${CFAILURE}Auto download failed! You can manually download ${src_url} into the src directory.${CEND}"
kill -9 $$ll
fi
}
# Check succ
Check_succ() {
[ -f "${phpExtensionDir}/${PHP_extension}.so" ] && { echo;echo "${CSUCCESS}PHP ${PHP_extension} module installed successfully! ${CEND}"; }
}
install_Zookeeper_lib
install_Zookeeper_extension
Check_succ