2015年9月17日 星期四

CentOS 安裝 Memcached Server、PHP memcached extension

PHP 有兩個 extension 可操作 Memcached Server
memcache extension (php-pecl-memcache)
memcached extension (php-pecl-memcached)
以下安裝 memcached extension,功能較多。

php-pecl-memcached 可選擇使用 igbinary 來序列化,用以取代 php 原本的 serializer,所以也可以安裝 igbinary (目前的版本,設定檔沒指定 memcached.serializer 時,若有 igbinary 可用,預設會使用 igbinary )
$ yum install memcached
$ yum install php-pecl-memcached
$ yum install igbinary

Memcached Server 設定檔位置
$ vi /etc/sysconfig/memcached
Slab Allocator:一種linux系統的記憶體管理機制


[補充] 手動編譯:
PHP memcached extension,是使用 libmemcached library 跟 memcached server 溝通,所以要先裝 libmemcached。
編譯安裝 libmemcached (http://libmemcached.org/)
$ 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
(若之前已編譯安裝過,要重新編譯,須先執行 make clean)
(# make clean)
$ ./configure --prefix=/usr/local/libmemcached --with-memcached
$ make
$ make install

編譯安裝 igbinary (http://pecl.php.net/package/igbinary)
$ wget http://pecl.php.net/get/igbinary-1.2.1.tgz
$ tar zxvf igbinary-1.2.1.tgz
$ cd igbinary-1.2.1
(若之前已編譯安裝過,要重新編譯,須先執行 make clean)
(# make clean)
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make
$ make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
Installing header files:          /usr/local/php/include/php/

編譯安裝 msgpack (https://pecl.php.net/package/msgpack)
除了 igbinary,也可以選 msgpack 當 serializer
$ wget https://pecl.php.net/get/msgpack-0.5.7.tgz
$ tar zxvf msgpack-0.5.7.tgz
$ cd msgpack-0.5.7
(若之前已編譯安裝過,要重新編譯,須先執行 make clean)
(# make clean)
$ /usr/local/php/bin/phpize
$ ./configure
configure: error: Cannot find php-config. Please use --with-php-config=PATH
加上 php-config 路徑
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make
$ make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
Installing header files:          /usr/local/php/include/php/

最後再編譯安裝 php pecl memcached(https://pecl.php.net/package/memcached)
視要使用 igbinary 或 msgpack 當 serializer,編譯參數可選擇加入 "--enable-memcached-igbinary" 或 "--enable-memcached-msgpack",也可都加入
$ wget http://pecl.php.net/get/memcached-2.2.0.tgz
$ tar zxvf memcached-2.2.0.tgz
$ cd memcached-2.2.0
(若之前已編譯安裝過,要重新編譯,須先執行 make clean)
(# make clean)
$ /usr/local/php/bin/phpize
$ ./configure --enable-memcached-igbinary --enable-memcached-msgpack --disable-memcached-sasl --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir --with-libmemcached-dir=/usr/local/libmemcached
$ make
$ make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

假設 memcached.serializer 要使用 msgpack,編輯 php.ini ,加入 memcached.serializer="msgpack" 設定
$ vi /usr/local/php/etc/php.ini
[memcached]
memcached.serializer="msgpack"

重新啟動PHP後,查看 memcached.serializer 設定
$ /usr/local/php/bin/php -i | grep memcached.serializer
memcached.serializer => msgpack => msgpack


若要解決安裝 igbinary、msgpack、php pecl memcached ,在 configure 時出現以下  WARNING 訊息
「configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.」
可安裝 re2c (http://re2c.org/)
$ wget https://github.com/skvadrik/re2c/releases/download/0.15.3/re2c-0.15.3.tar.gz
$ tar zxvf re2c-0.15.3.tar.gz
$ cd re2c-0.15.3
$ ./configure
$ make
$ make install


其他:
stop PHP
$ kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
start PHP
$ /usr/local/php/sbin/php-fpm -g /usr/local/php/var/run/php-fpm.pid &
restart PHP
$ kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`


參考:
http://php.net/manual/en/memcached.installation.php
http://iceeggplant.blog.51cto.com/1446843/819576
http://pecl.php.net/package/memcached
http://blog.slogra.com/post-421.html


沒有留言:

張貼留言