溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP7下安裝memcache和memcached擴(kuò)展的方法

發(fā)布時(shí)間:2021-06-25 09:35:12 來源:億速云 閱讀:384 作者:chen 欄目:編程語言

這篇文章主要介紹“PHP7下安裝memcache和memcached擴(kuò)展的方法”,在日常操作中,相信很多人在PHP7下安裝memcache和memcached擴(kuò)展的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PHP7下安裝memcache和memcached擴(kuò)展的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

memcache和memcached都是Memcached服務(wù)器的PHP擴(kuò)展。其中memcache比memcached早出現(xiàn),所以一些老的代碼可能還在用memcache擴(kuò)展??梢愿鶕?jù)自己需要,安裝一個(gè)即可。這里兩個(gè)的安裝方法都說一下。

Memcached 是高性能的分布式內(nèi)存緩存服務(wù)器,而PHP memcache 和 memcached 都是 Memcached 服務(wù)器的 PHP 擴(kuò)展。其中memcache 比 memcached 早出現(xiàn),所以一些老的代碼可能還在用 memcache 擴(kuò)展。memcached 后來出現(xiàn),并且大部分框架都支持 memcached,現(xiàn)在相對較流行。

安裝依賴

首先是 memcached,這個(gè)擴(kuò)展需要 libmemcached 客戶端庫,否則會(huì)出現(xiàn)如下錯(cuò)誤

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

可以通過如下方法安裝

[root@lnmp lnmp.cn]# yum install libmemcached libmemcached-devel

而 memcache 模塊使用了函數(shù) zlib 來支持?jǐn)?shù)據(jù)壓縮,因此安裝此模塊需要安裝 Zlib 模塊。否則會(huì)出現(xiàn)如下錯(cuò)誤:

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

安裝 memcached 擴(kuò)展

嘗試用 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 擴(kuò)展只支持 PHP 5.2 以上,6.00 以下的版本。還未更新到 PHP7。不過還好的是在 PECL 的 memcached 頁面可以找到他們在 github 上的鏈接:

https://github.com/php-memcached-dev/php-memcached

這上面的代碼已經(jīng)有可以支持到 PHP7 的分支。這里將源碼統(tǒng)一下載到 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 已經(jīng)安裝完成,并且擴(kuò)展文件已經(jīng)放到提示的目錄:

[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 頁面,已經(jīng)已經(jīng)看到 memcached 擴(kuò)展成功安裝了。

PHP7下安裝memcache和memcached擴(kuò)展的方法


安裝 memcache 擴(kuò)展

同樣嘗試用 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 擴(kuò)展,

https://pecl.php.net/package/memcache

2013年以來為更新過。此路不通只能另想辦法,同樣是到 github 上碰碰運(yùn)氣。搜索 pecl memcache

https://github.com/search?utf8=%E2%9C%93&q=pecl+memcache&type=Repositories&ref=searchresults

其中第一個(gè)(https://github.com/websupport-sk/pecl-memcache)就是想要的,并且代碼已經(jīng)支持到 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 都已經(jīng)成功安裝

PHP7下安裝memcache和memcached擴(kuò)展的方法

到此,關(guān)于“PHP7下安裝memcache和memcached擴(kuò)展的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI