Mac 编译安装 PHPRedis 模块
本地配置
PHP 版本
PHP 7.40
通过 HomeBrew
安装。
~ php -v
PHP 7.4.0 (cli) (built: Nov 29 2019 16:18:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.0, Copyright (c), by Zend Technologies
PHPRedis
模块地址
https://github.com/phpredis/phpredis
寻找与当前 PHP
版本相匹配的 phpredis
版本,此时直接使用 develop
版本即可。
安装
因为在编译安装 phpredis
过程中需要使用到 PHP
的几个配置文件,所以通过命令 brew list php
查看下 PHP
安装的主要文件所在的路径:
~ brew list php
/usr/local/Cellar/php/7.4.0/.bottle/etc/ (4 files)
/usr/local/Cellar/php/7.4.0/.bottle/var/log/php-fpm.log
/usr/local/Cellar/php/7.4.0/bin/pear
/usr/local/Cellar/php/7.4.0/bin/peardev
/usr/local/Cellar/php/7.4.0/bin/pecl
/usr/local/Cellar/php/7.4.0/bin/phar
/usr/local/Cellar/php/7.4.0/bin/phar.phar
/usr/local/Cellar/php/7.4.0/bin/php
/usr/local/Cellar/php/7.4.0/bin/php-cgi
/usr/local/Cellar/php/7.4.0/bin/php-config // 需要用到这个
/usr/local/Cellar/php/7.4.0/bin/phpdbg
/usr/local/Cellar/php/7.4.0/bin/phpize // 需要用到这个
/usr/local/Cellar/php/7.4.0/homebrew.mxcl.php.plist
/usr/local/Cellar/php/7.4.0/include/php/ (312 files)
/usr/local/Cellar/php/7.4.0/lib/httpd/modules/libphp7.so
/usr/local/Cellar/php/7.4.0/lib/php/ (15 files)
/usr/local/Cellar/php/7.4.0/pecl -> /usr/local/lib/php/pecl // 需要用到这个
/usr/local/Cellar/php/7.4.0/sbin/php-fpm
/usr/local/Cellar/php/7.4.0/share/man/ (8 files)
/usr/local/Cellar/php/7.4.0/share/php/ (159 files)
关键的PHP文件、文件夹说明
phpize
:phpize
是用来扩展PHP
扩展模块的,通过phpize
可以建立PHP
的外挂模块,
比如你想在原来编译好的PHP
中加入memcached
或者ImageMagick
等扩展模块,可以使用phpize
;php-config
:PHP
安装完后在 bin 目录下有个php-config
,php-config
是一个脚本文件,用于获取所安装的PHP
配置的信息;pecl
: PHP Extension Community Library(PHP
社区扩展库),管理底层的PHP
扩展,用 C 或者 C++编写外部模块然后加载至 PHP 中,如redis
,xdebug
等模块。简而言之,就是存放扩展模块的,这次我们安装的 redis.so 文件就会存放在这里。
安装 phpredis
-
下载 phpredis:
任意目录使用
git
直接下载即可:~ git clone https://github.com/phpredis/phpredis
-
进入 phpredis 目录:
~ cd phpredis
-
进行编译安装🧬
~ sudo /usr/local/Cellar/php/7.4.0/bin/phpize Configuring for: PHP Api Version: 20190902 Zend Module Api No: 20190902 Zend Extension Api No: 320190902 ~ ./configure --with-php-config=/usr/local/Cellar/php/7.4.0/bin/php-config . . . . creating libtool appending configuration tag "CXX" to libtool configure: patching config.h.in configure: creating ./config.status config.status: creating config.h config.status: config.h is unchanged ~ make && make install /bin/sh /Users/huanglongfei/Sites/ValetSites/restful-api/phpredis/libtool --mode=install cp ./redis.la /Users/huanglongfei/Sites/ValetSites/restful-api/phpredis/modules cp ./.libs/redis.so /Users/huanglongfei/Sites/ValetSites/restful-api/phpredis/modules/redis.so cp ./.libs/redis.lai /Users/huanglongfei/Sites/ValetSites/restful-api/phpredis/modules/redis.la ---------------------------------------------------------------------- Libraries have been installed in: /Users/huanglongfei/Sites/ValetSites/restful-api/phpredis/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 `DYLD_LIBRARY_PATH' environment variable during execution 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: /usr/local/Cellar/php/7.4.0/pecl/20190902/
可以看到,最后安装位置为
/usr/local/Cellar/php/7.4.0/pecl/20190902/
。配置 PHP,开启 redis.so 扩展
- 编辑
php.ini
文件;
此处当存在多个PHP版本时,一定要改对自己需要的版本。
~ vim /usr/local/etc/php/7.4/php.ini
-
在
Dynamic Extensions
配置组下,添加一行extension=redis.so
; -
查看 ·
extension_dir
配置,也就是扩展目录配置是否与实际的相同,不同则进行更改,此处应为/usr/local/Cellar/php/7.4.0/pecl/20190902/
。 -
重启
PHP
和PHP-FPM
。
查看是否安装成功
执行
php -m
命令即可:~ php -m . . . readline redis Reflection session shmop . . .
安装成功!
- 编辑
本作品采用《CC 协议》,转载必须注明作者和本文链接
规范并优化优雅加载配置处理,路径根据实际情况修改
Mac 下我习惯用 pecl 直接安装
为什么失败呢
这篇文章,对我最大的帮助就是,安装扩展的时候,一定要看好自己 PHP 的安装位置,最终要给那个版本的 PHP 装扩展。
我碰到编译安装错误
configure: error: invalid value of canonical build
搜了下改为:
./configure --with-php-config=/usr/local/Cellar/php/7.4.10/bin/php-config
然后就顺利安装了