一.安装
1.下载swoole,尽量使用stable版本,即稳定版
地址:https://github.com/swoole/swoole-src/releases
2.通过ftp上传到linux服务器
3.解压与安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//解压缩文件 unzip swoole-src-swoole-1.7.14-stable.zip //进入安装目录 cd swoole-src-swoole-1.7.14-stable //phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块 phpize //./configure是配置扩展模块的 //注:swoole的./configure有很多额外参数,可以通过./configure --help命令查看,这里仅开启其中async-mysql项,其他均选择默认项 //--with-php-config=php-config的路径 ./configure --with-php-config=/usr/local/php/bin/php-config --enable-async-mysql //编译 make //安装 make install |
安装完成后进入php.ini加入以下语句
extension=swoole.so
php.ini路径可通过 php –ini 查看
运行php -m 看看swoole扩展是否安装成功
重启nginx、php(不知道是否需要重启- – 保险起见还是重启一下)
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart
二.调试
在网站根目录编写index.php,内容如下:
1 2 3 4 5 6 |
<?php $http = new swoole_http_server("127.0.0.1", 9501); $http->on('request', function ($request, $response) { $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>"); }); $http->start(); |
通过 php index.php 启动服务
此时,通过浏览器访问127.0.0.1即可访问swoole的http服务器。