swoole是异步、协程并行的通讯引擎,所以B/S架构网络引擎一定是会用到的。所以浏览器上访问自然少不了。
解决方案:总结两点:
1、关闭防火墙
2、安全组放行对应端口
源码如下
on("start", function ($server) { // 启动事件
echo "Swoole http server is started at http://8.134.49.245n";
});
// http 请求事件
$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello World,Swoole study n");
});
echo "start 前n";
// 启动swoole的服务
$http->start();
echo "start end";
操作过程
1、启动服务
[root@iZ7xv0j6qg6j84hey84c6tZ 01]# php test.php start 前 Swoole http server is started at http://192.168.169.100:95012、访问服务
本地ip正常响应;
curl或者浏览器访问ip:端口均没有响应
[root@iZ7xv0j6qg6j84hey84c6tZ ~]# curl http://127.0.0.1:9501 Hello World ,Swoole study [root@iZ7xv0j6qg6j84hey84c6tZ ~]# curl http://8.134.49.245:9501 ^C3、查看防火墙状态并关闭,也是无法响应
注:centos7.9下
[root@iZ7xv0j6qg6j84hey84c6tZ ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-06-10 23:10:02 CST; 15h ago
Docs: man:firewalld(1)
Main PID: 17049 (firewalld)
CGroup: /system.slice/firewalld.service
└─17049 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
Jun 10 23:10:02 iZ7xv0j6qg6j84hey84c6tZ systemd[1]: Starting firewalld - dynamic firewall daemon...
Jun 10 23:10:02 iZ7xv0j6qg6j84hey84c6tZ systemd[1]: Started firewalld - dynamic firewall daemon.
Jun 10 23:10:02 iZ7xv0j6qg6j84hey84c6tZ firewalld[17049]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration o...it now.
Jun 11 14:52:38 iZ7xv0j6qg6j84hey84c6tZ firewalld[17049]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration o...it now.
Hint: Some lines were ellipsized, use -l to show in full.
[root@iZ7xv0j6qg6j84hey84c6tZ ~]# systemctl stop firewalld.service
[root@iZ7xv0j6qg6j84hey84c6tZ ~]# curl http://8.134.49.245:9501
^C
[root@iZ7xv0j6qg6j84hey84c6tZ ~]# curl http://8.134.49.245:9501
^C
4、登录云服务 -> 安全组 -> 9501端口放行,正常响应了。
[root@iZ7xv0j6qg6j84hey84c6tZ ~]# curl http://8.134.49.245:9501 Hello World ,Swoole study
重要提醒下: 1 、访问swoole httpserve,一定是 ip:端口访问,而不是访问访问文件路径。对于新手特别注意 2、进程查看:ps -aux 文件名.php;而不是 ps -aux swoole 3、查看端口对应进程id :netstat -anp | grep 9501



