案例1: php程序连接memcached服务器(php-memcached)
说明:php-memcached扩展使用了libmemcached库提供的api与memcached服务器端进行交互
安装脚本如下:
~~~
SOFTDIR=/usr/local/src
LIBMEMCACHED_VERSION=1.0.18
LIBMEMCACHED_DIR=/usr/local/libmemcached
PHPDIR=/usr/local/php5
PHP_MEMCACHED_VERSION=2.2.0
#Install dependencies
yum -y install autoconf automake make
#install libmemcached
libmemcached(){
cd $SOFTDIR && [ ! -f libmemcached-${LIBMEMCACHED_VERSION}.tar.gz ] && wget http://soft.51yuki.cn/libmemcached-${LIBMEMCACHED_VERSION}.tar.gz
[ ! -d libmemcached-${LIBMEMCACHED_VERSION} ] && tar zxf libmemcached-${LIBMEMCACHED_VERSION}.tar.gz
cd libmemcached-${LIBMEMCACHED_VERSION}
./configure --prefix=${LIBMEMCACHED_DIR} && make && make install
if [ $? -ne 0 ];then
echo "install libmemcached fail"
exit 2
fi
}
#install php-memcached
php_memcached(){
cd $SOFTDIR && [ ! -f memcached-${PHP_MEMCACHED_VERSION}.tgz ] && wget http://soft.51yuki.cn/memcached-${PHP_MEMCACHED_VERSION}.tgz
[ ! -d memcached-${PHP_MEMCACHED_VERSION} ] && tar xf memcached-${PHP_MEMCACHED_VERSION}.tgz
cd memcached-${PHP_MEMCACHED_VERSION}
$PHPDIR/bin/phpize
./configure --with-php-config=${PHPDIR}/bin/php-config --with-libmemcached-dir=${LIBMEMCACHED_DIR} --disable-memcached-sasl
make && make install
}
#copy memcached.so
copy_memcached(){
grep memcached.so ${PHPDIR}/etc/php.ini
if [ $? -ne 0 ];then
echo "extension=memcached.so" >> ${PHPDIR}/etc/php.ini
else
echo "memcached.so is existing"
fi
}
main(){
libmemcached
php_memcached
copy_memcached
}
main
~~~
如果安装成功,会提示
Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-non-zts-20131226/
测试:
![](https://box.kancloud.cn/0a37502faa9383bb0766539d54fd3716_1039x664.png)
案例2: 通过saltstack批量安装php-memcached扩展(在centos7上操作)
前提:要求每台客户端上php安装位置和配置文件的位置要一致
~~~
第一步:安装libmemcached
[admin@master prod]$ sudo mkdir -p libmemcached/files
[admin@master prod]$ cd libmemcached/files/
[admin@master files]$ sudo wget http://soft.51yuki.cn/libmemcached-1.0.18.tar.gz
[admin@master prod]$ cat libmemcached/install.sls
include:
- pkg.pkg-init
libmemcached-install:
file.managed:
- name: /usr/local/src/libmemcached-1.0.18.tar.gz
- source: salt://libmemcached/files/libmemcached-1.0.18.tar.gz
- user: root
- group: root
- mode: 644
cmd.run:
- name: cd /usr/local/src && sudo tar zxf libmemcached-1.0.18.tar.gz && cd libmemcached-1.0.18 && sudo ./configure --prefix=/usr/local/libmemcached && sudo make && sudo make install
- unless: test -d /usr/local/libmemcached
- require:
- file: libmemcached-install
第二步:安装php-memcached扩展
[admin@master prod]$ sudo mkdir -p phpmemcached/files
[admin@master prod]$ cd phpmemcached/files/
[admin@master files]$ sudo wget http://soft.51yuki.cn/memcached-2.2.0.tgz
[admin@master prod]$ cat phpmemcached/install.sls
include:
- libmemcached.install
phpmemcached_install:
file.managed:
- name: /usr/local/src/memcached-2.2.0.tgz
- source: salt://phpmemcached/files/memcached-2.2.0.tgz
- user: root
- group: root
- mode: 644
cmd.run:
- name: cd /usr/local/src && sudo tar xf memcached-2.2.0.tgz && cd memcached-2.2.0 && /usr/local/php5/bin/phpize && sudo ./configure --with-php-config=/usr/local/php5/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl && sudo make && sudo make install
- unless: test -f /usr/local/php5/lib/php/extensions/no-debug-non-zts-20131226/memcached.so
- require:
- file: phpmemcached_install
/usr/local/php5/etc/php.ini:
file.append:
- text:
- extension=memcached.so
测试:
[admin@master prod]$ sudo salt 'node7*' state.sls phpmemcached.install env=prod test=true
Summary
-------------
Succeeded: 16 (unchanged=2, changed=2)
Failed: 0
-------------
Total states run: 16
~~~