wget -c https://github.com/swoole/swoole-src/archive/v2.0.6.tar.gz
解压: tar -zxvf v2.0.6.tar.gz
cd swoole-src-2.0.6
编译&安装
使用phpize来生成php编译配置
./configure 来做编译配置检测
使用SSL必须在编译swoole时加入--enable-openssl选项
如果之前曾经编译过,make clean 可以将之前产生的可执行档及其他档案删除
make进行编译,make install进行安装
[root@php7 swoole-src-2.0.6]# phpize
[root@php7 swoole-src-2.0.6]# ./configure --enable-openssl
[root@php7 swoole-src-2.0.6]# make clean
[root@php7 swoole-src-2.0.6]# make && make install
make install后,如果正确,会出现以下内容
[root@php7 swoole-src-2.0.6]# make install
Installing shared extensions: /usr/lib64/php/modules/
这表示,在 /usr/lib64/php/modules/ 目录中,成功生成了 swoole.so 文件
修改php配置文件
在php.ini 文件添加Swoole扩展
extension=swoole
重启服务
[root@php7 swoole-src-2.0.6]# systemctl restart nginx
[root@php7 swoole-src-2.0.6]# systemctl restart php-fpm
通过php -m或phpinfo()来查看是否成功加载了swoole

测试demo
创建一个名为swoole.php的文件,代码如下
<?php
$http = new Swoole\Http\Server('0.0.0.0', 9501);
$http->set([
'daemonize' => false, // 设置程序进入后台作为守护进程运行
'worker_num' => 8, // 启动的worker进程数
'max_request' => 10000 // 每个worker进程允许处理的最大任务数
]);
$http->on('Connect', function ($serve, $fd) {
echo "连接进程".$fd.PHP_EOL;
});
$http->on('Request', function ($request, $response) {
$uri = $request->server['request_uri'];
if ($uri == '/favicon.ico') {
$response->status(404);
$response->end();
}
$response->header('Content-Type', 'text/html; charset=utf-8');
$get = isset($request->get['hello']) ? $request->get['hello'] : '';
echo $get.PHP_EOL;
$response->end('<h1>Hello Swoole '.$get.'</h1>');
});
$http->on('Close', function ($serve, $fd) {
echo "关闭进程".$fd.PHP_EOL;
});
$http->start();
执行swoole启动命令
php swoole.php
到浏览器中访问 http://127.0.0.1:9501/?hello=100,即可获取服务器输出响应内容
转载请注明出处。