PHP 三方扩展组件的安装

| 选择喜欢的代码风格  

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安装Memcache扩展


wget https://pecl.php.net/get/memcache-2.2.7.tgz
tar -zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
 ./configure --enable-memcache=/usr/local/memcached \
--with-php-config=/usr/local/php/bin/php-config

make && make install

#编辑php.ini文件
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

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

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

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
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
Active persistent connections => 0
Version => 2.2.7
Revision => $Revision: 327750 $

Directive => Local Value => Master Value
memcache.allow_failover => 1 => 1
memcache.max_failover_attempts => 20 => 20
memcache.default_port => 11211 => 11211
memcache.chunk_size => 8192 => 8192
memcache.hash_strategy => standard => standard
memcache.hash_function => crc32 => crc32

memcache 扩展安装的坑:


如果是 PHP 7.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.3 为 PHP 的安装路径,需要根据你安装的实际目录调整
[root@LoadBalancing /Data/tools/php-memcached]
/Data/apps/php7.3.3/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.3/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 查看是否生效。



发表评论