memcached做爲高速運行的分佈式緩存服務器,具備如下的特色。github
- 協議簡單
- 基於libevent的事件處理
- 內置內存存儲方式
- memcached不互相通訊的分佈式
Memcached 是高性能的分佈式內存緩存服務器,而PHP memcache 和 memcached 都是 Memcached 服務器的 PHP 擴展。其中memcache 比 memcached 早出現,因此一些老的代碼可能還在用 memcache 擴展。memcached 後來出現,而且大部分框架都支持 memcached,如今相對較流行。能夠根據本身須要,安裝一個就能夠。web
1、安裝依賴
首先是 memcached,這個擴展須要 libmemcached 客戶端庫,不然會出現以下錯誤算法
checking for libmemcached location... configure: error: memcached support requires libmemcached. Use --with-libmemcached-dir=<DIR> to specify the prefix where libmemcached headers and library are located
ERROR: `/var/tmp/memcached/configure --with-libmemcached-dir=no' failed數據庫
能夠經過以下方法安裝vim
[root@lnmp lnmp.cn]# yum install libmemcached libmemcached-develapi
而 memcache 模塊使用了函數 zlib 來支持數據壓縮,所以安裝此模塊須要安裝 Zlib 模塊。不然會出現以下錯誤:緩存
checking for the location of zlib... configure: error: memcache support requires ZLIB. Use --with-zlib-dir=<DIR> to specify prefix where ZLIB include and library are located
ERROR: `/var/tmp/memcache/configure --enable-memcache-session=No' failed
能夠以下方法用 yum 來安裝:
[root@lnmp lnmp.cn]# yum install zlib zlib-devel
2、安裝 memcached 擴展
嘗試用 PECL 安裝,memcached 在 PECL 上的地址是:
https://pecl.php.net/package/memcached
[root@lnmp lnmp.cn]# pecl install memcached
pecl/memcached requires PHP (version >= 5.2.0, version <= 6.0.0, excluded versions: 6.0.0), installed version is 7.0.8
No valid packages found
install failed
[root@localhost vagrant]#
提示很明顯,PECL 上的 memcached 擴展只支持 PHP 5.2 以上,6.00 如下的版本。還未更新到 PHP7。不過還好的是在 PECL 的 memcached 頁面能夠找到他們在 github 上的連接:
https://github.com/php-memcached-dev/php-memcached
這上面的代碼已經有能夠支持到 PHP7 的分支。這裏將源碼統一下載到 php 源碼的 ext 目錄:
[root@lnmp lnmp.cn]# cd /usr/local/src/php-7.0.8/ext/
[root@lnmp ext]# git clone https://github.com/php-memcached-dev/php-memcached memcached
[root@lnmp ext]# cd memcached/
checkout 到 php7 分支:
[root@lnmp memcached]# git checkout php7
Branch php7 set up to track remote branch php7 from origin.
Switched to a new branch 'php7'
[root@lnmp memcached]#
用 phpize 安裝,個人 PHP 是安裝在 /usr/local/php7 下
[root@lnmp memcached]# /usr/local/php7/bin/phpize
[root@lnmp memcached]# ./configure --with-php-config=/usr/local/php7/bin/php-config
接着 make 和 make install
[root@lnmp memcached]# make
[root@lnmp memcached]# make install
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/
[root@lnmp memcached]#
能夠看到 memcached 已經安裝完成,而且擴展文件已經放到提示的目錄:
[root@lnmp memcached]# ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/
memcached.so opcache.a opcache.so
[root@lnmp memcached]#
最後一步在 php.ini 中引入 memcached.so
[root@lnmp memcached]# vim /usr/local/php7/lib/php.ini
加入:
extension=memcached.so
記得 reload 一下 php-fpm 才能生效
[root@lnmp memcached]# systemctl reload php-fpm
打開 phpinfo 頁面,已經已經看到 memcached 擴展成功安裝了。
3、安裝 memcache 擴展
一樣嘗試用 PECL 來安裝:
[root@lnmp memcached]# pecl install memcache
但一樣失敗
/tmp/pear/temp/memcache/memcache.c:40:40: fatal error: ext/standard/php_smart_str.h: No such file or directory
#include "ext/standard/php_smart_str.h"
^
compilation terminated.
make: *** [memcache.lo] Error 1
ERROR: `make' failed
貌似緣由也是 PECL 還不支持在 PHP7 下安裝 memcache 擴展,
https://pecl.php.net/package/memcache
2013年以來爲更新過。此路不通只能另想辦法,一樣是到 github 上碰碰運氣。搜索 pecl memcache
https://github.com/search?utf8=%E2%9C%93&q=pecl+memcache&type=Repositories&ref=searchresults
其中第一個(https://github.com/websupport-sk/pecl-memcache)就是想要的,而且代碼已經支持到 PHP7,當即下載代碼編譯:
[root@lnmp memcached]# cd ../
[root@lnmp ext]# git clone https://github.com/websupport-sk/pecl-memcache memcache
[root@lnmp ext]# cd memcache
用 phpize 安裝,步驟和 memcached 如出一轍
[root@lnmp memcache]# /usr/local/php7/bin/phpize
[root@lnmp memcache]# ./configure --with-php-config=/usr/local/php7/bin/php-config
[root@lnmp memcache]# make
[root@lnmp memcache]# make install
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/
[root@lnmp memcache]#
相似 memcached , 將 memcache.so 在 php.ini 中引入
[root@lnmp memcache]# vim /usr/local/php7/lib/php.ini
加入:
extension=memcache.so
最後 reload php-fpm
[root@lnmp memcache]# systemctl reload php-fpm
大功告成,能夠在 phpinfo 頁面看到 memcahce 和 memchaced 都已經成功安裝
window下:https://github.com/nono303/PHP7-memcache-dll
4、安裝 Memcached 內存緩存服務器
Centos 下能夠用 yum 進行安裝
[root@lnmp memcache]# yum install memcached
再啓動 Memcached 就能夠測試 PHP 擴展了
[root@lnmp memcache]# systemctl start memcached
或編譯安裝 libevent 和 memcached
[root@lnmp memcache]# wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
[root@lnmp memcache]# tar xzvf libevent-2.1.8-stable.tar.gz
[root@lnmp memcache]# cd libevent-2.1.8-stable
[root@lnmp memcache]# ./configure --prefix=/usr/local/libevent
[root@lnmp memcache]# make && make install
libevent是一套跨平臺的事件處理接口的封裝。memcached使用libevent來進行網絡併發鏈接的處理,可以在很大併發的狀況下,仍保持快速的響應能力。
[root@lnmp memcache]# wget https://memcached.org/files/memcached-1.5.14.tar.gz
[root@lnmp memcache]# tar xzvf memcached-1.5.14.tar.gz
[root@lnmp memcache]# cd memcached-1.5.14
[root@lnmp memcache]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@lnmp memcache]# make && make install
啓動 Memcached
[root@lnmp memcache]# /usr/local/memcached/lib/memcached -d -m 128 -u memcache -p 11211 -P /run/memcached.pid
或 默認端口是 11211,默認memcached可使用的內存大小是 64MB
[root@lnmp memcache]# /usr/local/memcached/lib/memcached -u memcache &
關閉 Memcached
[root@lnmp memcache]# kill `cat /run/memcached.pid`
5、Memcached 運行
Memcached命令的運行:
$ /usr/local/memcached/bin/memcached -h 命令幫助
注意:若是使用自動安裝 memcached 命令位於 /usr/local/bin/memcached。
啓動選項:
- -d是啓動一個守護進程;
- -m是分配給Memcache使用的內存數量,單位是MB;
- -u是運行Memcache的用戶;
- -l是監聽的服務器IP地址,能夠有多個地址;
- -p是設置Memcache監聽的端口,,最好是1024以上的端口;
- -c是最大運行的併發鏈接數,默認是1024;
- -P是設置保存Memcache的pid文件。
參考:
http://www.lnmp.cn/install-memcache-and-memcached-extends-under-php7.html