参考于Git地址:https://github.com/LinkedDestiny/swoole-doc
第一章:环境搭建及扩展安装
安装基础环境:
PHP下载地址:http://php.net/
Ubuntu环境下:
```bash
sudo apt-get install build-essential gcc g++ autoconf libiconv-hook-dev libmcrypt-dev libxml2-dev libmysqlclient-dev libcurl4-openssl-dev libjpeg8-dev libpng12-dev libfreetype6-dev
```
CentOS环境下:
```bash
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers gd gd2 gd-devel gd2-devel perl-CPAN pcre-devel
```
安装PHP
```bash
cd php-5.5.10/
./configure --prefix=/usr/local/php --with-config-file-path=/etc/php --enable-fpm --enable-pcntl --enable-mysqlnd --enable-opcache --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-shmop --enable-zip --enable-ftp --enable-soap --enable-xml --enable-mbstring --disable-rpath --disable-debug --disable-fileinfo --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pcre-regex --with-iconv --with-zlib --with-mcrypt --with-gd --with-openssl --with-mhash --with-xmlrpc --with-curl --with-imap-ssl
sudo make
sudo make install
sudo cp php.ini-development /etc/php/
```
至此,PHP已经成功安装,但是此时在终端里是无法直接通过php --version查看php版本的还需要将PHP的可执行目录添加到环境变量中。
使用Vim/Sublime打开~/.bashrc,或是vi ~/.bash_profile
在末尾添加如下内容:
```shell
export PATH=/usr/local/php/bin:$PATH
export PATH=/usr/local/php/sbin:$PATH
```
保存后,终端输入命令:
```bash
source ~
source ~/.bash_profile
(注:swoole的./configure有很多额外参数,可以通过./configure --help命令查看,这里仅开启其中async-mysql项,其他均选择默认项)
安装完成后,进入/etc/php目录下,打开php.ini文件,在其中加上如下一句:
```shell
extension=swoole.so
```
随后在终端中输入命令
```bash
php -m
```
//以上为附加的安装说明,按慕课网课程是另一种体验~~
安装gcc 和 g++
yum install gcc (make命令不被识别)
yum install gcc-c++
安装perl(因为安装autoconf需要)
参考:https://blog.csdn.net/zhang6622056/article/details/52594242
wget http://www.cpan.org/src/5.0/perl-5.16.1.tar.gz
tar -zxvf perl-5.16.1.tar.gz
./Configure -des -Dprefix=/usr/local/perl
make
make test
make install
安装m4,autoconf,automake,libtool
参考:https://blog.csdn.net/qq_30549833/article/details/72955881
解压.tar.bz2
需要安装bzip2
yum -y install bzip2
安装php源码之前需要安装libxml2和libxml2-devel
直接yum install [xx] 就行
(PHP安装参考上面)
源码安装完PHP后,需要修改全局变量
vi ~/.bash_profile
添加
alias php=/home/work/study/soft/php/bin/php
修改完成后需要重新加载
source ~/.bash_profile
需要把安装包中的php.ini-development
复制到 php/lib
修改名字 mv php.ini-development php.ini
(查看配置文件的默认目录)
php –i | grep php.ini
安装Swoole
官网下载或是克隆下载
git clone [http://] https://gitee.com/swoole/swoole.git
swoole文件夹里没有configure需要用phpize生成
在swoole目录下执行
/home/work/study/soft/php/bin/phpize
1. ./configure --with-php-config=/home/work/study/soft/php/bin/php-config
2. make(编译)
3. make install
examples/sever/echo.php //为模板文件
安装swoole后需要启用服务
vi php.ini
extension=swoole
php –m //查看扩展
php echo.php //执行swoole示例文件,开启9501端口
netstat -anp | grep 9501 //可能需要安装net-tools
PS:查找命令在哪个压缩包内,yum search xx
执行swoole,Ctrl+C退出
小案例:
新建http_server
//http_server 包含server,0.0.0.0表示监听所有地址
$http = new swoole_http_server("0.0.0.0",8811);
$http->set(
[
'enable_static_handler' => true,
'document_root' => "/root/hdtocs/swoole/data",
]
);
$http->on('request', function($request, $response){
//print_r($request->get);
$content = [
'date:' => date("Y-m-d H:i:s").PHP_EOL,//date(format("Ymd H:i:s")),
'get:' => $request->get,
'post:' => $request->post,
'header:' => $request->header,
];
swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename){
//todo
}, FILE_APPEND);
$response->cookie("chuangxiang","niubi",time()+1800);
$response->end("sss".json_encode($request->get));
});
//end中为浏览器输出内容,必须为字符串
$http->start();
新建Server对象,类型为TCP
//创建Server对象,监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set([
'worker_num' => 8 ,//worker进程数 cpu 1-4
'max_request' => 10000, //处理完n次请求后结束运行
]);
//监听连接进入事件
/**
* $fd 客户端连接的唯一标示
* $reactor 线程ID
*/
$serv->on('connect', function ($serv, $fd, $reactor_id) {
echo "Client: {$reactor_id}-{$fd}--Connect.\n";
});
//监听数据接收事件
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
$serv->send($fd, "Server: {$reactor_id} - {$fd}".$data);
});
//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
//启动服务器
$serv->start();
对应的tcp_client
//连接 swoole tcp服务
$client = new swoole_client(SWOOLE_SOCK_TCP);
if(!$client->connect("127.0.0.1", 9501)){
echo "连接失败";
exit;
}
// php cli常量
fwrite(STDOUT, "Please enter information:");
$msg = trim(fgetc(STDOUT));
//发送信息给 tcp server服务器
$client->send($msg);
//接受来自server的数据
$result = $client->recv();
echo $result;
新建Server对象,类型为UDP
//创建Server对象,监听 127.0.0.1:9503端口,类型为SWOOLE_SOCK_UDP
$serv = new Swoole_Server("127.0.0.1", 9503, SWOOLE_BASE, SWOOLE_SOCK_UDP);
//监听数据发送事件
$serv->on('Packet', function ($serv, $data, $clientInfo) {
//发送给客户端 用sendto
$serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
var_dump($data);
});
//启动服务器
$serv->start();
新建对应的UDP客户端:
$client = new Swoole_Client(SWOOLE_SOCK_UDP);
$client->connect('127.0.0.1', 9503, 1);
$i = 0;
while ($i < 1000) {
$client->send($i."\n");
$message = $client->recv();
echo "Get Message From Server:{$message}\n";
$i++;
}