怎么在centos7环境中安装swoole1.9与HttpServer-创新互联

今天就跟大家聊聊有关怎么在centos7环境中安装swoole1.9与HttpServer,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都创新互联公司长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为望城企业提供专业的成都网站建设、网站设计望城网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

一、下载swoole源码包


二、编译安装


> 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下,请自行修改

> /data/php56/bin/phpize
> ./configure
> make && make install

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

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

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

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

查看扩展是否装上

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

三、HttpServer的使用

http.php代码如下:

<?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();

运行该脚本

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

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


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

<?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文件?

<?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就可以了。

<?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();

看完上述内容,你们对怎么在centos7环境中安装swoole1.9与HttpServer有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。

分享文章:怎么在centos7环境中安装swoole1.9与HttpServer-创新互联
分享URL:https://www.cdcxhl.com/article42/ceohhc.html

成都网站建设公司_创新互联,为您提供自适应网站网站收录营销型网站建设网站内链服务器托管网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计