[Swoole] 在Ubuntu下安装、快速开始
本文主要讲述在 Ubuntu 下编译安装 Swoole,并根据官方文档给出的demo进行了测试和搬运,包括:TCP服务器、UDP服务器、HTTP服务器、WebSocket服务器、异步客户端、定时器和协程相关,通过模仿官方例子领略Swoole给PHPer带来全新的编程模式和思想。
它弥补PHP在网络编程的不足。
一、说明
运行环境:win10 下的 Ubuntu、PHP7.2、Swoole4.3
参考文档: https://wiki.swoole.com/wiki/page/p-quickstart.html
二、安装Swoole
下载解压
sudo wget https://github.com/swoole/swoole-src/archive/v4.3.6.tar.gz
cp v4.3.6.tar.gz swoole-v4.3.6.tar.gz
tar -zxvf swoole-v4.3.6.tar.gz
cd swoole-v4.3.6
安装依赖
# 根据下面的编译提示进行选择安装
sudo apt-get install php-dev
sudo apt-get install autoconf
编译安装
# 根据自己 php 安装的目录
cd swoole-v4.3.6
/usr/local/php/bin/phpize
./configure
make
sudo make install
make 的结果:
...
----------------------------------------------------------------------
Libraries have been installed in:
/home/fly/swoole-src-4.3.6/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
sudo make install 的结果:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/
Installing header files: /usr/local/php/include/php/
配置 php.ini
# 编译安装成功后,在 php.ini 文件加入
extension=swoole.so
# extension=/usr/lib/php/20170718/swoole.so
查看是否加载 swoole 扩展
php -m
错误排查(可忽略)
下面错误很明显,需要指定 php-conf 路径:
configure: error: Cannot find php-config. Please use --with-php-config=PATH
解决方法:
./configure --with-php-config=/usr/local/php/bin/php-config
三、快速入门
a) TCP 服务器
# 创建 php 文件
vi tcp_server.php
on('Connect', function ($serv, $fd) {
echo "Client: Connect.\n";
});
//监听数据接收事件
$serv->on('Receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, "Server: ".$data);
});
//监听连接关闭事件
$serv->on('Close', function ($serv, $fd) {
echo "Client: Close.\n";
});
//启动服务器
$serv->start();
# 程序运行测试
# 运行 server
php tcp_server.php
# 使用telnet 测试(退出telnet:Ctrl+] 回车,输入quit 回车)
telnet 127.0.0.1 9501
b) UDP 服务器
vi udp_server.php
on('Packet', function ($serv, $data, $clientInfo) {
$serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
var_dump($clientInfo);
});
//启动服务器
$serv->start();
# 运行
php udp_server.php
# 使用 netcat 连接
netcat -u 127.0.0.1 9502
c) HTTP 服务器
vi http_server.php
on('request', function ($request, $response) {
if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
return $response->end();
}
var_dump($request->get, $request->post);
$response->header("Content-Type", "text/html; charset=utf-8");
$response->end("