Mac安装swoole并解决安装错误

Song2807 次浏览0个评论2020年05月11日

一、Swoole安装

1、PHP版本条件

  • Swoole-1.x需要 PHP-5.3.10 或更高版本
  • Swoole-2.x需要 PHP-7.0.0 或更高版本
  • Swoole-4.x需要 PHP-7.1.0 或更高版本

1、源码安装

前往swoole/swoole-src下载源码。前往安装:

cd swoole-src && \
phpize && \
./configure && \
make && sudo make install

注意要查看查看结尾安装路径,然后要注意一下Warning: mkdir(): File exists in System.php提示不影响安装使用。

Build process completed successfully
Installing '/usr/local/Cellar/php@7.3/7.3.17_1/include/php/ext/swoole/config.h'
Installing '/usr/local/Cellar/php@7.3/7.3.17_1/pecl/20180731/swoole.so'

Warning: mkdir(): File exists in System.php on line 294
PHP Warning:  mkdir(): File exists in /usr/local/Cellar/php@7.3/7.3.17_1/share/php@7.3/pear/System.php on line 294

Warning: mkdir(): File exists in /usr/local/Cellar/php@7.3/7.3.17_1/share/php@7.3/pear/System.php on line 294
ERROR: failed to mkdir /usr/local/Cellar/php@7.3/7.3.17_1/pecl/20180731

2、使用swoole安装

pecl install swoole

注意要查看查看结尾安装路径,然后要注意一下Warning: mkdir(): File exists in System.php提示不影响安装使用。

/bin/sh /private/var/www/swoole-src-4.5.0/libtool --mode=install cp ./swoole.la /private/var/www/swoole-src-4.5.0/modules
cp ./.libs/swoole.so /private/var/www/swoole-src-4.5.0/modules/swoole.so
cp ./.libs/swoole.lai /private/var/www/swoole-src-4.5.0/modules/swoole.la
----------------------------------------------------------------------
Libraries have been installed in:
   /private/var/www/swoole-src-4.5.0/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'.

Password:
mkdir: /usr/local/Cellar/php@7.3/7.3.17_1/pecl: File exists
mkdir: /usr/local/Cellar/php@7.3/7.3.17_1/pecl: No such file or directory
make: *** [install-modules] Error 1

3、PHP中引入配置文件

然后通过php -i | grep php.ini定位php.ini文件所在位置,并打开该配置文件,在文件末尾shift + G追加如下内容:

[swoole]
extension=/private/var/www/swoole-src-4.5.0/modules/swoole.so

最后重启一下php即可:

sudo service php7.3-fpm restart

执行php-m看到swoole代表已成功安装。

4、异常解决方案

如果引入失败出现PHP Warning: PHP Startup: Unable to load dynamic library '/ussr/lib/php/20180731/swoole.so'报错,则是swoole.so文件位置不对。可以前往/usr/lib/php/的时间文件下查找是否有swoole.so如:

/usr/lib/php/20180731/

最后记得重启php

二、快速上手

在test.php中写入如下代码。

<?php

// 表明服务器启动后监听本地 9051 端口
$server = new swoole_http_server('127.0.0.1', 9501);

// 服务器启动时返回响应
$server->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

// 向服务器发送请求时返回响应
// 可以获取请求参数,也可以设置响应头和响应内容
$server->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

// 启动 HTTP 服务器
$server->start();

执行php test.php即可。

\屏幕快照 2020-05-11 18.43.55.png

这样,表示服务器已经启动并且在监听请求了,到浏览器中访问 http://127.0.0.1:9501,即可获取服务器输出响应内容

\64ff8a0a7535acf1e7c7e742932ab1f2.jpg

提交评论

请登录后评论

用户评论

    当前暂无评价,快来发表您的观点吧...

更多相关好文

    当前暂无更多相关好文推荐...