1、下载及解包
$ wget https://github.com/swoole/swoole-src/archive/v4.3.2.tar.gz
解压后执行
$ cd v4.3.2.tar.gz
$ phpize
(ubuntu 没有安装phpize可执行命令:sudo apt-get install php-dev来安装phpize)
$ ./configure --with-php-config=/www/server/php/71/bin/php-config
(/www/server/php/71/bin/php-config为你当前php的配置位置,记得是你的不要直接复制粘贴,这个大坑,官网也没写)
$ make
$ sudo make install
在php.ini中加
[swoole]
extension = swoole.so
# 如果找不到swoole.so的话你可以安装完后全盘搜,建议也是绝对路径
有些linux面板可以直接点安装,就更方便了
这里不卖广告,放个图给你们看看就算了
2、我用的是tp5的整合版
composer require topthink/think-swoole
# 不报错就证明搞定了
3、记得不能ping通的时候请关掉防火墙
sudo systemctl stop firewalld.service
#端口打开应用
netstat -lnp|grep 8000
#查看11100yingyong
ps 11100
#关闭11100应用pid
kill -9 11100
#查看防火墙状态
firewall-cmd --state
#停止firewall
systemctl stop firewalld.service
#禁止firewall开机启动
systemctl disable firewalld.service
#执行开机禁用防火墙自启命令
systemctl disable firewalld.service
#启动
systemctl start firewalld.service
#防火墙随系统开启启动
systemctl enable firewalld.service
4、使用
php think swoole:serve
# 也可以使用php think swoole:server
# 编程在 根目录/config/swoole.php 或 swoole_server.php
自定义的话在上两个文件里面加入
<?php
return [
'swoole_class'=>'\app\http\Swoole' # 自定义文件
];
# /application/http/Swoole.php
<?php
namespace app\http;
use think\swoole\Server;
class Swoole extends Server
{
protected $host = '127.0.0.1';
protected $port = 9502;
protected $option = [
'worker_num'=> 4,
'daemonize' => true,
'backlog' => 128
];
public function onReceive($server, $fd, $from_id, $data)
{
$server->send($fd, 'Swoole: '.$data);
}
}
参考
swoole官网教程 https://wiki.swoole.com/wiki/page/6.html