这仅是我在网上找了多个解决方法,搞定了我遇到的问题,做的一个记录,买这个服务器就是为了测试swoole,结果快到期了,swoole还没装好
PHP版本依赖
-
Swoole-1.x
需要PHP-5.3.10
或更高版本 -
Swoole-2.x
需要PHP-7.0.0
或更高版本 -
Swoole-4.x
需要PHP-7.1.0
或更高版本
安装httpd(就是apache)
1.安装
yum -y install httpd
2.开启apache服务
systemctl start httpd.service
3.设置apache服务开机启动
systemctl enable httpd.service
安装php
CentOS/RHEL 7.x:
1 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2 rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
CentOS/RHEL 6.x:
1 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
2 rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
yum安装php7.0:(拓展自选)
1 yum install php70w-common php70w-fpm php70w-opcache php70w-gd php70w-mysqlnd php70w-mbstring php70w-pecl-memcached php70w-devel
以上命令安装了下面的拓展!
php-api, php-bz2, php-calendar, php-ctype, php-curl, php-date, php-exif, php-fileinfo, php-filter, php-ftp, php-gettext, php-gmp, php-hash, php-iconv, php-json, php-libxml, php-openssl, php-pcre, php-pecl-Fileinfo, php-pecl-phar, php-pecl-zip, php-reflection, php-session, php-shmop, php-simplexml, php-sockets, php-spl, php-tokenizer, php-zend-abi, php-zip, php-zlib
安装完后,可以先写个index.php测试一下,我的网站目录是在根目录的var/www/html/下面写php或html即可
如果写的html可以浏览器访问,但是php直接把源码显示出来了,网上说是因为apache没有安装或关联php模块
使用这个命令yum install mod_php71w
这样显示源码的问题就ok了
安装swool
pecl install swoole
安装可选参数默认 NO
enable debug/trace log support?
enable sockets supports?
enable openssl support?
enable http2 support?
enable async-redis support?
enable mysqlnd support?
enable postgresql coroutine client support?
然后修改php.ini
增加
extension=swoole.so
然后重启httpd(systemctl restart httpd.service)
检查
php -m | grep swoole
如果有显示说明安装好了
安装mysql去网上查吧
至于测试swoole前往 https://wiki.swoole.com/wiki/page/p-quickstart.html
查看进程
netstat -antp #查看所有建立的TCP连接
netstat -lntp #查看监听(Listen)的端口
根据pid杀死进程
kill -9 pid数
大意就是新建一个测试用的php里面代码都是抄官方文档的
新建文件 server.php
<?php
$serv = new swoole_server('127.0.0.1', 9501);
//server的创建,只需要绑定要监听的ip和端口,如果ip指定为127.0.0.1,则表示客户端只能位于本机才能连接,其他计算机无法连接。
//端口这里指定为9501,可以通过netstat查看下该端口是否被占用。如果该端口被占用,可更改为其他端口,如9502,9503等。
$serv->set([ //我开2个worker进程处理我们的业务
'worker_num' => 2,
]);
// 有新的客户端连接时,worker进程内会触发该回调
$serv->on('Connect', function ($serv, $fd) {
echo "new client connected." . PHP_EOL;
});
//参数$serv是我们一开始创建的swoole_server对象,
//参数$fd是唯一标识,用于区分不同的客户端,同时该参数是1-1600万之间可以复用的整数。
// server接收到客户端的数据后,worker进程内触发该回调
$serv->on('Receive', function ($serv, $fd, $fromId, $data) {
// 收到数据后发送给客户端
$serv->send($fd, 'Server '. $data);
});
// 客户端断开连接或者server主动关闭连接时 worker进程内调用
$serv->on('Close', function ($serv, $fd) {
echo "Client close." . PHP_EOL;
});
// 启动server
$serv->start();
?>
然后进入到server.php 文件的目录
执行 php server.php
然后再开一个客户端
执行命令
telnet 127.0.0.1 9501
> hello world
> Swoole: hello world
可能telnet没有安装会报错
执行命令
yum install -y telnet
破罐子互摔