1. 安装
cd /home
# swoole包下载地址:https://pecl.php.net/package/swoole
wget https://pecl.php.net/get/swoole-4.6.6.tgz
# 解压压缩包
tar -xzvf swoole-4.6.6.tgz
# 将解压出来的安装包copy到php容器
# 注意若容器中的路径不存在请先建立
docker cp swoole-4.6.6 php:/usr/src/php/ext/swoole
# 进入容器中并安装扩展
docker exec -it php bash
# 安装swoole
docker-php-ext-install swoole
# 查看是否安装成功
php -m
php --ri swoole
2. 使用
$http = new swoole\Http\Server('0.0.0.0', 9501);
$http->on('Request', function ($request, $response) {
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});
$http->start();