菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
38
0

Mac 编译安装 PHPRedis 模块

原创
05/13 14:22
阅读数 351

本地配置

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 协议》,转载必须注明作者和本文链接

发表评论

0/200
38 点赞
0 评论
收藏
为你推荐 换一批