公司新开发的项目上线了,出于好奇心,打算将网站服务改为swoole测一下性能,正好换了个新电脑,可以从头安装一下Swoole扩展,却发现苹果电脑M1芯片安装起扩展来有很多坑,花了一下午时间才搞好,网络上的解决方案都不全或者很分散,在此整合记录一下。

一、下载

Mac上安装扩展有两种方式,可以用PHP自带的PECL工具,也可以源码编译安装,源码下载地址:

二、安装

1. PECL工具安装

比较推荐PECL方式安装,可以省去一些M1芯片带来的兼容性问题,相比源码方式简便许多。

执行命令

sudo pecl install swoole

2.源码安装

进入源码文件夹,源码文件夹可以解压下载的源码压缩包得到

cd swoole-src

按顺序执行命令

sudo phpize
sudo ./configure --with-php-config=/opt/homebrew/opt/php@7.3/bin/php-config --enable-sockets=yes --enable-openssl=yes --with-openssl-dir=/opt/homebrew/opt/openssl@1.1 --enable-http2=yes
sudo make
sudo make install

安装成功后会打印出安装好的swoole.so的位置,在php.ini中加入以下语句启用扩展

extension=swoole.so

重启PHP,看一下php -m中是否有swoole,有就说明成功了

三、常见异常

  1. 使用pecl安装的时候会提示你需要开启的功能,如果开启openssl的话,使用默认的设置会报错,这是因为默认的openssl是brew安装的,文件位置和pecl默认的文件位置不同,会提示无法找到openssl,这时需要找到你电脑上openssl实际的安装位置,我的是/opt/homebrew/opt/openssl@1.1,具体操作如下:
	enable sockets supports? [no] : yes
	enable openssl support? [no] : yes --with-openssl-dir=/opt/homebrew/opt/openssl@1.1
	enable http2 support? [no] : yes
	enable mysqlnd support? [no] : yes
	enable json support? [no] : yes
	enable curl support? [no] : yes
  1. 提示fatal error: 'pcre2.h' file not found。这是由于M1芯片缺少pcre软连接,解决方法:
ln -s /opt/homebrew/include/pcre2.h /opt/homebrew/Cellar/php@7.4/7.4.16/include/php/ext/pcre/pcre2.h
  1. 安装扩展后,运行PHP时提示mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e))),注意need后的arm64e。这是由于我的电脑的架构是arm64e的,需要使用相同的架构来安装扩展。
  • 查看本机系统信息
uname -a
  • 得到结果 x86_64
Darwin songruideMacBook-Pro.local 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000 x86_64
  • 安装扩展
arch -arm64e sudo pecl install swoole
  • 如果是arm64e则
arch -x86_64 sudo pecl install swoole
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/iwillrich/p/16821321.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程