服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - centos7环境下swoole1.9的安装与HttpServer的使用方法分析

centos7环境下swoole1.9的安装与HttpServer的使用方法分析

2021-10-06 13:34怀素真 PHP教程

这篇文章主要介绍了centos7环境下swoole1.9的安装与HttpServer的使用方法,结合实例形式分析了centos7环境下swoole1.9的安装、配置方法以及HttpServer的相关使用技巧,需要的朋友可以参考下

本文实例讲述了centos7环境下swoole1.9的安装与HttpServer的使用方法。分享给大家供大家参考,具体如下:

一、下载swoole源码包

https://github.com/swoole/swoole-src/releases

如:swoole-src-1.9.6.tar.gz

二、编译安装

?
1
2
3
> yum install gcc gcc-c++ kernel-devel make autoconf
> tar xf swoole-src-1.9.6.tar.gz
> cd swoole-src-1.9.6

我的php是安装在/data/php56下,请自行修改

?
1
2
3
> /data/php56/bin/phpize
> ./configure
> make && make install

修改php.ini文件添加如下两行

?
1
> vi /data/php56/lib/php.ini

以下路径请根据自的环境修改

?
1
2
extension_dir = "/data/php56/lib/php/extensions/no-debug-zts-20131226/"
extension=swoole.so

查看扩展是否装上

?
1
> /data/php56/bin/php -m|grep swoole

三、HttpServer的使用

http.php代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$http = new swoole_http_server('0.0.0.0', 8888);
//设置回调函数,当收到请求时,会回调此函数
$http->on('request', function($request, $response) {
  //$request包含了客户端请求的信息
  var_dump($request);
  //$response服务端响应信息
  var_dump($response);
  //向客户端发送404状态码
  $response->status(404);
  //向客户端发送hello
  $response->end('hello');
});
//启动http服务
$http->start();

运行该脚本

?
1
> /data/php56/bin/php http.php

1、HttpServer如何处理静态文件?

一般是分析客户端发送的请求信息,如果是一个文件,那么读取并发送给客户端,如果不是则返回404。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$http = new swoole_http_server('0.0.0.0', 8888);
//设置回调函数,当收到请求时,会回调此函数
$http->on('request', function($request, $response) {
  $pathInfo = $request->server['path_info'];
  $file = __DIR__ . $pathInfo;
  //获取文件的MIME
  $fileInfo = finfo_open(FILEINFO_MIME);
  $fileMime = finfo_file($fileInfo, $file);
 
  if(is_file($file)) {
    //这里需要手动设置文件MIME格式
    $response->header('Content-Type', $fileMime);
    $response->sendfile($file);
  } else {
    $response->status(404);
    $response->end('not found');
  }
});
//启动http服务
$http->start();

我们在http.php同目录下放上一张1.jpg图片,然后请求192.168.1.222:8888/1.jpg就可正常访问。

2、HttpServer如何处理动态php文件?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
$http = new swoole_http_server('0.0.0.0', 8888);
//设置回调函数,当收到请求时,会回调此函数
$http->on('request', function($request, $response) {
  $pathInfo = $request->server['path_info'];
  $file = __DIR__ . $pathInfo;
 
  if(is_file($file)) {
    //判断文件后缀名
    if(pathinfo($pathInfo)['extension'] == 'php') {
      ob_start();
      include $file;
      $content = ob_get_contents();
      ob_end_clean();
      $response->end($content);
    } else {
      //处理其他文件
    }
  } else {
    $response->status(404);
    $response->end('not found');
  }
});
//启动http服务
$http->start();

我们在http.php同目录下创建1.php脚本,然后请求192.168.1.222:8888/1.php就可正常访问。

3、HttpServer的守护进程化?

只需设置配置参数daemonize为1就可以了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
$http = new swoole_http_server('0.0.0.0', 8888);
 
//设置进程数量,和守护进程化
$http->set(array(
  'worker_num' => 4,
  'daemonize' => 1,
));
 
//设置回调函数,当收到请求时,会回调此函数
$http->on('request', function($request, $response) {
  $pathInfo = $request->server['path_info'];
  $file = __DIR__ . $pathInfo;
 
  if(is_file($file)) {
    //判断文件后缀名
    if(pathinfo($pathInfo)['extension'] == 'php') {
      ob_start();
      include $file;
      $content = ob_get_contents();
      ob_end_clean();
      $response->end($content);
    } else {
     
    }
  } else {
    $response->status(404);
    $response->end('not found');
  }
});
//启动http服务
$http->start();

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://www.cnblogs.com/jkko123/p/6509976.html

延伸 · 阅读

精彩推荐