报错场景:

安装swoole后查看swoole详情

php --ri swoole

遇到报错

PHP Warning:  PHP Startup: Unable to load dynamic library 'swoole' (tried: /usr/lib64/php/modules/swoole (/usr/lib64/php/modules/swoole: cannot open shared object file: No such file or directory), /usr/lib64/php/modules/swoole.so (/usr/lib64/php/modules/swoole.so: undefined symbol: php_sockets_le_socket)) in Unknown on line 0
Extension 'swoole' not present.

错误原因分析:

报错未定义 php_sockets_le_socket。

因为编译swoole的时候开启了 --enable-sockets ,而swoole-scokets是需要依赖php-scokets扩展的,开始查找php-scokets扩展的原因。

查看php安装的扩展,发现scokets扩展是已经安装的。

查看php.ini的加载顺序

php --ini

发现引入php.ini后下面又引入了/etc/php.d/目录下的一些配置文件,其中有sockets.ini文件

[root@6d5cf7b754b6 /]# php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:      /etc/php.d/20-bz2.ini,
/etc/php.d/20-calendar.ini,
/etc/php.d/20-ctype.ini,
/etc/php.d/20-curl.ini,
/etc/php.d/20-dom.ini,
/etc/php.d/20-exif.ini,
/etc/php.d/20-fileinfo.ini,
/etc/php.d/20-ftp.ini,
/etc/php.d/20-gettext.ini,
/etc/php.d/20-iconv.ini,
/etc/php.d/20-json.ini,
/etc/php.d/20-mbstring.ini,
/etc/php.d/20-pdo.ini,
/etc/php.d/20-phar.ini,
/etc/php.d/20-simplexml.ini,
/etc/php.d/20-sqlite3.ini,
/etc/php.d/20-sockets.ini,
/etc/php.d/20-tokenizer.ini,
/etc/php.d/20-xml.ini,
/etc/php.d/20-xmlwriter.ini,
/etc/php.d/20-xsl.ini,
/etc/php.d/30-mysqli.ini,
/etc/php.d/30-pdo_mysql.ini,
/etc/php.d/30-pdo_sqlite.ini,
/etc/php.d/30-xmlreader.ini,
/etc/php.d/40-zip.ini

看到这里就能理解报错原因了。php先加载了swoole扩展其次才加载sockets扩展,而swoole是需要依赖scokets扩展的,所以才报错sockets不可用。

解决方案:

删除sockets.ini文件

rm -rf /etc/php.d/20-sockets.ini

添加 extension=sockets 到php.ini。(需要放到swoole之前)

extension=sockets
extension=swoole

其他:

如果遇到类似json,mysqlnd之类的报错同上处理即可

07-19 13:29