背景说明:

公司现有项目是基于laravle5.5开发的,随着业务的增长php及框架的短板也凸显出来,于是准备使用swoole扩展来解决并发问题,以下是记录安装swoole的过程。

环境说明及版本说明:

Homestead v10.7.0
swoole v4.4.17

php v7.3

1. 安装 swoole

[email protected]:~$ sudo pecl install swoole

  • 遇到以下错误

WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
Could not download from "https://pecl.php.net/get/mongodb-1.5.5.tgz", cannot download "pecl/mongodb" (Connection to `ssl://pecl.php.net:443' failed: ) Error: cannot download "pecl/mongodb" Download failed
install failed

  • 解决方法:

[email protected]:~$ sudo pecl channel-update pecl.php.net

  • 重新安装

[email protected]:~$ sudo pecl install swoole

安装的时候会有 yes/no 之类的选项,直接 enter 就可以了
出现如下文字说明已经安装成功:

Build process completed successfully
Installing '/usr/lib/php/20180731/swoole.so' Installing '/usr/include/php/20180731/ext/swoole/config.h' install ok: channel://pecl.php.net/swoole-4.4.17 configuration option "php_ini" is not set to php.ini location
You should add "extension=swoole.so" to php.ini

2. 开启扩展

  • 查看当前 PHP 版本

[email protected]:~$ php -v PHP 7.3.7-2+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Jul 25 2019 11:44:59) ( NTS ) Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
  with Zend OPcache v7.3.7-2+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

  • 创建配置文件

[email protected]:~$ sudo touch /etc/php/7.3/mods-available/swoole.ini
  • 写入以下内容

    extension=swoole.so
  • 添加软连接

[email protected]:/etc/php/7.3/cli/conf.d$ sudo ln -s /etc/php/7.3/mods-available/swoole.ini ./20-swoole.ini
  • 重启 php-fpm

    sudo service php7.3-fpm restart
  • 查看扩展

[email protected]:/etc/php/7.3/cli/conf.d$ php --ri swoole

如果出现以下文字说明已经安装成功

swoole
Swoole => enabled
Author => Swoole Team <[email protected].com> Version => 4.4.17 Built => Apr 17 2020 20:14:41 coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
pcre => enabled
zlib => 1.2.11 mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled
Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608