背景

网上很多教程基于macos intel芯片的,目前m1芯片网上几乎没有
我打开命令行窗口,执行一条命令
image.png
php -v
得到的输出是

  1. WARNING: PHP is not recommended
  2. PHP is included in macOS for compatibility with legacy software.
  3. Future versions of macOS will not include PHP.
  4. PHP 7.3.24-(to be removed in future macOS) (cli) (built: Nov 23 2020 06:45:16) ( NTS )
  5. Copyright (c) 1997-2018 The PHP Group
  6. Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

PS:如果没有安装brew 参考我的 brew 安装教程
意思是说本身就带了php7.3,未来的版本可能会去掉,此时我执行命令

安装

安装PHP7.4

  1. brew install php@7.4

为了覆盖本地PHP 环境信息(这里的7.4.20 是我安装的版本 )

  1. Version=$(ls -al /opt/homebrew/Cellar/php@7.4/ | awk '{print $9}' | grep 7.4)
  2. echo "PATH=/opt/homebrew/Cellar/php@7.4/$Version/bin:/opt/homebrew/Cellar/php@7.4/$Version/sbin:$PATH" >> ~/.zshrc
  3. source ~/.zshrc
  4. php -v # 信息应为 php7.4.20

启动

  1. brew services start php@7.4

安装依赖(M1特有)

安装YAF

  1. sudo pecl install yaf

遇到问题查看下面报错fatal error: ‘pcre2.h’

报错fatal error: ‘pcre2.h’

  1. ## 例子
  2. In file included from /private/tmp/pear/temp/swoole/ext-src/php_swoole.cc:21:
  3. /usr/local/Cellar/php/8.0.0_1/include/php/ext/pcre/php_pcre.h:23:10: fatal error: 'pcre2.h' file not found
  4. #include "pcre2.h"
  5. ^~~~~~~~~
  6. 1 error generated.
  7. make: *** [ext-src/php_swoole.lo] Error 1
  8. ERROR: `make' failed

这块一直error 原因你本地没有对应的引用 需要你本地进行一下软连接
新版本M1芯片 在/opt/homebrew地方

1.递归创建目录

  1. mkdir -p /opt/homebrew/Cellar/php@7.4/7.4.20/include/php/ext/pcre/

2.软连接操作

  1. ln -s /opt/homebrew/Cellar/pcre2/10.37/include/pcre2.h /opt/homebrew/Cellar/php@7.4/7.4.20/include/php/ext/pcre/pcre2.h

PS:后续使用会警告

老PHP程序员告诉我不用管,可以忽略

  1. PHP Warning: PHP Startup: Unable to load dynamic library 'yaf.so' (tried: /opt/homebrew/lib/php/pecl/20190902/yaf.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/yaf.so, 9): no suitable image found. Did find:
  2. /opt/homebrew/lib/php/pecl/20190902/yaf.so: mach-o, but wrong architecture
  3. /opt/homebrew/lib/php/pecl/20190902/yaf.so: stat() failed with errno=22), /opt/homebrew/lib/php/pecl/20190902/yaf.so.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/yaf.so.so, 9): image not found)) in Unknown on line 0
  4. Warning: PHP Startup: Unable to load dynamic library 'yaf.so' (tried: /opt/homebrew/lib/php/pecl/20190902/yaf.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/yaf.so, 9): no suitable image found. Did find:
  5. /opt/homebrew/lib/php/pecl/20190902/yaf.so: mach-o, but wrong architecture
  6. /opt/homebrew/lib/php/pecl/20190902/yaf.so: stat() failed with errno=22), /opt/homebrew/lib/php/pecl/20190902/yaf.so.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/yaf.so.so, 9): image not found)) in Unknown on line 0
  7. PHP Warning: PHP Startup: Unable to load dynamic library 'swoole.so' (tried: /opt/homebrew/lib/php/pecl/20190902/swoole.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/swoole.so, 9): no suitable image found. Did find:
  8. /opt/homebrew/lib/php/pecl/20190902/swoole.so: mach-o, but wrong architecture
  9. /opt/homebrew/lib/php/pecl/20190902/swoole.so: mach-o, but wrong architecture), /opt/homebrew/lib/php/pecl/20190902/swoole.so.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/swoole.so.so, 9): image not found)) in Unknown on line 0
  10. Warning: PHP Startup: Unable to load dynamic library 'swoole.so' (tried: /opt/homebrew/lib/php/pecl/20190902/swoole.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/swoole.so, 9): no suitable image found. Did find:
  11. /opt/homebrew/lib/php/pecl/20190902/swoole.so: mach-o, but wrong architecture
  12. /opt/homebrew/lib/php/pecl/20190902/swoole.so: mach-o, but wrong architecture), /opt/homebrew/lib/php/pecl/20190902/swoole.so.so (dlopen(/opt/homebrew/lib/php/pecl/20190902/swoole.so.so, 9): image not found)) in Unknown on line 0
  13. PHP 7.4.20 (cli) (built: Jun 3 2021 19:10:14) ( NTS )
  14. Copyright (c) The PHP Group
  15. Zend Engine v3.4.0, Copyright (c) Zend Technologies
  16. with Zend OPcache v7.4.20, Copyright (c), by Zend Technologies

参考:
https://github.com/swoole/swoole-src/issues/3926
https://blog.csdn.net/silk_java/article/details/111769920