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 版本即可。

20191231191008.png

安装

因为在编译安装 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-configphp-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/

    • 重启 PHPPHP-FPM

    查看是否安装成功

    执行 php -m 命令即可:

    ~ php -m
    .
    .
    .
    readline
    redis
    Reflection
    session
    shmop
    .
    .
    .

    安装成功!

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5

规范并优化优雅加载配置处理,路径根据实际情况修改

echo extension=redis.so >> /etc/php/7.2/mods-available/redis.ini
ln -s /etc/php/7.2/mods-available/redis.ini /etc/php/7.2/cli/conf.d/redis.ini
ln -s /etc/php/7.2/mods-available/redis.ini /etc/php/7.2/fpm/conf.d/redis.ini
4年前 评论
alalala (楼主) 4年前
alalala (楼主) 4年前
AlicFeng (作者) 4年前

Mac 下我习惯用 pecl 直接安装

$ pecl install redis
4年前 评论
alalala (楼主) 4年前

为什么失败呢

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:     /usr/lib64/php/modules/
[root@VM_0_11_centos phpredis]# php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
修改php.ini
[root@VM_0_11_centos phpredis]# grep redis /etc/php.ini
extension=/usr/lib64/php/modules/redis.so
[root@VM_0_11_centos phpredis]# php -m
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/redis.so' (tried: /usr/lib64/php/modules/redis.so (/usr/lib64/php/modules/redis.so: undefined symbol: php_json_decode_ex), /usr/lib64/php/modules//usr/lib64/php/modules/redis.so.so (/usr/lib64/php/modules//usr/lib64/php/modules/redis.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
4年前 评论
alalala (楼主) 4年前
lovecn (作者) 4年前
alalala (楼主) 4年前

这篇文章,对我最大的帮助就是,安装扩展的时候,一定要看好自己 PHP 的安装位置,最终要给那个版本的 PHP 装扩展。

3年前 评论

我碰到编译安装错误
configure: error: invalid value of canonical build
搜了下改为:
./configure --with-php-config=/usr/local/Cellar/php/7.4.10/bin/php-config
然后就顺利安装了

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!