swoole[1]安装

编程

之前我们提到过PHP协程机制,而swoole就是一个PHP协程框架,参考官方文档  https://wiki.swoole.com/#/environment。我们来安装一下

1,源文件下载

git clone https://gitee.com/swoole/swoole.git

Cloning into "swoole"...

remote: Enumerating objects: 5370, done.

remote: Counting objects: 100% (5370/5370), done.

remote: Compressing objects: 100% (1798/1798), done.

remote: Total 77007 (delta 4632), reused 3839 (delta 3554), pack-reused 71637

Receiving objects: 100% (77007/77007), 24.47 MiB | 2.68 MiB/s, done.

Resolving deltas: 100% (59152/59152), done.

Checking connectivity... done.

2,安装

从 README-CN.md文件中,可以看到安装步骤

### 1. 使用PHP官方的PECL工具安装 (初学者)

```shell

pecl install swoole

```

### 2. 从源码编译安装 (推荐)

> 非内核开发研究之用途, 请下载[发布版本](https://github.com/swoole/swoole-src/releases)的源码编译

```shell

cd swoole-src &&

phpize &&

./configure &&

make && sudo make install

```

#### 启用扩展

编译安装到系统成功后, 需要在`php.ini`中加入一行`extension=swoole.so`来启用Swoole扩展

这里选择的是源码编译安装。

2.1 源码安装

cd swoole

进行编译 

phpize7.3

Configuring for:

PHP Api Version: 20180731

Zend Module Api No: 20180731

Zend Extension Api No: 320180731

autoconf: warning: both `configure.ac" and `configure.in" are present.

autoconf: warning: proceeding with `configure.ac".

autoheader: "configure.ac" and "configure.in" both present.

autoheader: proceeding with "configure.ac"

配置

./configure

checking for grep that handles long lines and -e... /bin/grep

checking for egrep... /bin/grep -E

checking for a sed that does not truncate output... /bin/sed

checking for cc... cc

checking whether the C compiler works... yes

.

.

.

configure: creating ./config.status

config.status: creating config.h

config.status: executing libtool commands

编译安装

make && suo make install

/bin/bash /opt/wwwroot/swoole/libtool --mode=compile g++ -I. -I/opt/wwwroot/swoole -DPHP_ATOM_INC

.

.

.

中间提示,库以及安装在 目录 下

----------------------------------------------------------------------

Libraries have been installed in:

/opt/wwwroot/swoole/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 "LD_LIBRARY_PATH" environment variable

during execution

- add LIBDIR to the "LD_RUN_PATH" environment variable

during linking

- use the "-Wl,-rpath -Wl,LIBDIR" linker flag

- have your system administrator add LIBDIR to "/etc/ld.so.conf"

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".

[sudo] password for c80k2:

Installing shared extensions: /usr/lib/php/20180731/

Installing header files: /usr/include/php/20180731/

安装了 扩展 和 头文件,还提醒,别忘了运行 make test 命令。

2.2 加入php扩展

安装好后,需要将swoole.so 扩展添加到 php.ini 文件中,使其生效。在我的ubuntu本地,php7.3 有cli 和 fpm,两者都有各自的php.ini文件,所以我这里就以fpm为例来演示。有其他环境可以直接找到 php.ini 文件,将其添加进去。

在我的本地,/etc/php/7.3/fpm 目录下有以下几个文件

ls -al

total 168

drwxr-xr-x 4 root root 4096 6月 3 19:55 .

drwxr-xr-x 6 root root 4096 6月 3 2019 ..

drwxr-xr-x 2 root root 4096 6月 3 16:04 conf.d

-rw-r--r-- 1 root root 5337 5月 9 12:21 php-fpm.conf

-rw-r--r-- 1 root root 71958 5月 14 20:42 php.ini

-rw-r--r-- 1 root root 72651 5月 18 16:00 php.ini.ucf-old

drwxr-xr-x 2 root root 4096 6月 3 19:52 pool.d

在 conf.d 目录中,有很多这种软链接:

lrwxrwxrwx 1 root root 39 5月 27 2019 10-mysqlnd.ini -> /etc/php/7.3/mods-available/mysqlnd.ini

lrwxrwxrwx 1 root root 39 5月 27 2019 10-opcache.ini -> /etc/php/7.3/mods-available/opcache.ini

lrwxrwxrwx 1 root root 35 5月 27 2019 10-pdo.ini -> /etc/php/7.3/mods-available/pdo.ini

所以,我们也要在这里创建一个 swoole配置的软链接。先创建源文件

sudo vi /etc/php/7.3/mods-available/swoole.ini

 在里面写入扩展

; configuration for php swoole module

; priority=20

extension=swoole.so

 这里,其实就是将编译出来的 swoole.so 从前面的 phpize7.3 中显示的 /usr/lib/php/20180731/ 中的 swoole.so 扩展,添加到 php.ini 里

/usr/lib/php/20180731

-rw-r--r-- 1 root root 88168 5月 14 20:43 sockets.so

-rwxr-xr-x 1 root root 14258264 6月 3 13:56 swoole.so

-rw-r--r-- 1 root root 14440 5月 14 20:43 sysvmsg.so

然后在  /etc/php/7.3/fpm/conf.d 中创建软链接

ln -s /etc/php/7.3/mods-available/swoole.ini 20-swoole.ini

同时在 cli 中也做相同操作。执行完后,重启php,看一下扩展情况:

service php7.3-fpm reload

php -m

[PHP Modules]

bcmath

calendar

Core

ctype

.

.

.

swoole

.

.

.

[Zend Modules]

Zend OPcache

看到已经有swoole扩展了。

2.3 make test

上面提到,swoole 编译安装完成后,提示了 make test 去做单元测试,我们来继续看看。

Build complete.

Don"t forget to run "make test".

=====================================================================

PHP : /usr/bin/php7.3

PHP_SAPI : cli

PHP_VERSION : 7.3.18-1+ubuntu16.04.1+deb.sury.org+1

ZEND_VERSION: 3.3.18

PHP_OS : Linux - Linux ubuntu 4.15.0-101-generic #102~16.04.1-Ubuntu SMP Mon May 11 11:38:16 UTC 2020 x86_64

INI actual : /opt/wwwroot/swoole/tmp-php.ini

More .INIs :

CWD : /opt/wwwroot/swoole

Extra dirs :

VALGRIND : Not used

=====================================================================

TIME START 2020-06-03 06:40:02

=====================================================================

SKIP swoole_websocket_server: websocket server set cookie on handshake (#3270)

[tests/swoole_websocket_server/set_cookie_on_hanshake.phpt] reason:

please read /opt/wwwroot/swoole/tests/README.md and try again

=====================================================================

TIME END 2020-06-03 08:26:14

=====================================================================

TEST RESULT SUMMARY

---------------------------------------------------------------------

Exts skipped : 0

Exts tested : 15

---------------------------------------------------------------------

Number of tests : 939 0

Tests skipped : 939 (100.0%) --------

Tests warned : 0 ( 0.0%) ( 0.0%)

Tests failed : 0 ( 0.0%) ( 0.0%)

Expected fail : 0 ( 0.0%) ( 0.0%)

Tests passed : 0 ( 0.0%) ( 0.0%)

---------------------------------------------------------------------

Time taken : 18 seconds

=====================================================================

This report can be automatically sent to the PHP QA team at

http://qa.php.net/reports and http://news.php.net/php.qa.reports

This gives us a better understanding of PHP"s behavior.

If you don"t want to send the report immediately you can choose

option "s" to save it. You can then email it to qa-reports@lists.php.net later.

Do you want to send this report now? [Yns]: N

可以看到,这里所有的测试都被跳过了。提示查看文件 /opt/wwwroot/swoole/tests/README.md

## Preparation

try to run `./init` to initialize the databases.

| | mysql | redis |

| ------------- | ------------------------------------- | ----------------------------------- |

| path (env) | $MYSQL_SERVER_PATH | $REDIS_SERVER_PATH |

| path (travis) | ${travis}/data/run/mysqld/mysqld.sock | ${travis}/data/run/redis/redis.sock |

| host (raw) | 127.0.0.1 | 127.0.0.1 |

| host (docker) | mysql | redis |

| port | 3306 | 6379 |

| user | root | - |

| password | root | root (optional) |

| database | test | 0 |

## How to run

1. just run `./start.sh`

2. or use`./start.sh ./swoole_*` command to run a part of the tests

3. or use `./start.sh base` command to run base tests

看到上面的 Preparation,需要在mysql 和 redis 中做相关的配置。再执行 ./init 命令。

c80k2@ubuntu❯/opt/wwwroot/swoole/tests(master)$ ./init

[DB-init] initialization MySQL database...

[DB-init] Done!

再执行 ./start.sh ,就会出现相应的测试数据。

=====================================================================

PHP : /usr/bin/php

PHP_SAPI : cli

PHP_VERSION : 7.3.18-1+ubuntu16.04.1+deb.sury.org+1

ZEND_VERSION: 3.3.18

PHP_OS : Linux - Linux ubuntu 4.15.0-101-generic #102~16.04.1-Ubuntu SMP Mon May 11 11:38:16 UTC 2020 x86_64

INI actual : /etc/php/7.3/cli/php.ini

More .INIs : /etc/php/7.3/cli/conf.d/10-mysqlnd.ini,/etc/php/7.3/cli/conf.d/10-opcache.ini,/etc/php/7.3/cli/conf.d/10-pdo.ini,/etc/php/7.3/cli/conf.d/15-xml.ini,/etc/php/7.3/cli/conf.d/20-bcmath.ini,/etc/php/7.3/cli/conf.d/20-calendar.ini,/etc/php/7.3/cli/conf.d/20-ctype.ini,/etc/php/7.3/cli/conf.d/20-curl.ini,/etc/php/7.3/cli/conf.d/20-dom.ini,/etc/php/7.3/cli/conf.d/20-exif.ini,/etc/php/7.3/cli/conf.d/20-fileinfo.ini,/etc/php/7.3/cli/conf.d/20-ftp.ini,/etc/php/7.3/cli/conf.d/20-gd.ini,/etc/php/7.3/cli/conf.d/20-gettext.ini,/etc/php/7.3/cli/conf.d/20-iconv.ini,/etc/php/7.3/cli/conf.d/20-igbinary.ini,/etc/php/7.3/cli/conf.d/20-json.ini,/etc/php/7.3/cli/conf.d/20-ldap.ini,/etc/php/7.3/cli/conf.d/20-mbstring.ini,/etc/php/7.3/cli/conf.d/20-mysqli.ini,/etc/php/7.3/cli/conf.d/20-pdo_mysql.ini,/etc/php/7.3/cli/conf.d/20-phar.ini,/etc/php/7.3/cli/conf.d/20-posix.ini,/etc/php/7.3/cli/conf.d/20-readline.ini,/etc/php/7.3/cli/conf.d/20-redis.ini,/etc/php/7.3/cli/conf.d/20-shmop.ini,/etc/php/7.3/cli/conf.d/20-simplexml.ini,/etc/php/7.3/cli/conf.d/20-sockets.ini,/etc/php/7.3/cli/conf.d/20-swoole.ini,/etc/php/7.3/cli/conf.d/20-sysvmsg.ini,/etc/php/7.3/cli/conf.d/20-sysvsem.ini,/etc/php/7.3/cli/conf.d/20-sysvshm.ini,/etc/php/7.3/cli/conf.d/20-tokenizer.ini,/etc/php/7.3/cli/conf.d/20-wddx.ini,/etc/php/7.3/cli/conf.d/20-xmlreader.ini,/etc/php/7.3/cli/conf.d/20-xmlrpc.ini,/etc/php/7.3/cli/conf.d/20-xmlwriter.ini,/etc/php/7.3/cli/conf.d/20-xsl.ini,/etc/php/7.3/cli/conf.d/20-zip.ini

CWD : /opt/wwwroot/swoole/tests

Extra dirs :

VALGRIND : Not used

=====================================================================

Running selected tests.

TEST 1/939 [PASS] swoole_atomic: add/sub/get/cmpset [swoole_atomic/atomic.phpt]

TEST 2/939

========DIFF========

002+

003+ Assert failed: in /opt/wwwroot/swoole/tests/swoole_atomic/multi_wakeup.php on line 9

004+ Stack trace:

005+ #0 /opt/wwwroot/swoole/tests/include/lib/src/Assert.php(203): SwooleTestAssert::reportInvalidArgument("")

006+ #1 /opt/wwwroot/swoole/tests/swoole_atomic/multi_wakeup.php(9): SwooleTestAssert::assert(false)

007+ #2 [internal function]: {closure}(99639)

008+ #3 /opt/wwwroot/swoole/tests/include/lib/src/ProcessManager.php(115): call_user_func(Object(Closure), 99639)

009+ #4 /opt/wwwroot/swoole/tests/include/lib/src/ProcessManager.php(249): SwooleTestProcessManager->runParentFunc(99639)

010+ #5 /opt/wwwroot/swoole/tests/swoole_atomic/multi_wakeup.php(23): SwooleTestProcessManager->run()

011+ #6 {main}

========DONE========

[FAIL] swoole_atomic: multi wakeup [swoole_atomic/multi_wakeup.phpt]

TEST 3/939 [PASS] swoole_atomic: wakeup & wait [swoole_atomic/wait.phpt]

TEST 4/939 [PASS] swoole_atomic: wakeup & wait ex [swoole_atomic/wait_ex.phpt]

TEST 5/939 [SKIP] swoole_channel_coro: consumer first with select mode [swoole_channel

和之前的全部 SKIP 不同,这里大部分是PASS,还有一些 FAILED,需要去做处理。

 

至此,我们算是把 swoole 安装好了。

以上是 swoole[1]安装 的全部内容, 来源链接: utcz.com/z/517103.html

回到顶部