PHP 7 Memcache 扩展组件的安装

| 选择喜欢的代码风格  

memcache官网 http://memcached.org


 #下载memcache源码包
 wget http://memcached.org/latest
 #解压
 tar -zxvf memcached-1.x.x.tar.gz
 cd memcached-1.x.x
 #编译安装
 ./configure && make && make test && sudo make install
 #运行memcached
 /usr/local/memcached/bin/memcached -d -m 128 -l localhost -p 11211 -u root
 -d 以守护程序(daemon)方式运行 memcached
 -m 设置 memcached 可以使用的内存大小,单位为 M
 -l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数
 -p 设置监听的端口,默认为 11211,所以也可以不设置此参数
 -u 指定用户

PHP 7 安装 Memcache 扩展


选用 Github 的 pecl-memcache 分支版本:https://github.com/websupport-sk/pecl-memcache/git clone 或者直接下载编译安装 PHP 7.3 的 Memcache 扩展:

wget https://github.com/websupport-sk/pecl-memcache/archive/NON_BLOCKING_IO_php7.zip
tar -xvf NON_BLOCKING_IO_php7.zip
cd pecl-memcache-NON_BLOCKING_IO_php7/

phpize
./configure --with-php-config=/Data/apps/php7.3.7/bin/php-config #之前 PHP 7.3 的自定义安装路径

make && make install

#最后系统提示路径:
/Data/apps/php7.3.7/lib/php/extensions/no-debug-non-zts-20180731/

#编辑php.ini文件
[memcache]
extension_dir = "/Data/apps/php7.3.7/lib/php/extensions/no-debug-non-zts-20180731/"
extension = memcache.so

#然后重启 php-fpm
/etc/init.d/php-fpm restart

#查看 phpinfo 后可以看到 memcache 就证明已经安装成功了
#或者 php -m 命令,查看 PHP 7 加载的扩展也可以

PHP查看扩展的方法


除了在php文件里,调用 phpinfo() 之外,用命令行执行 php -m

[root@Dev_Test ~]$php -m
[PHP Modules]
apc
bcmath
calendar
Core
ctype
curl
date
dom
ereg
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
mcrypt
memcache
memcached
mhash
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
protocolbuffers
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
ssh2
standard
sysvsem
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
zip
zlib

[Zend Modules]

Linux 下执行命令 php --ri memcache 查看组件详细配置信息

$php --ri memcache

memcache

memcache support => enabled
Version => 4.0.4
Revision => $Revision$

Directive => Local Value => Master Value
memcache.allow_failover => 1 => 1
memcache.max_failover_attempts => 20 => 20
memcache.default_port => 11211 => 11211
memcache.chunk_size => 32768 => 32768
memcache.protocol => ascii => ascii
memcache.hash_strategy => consistent => consistent
memcache.hash_function => crc32 => crc32
memcache.redundancy => 1 => 1
memcache.session_redundancy => 2 => 2
memcache.compress_threshold => 20000 => 20000
memcache.lock_timeout => 15 => 15
memcache.session_prefix_host_key => 0 => 0
memcache.session_prefix_host_key_remove_www => 1 => 1
memcache.session_prefix_host_key_remove_subdomain => 0 => 0
memcache.session_prefix_static_key => no value => no value
memcache.session_save_path => no value => no value
memcache.prefix_host_key => 0 => 0
memcache.prefix_host_key_remove_www => 1 => 1
memcache.prefix_host_key_remove_subdomain => 0 => 0
memcache.prefix_static_key => no value => no value

memcache 扩展安装的坑:


如果是 PHP 7.3.x,则上述的 memcache 扩展需要用下面方式安装:

如果你的系统还未编译 libmemcached,则下载编译它:https://launchpad.net/libmemcached/+download

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
./configure --prefix=/usr/local/libmemcached --with-memcached
make
make install
git clone -b php7 https://github.com/php-memcached-dev/php-memcached.git
Cloning into 'php-memcached'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
Receiving objects:  81% (3227/3983), 1.07 MiB | 9.00 KiB/s 
....

[root@LoadBalancing /Data/tools]
cd php-memcached/

#/Data/apps/php7.3.7 为 PHP 的安装路径,需要根据你安装的实际目录调整
[root@LoadBalancing /Data/tools/php-memcached]
/Data/apps/php7.3.7/bin/phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

./configure --with-php-config=/Data/apps/php7.3.7/bin/php-config  --enable-memcached --enable-memcached-json --enable-memcached-igbinary  --disable-memcached-sasl --with-libmemcached-dir=/usr/local/libmemcached

make && make install

#SASL 全称 Simple Authentication and Security Layer,是一种用来扩充C/S模式验证能力的机制。 
#简单来说SASL是一个胶合(glue)库,通过这个库把应用层与形式多样的认证系统整合在一起。
#这有点类似于 PAM,但是后者是认证方式,决定什么人可以访问什么服务,而SASL是认证过程,侧重于信任建立过程,
#这个过程可以调用PAM来建立信任关系。在这里Memcached就是上面提到的应用层,具体的认证交给SASL库,SASL会根据相应的认证机制来完成验证功能。

phpize 的时候,提示: Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.

yum install m4
yum install autoconf

提示: checking for igbinary includes... configure: error: Cannot find igbinary.h

yum install m4
wget https://pecl.php.net/get/igbinary-3.0.1.tgz

tar -xvf igbinary-3.0.1.tgz 
cd igbinary-3.0.1

/Data/apps/php7.3.3/bin/phpize
./configure --prefix=/usr/local/igbinary-2.0.8 --with-php-config=/Data/apps/php7.3.3/bin/php-config

make && make install 
----------
----------------------------------------------------------------------
Libraries have been installed in:
   /Data/tools/igbinary-3.0.1/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /Data/apps/php7.3.3/lib/php/extensions/no-debug-non-zts-20180731/
Installing header files:          /Data/apps/php7.3.3/include/php/
----------------------------------------------------------------------
php.ini 加载 extension=igbinary.so

最终加入 extension=memcached.so 即可,再执行 php -m 查看是否生效。



发表评论