佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法分析

centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法分析

熱門標(biāo)簽:Linux服務(wù)器 服務(wù)器配置 科大訊飛語音識(shí)別系統(tǒng) 銀行業(yè)務(wù) 阿里云 團(tuán)購網(wǎng)站 Mysql連接數(shù)設(shè)置 電子圍欄

本文實(shí)例講述了centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法。分享給大家供大家參考,具體如下:

一、下載swoole源碼包

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

如:swoole-src-1.9.6.tar.gz

二、編譯安裝

> 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

以下路徑請根據(jù)自的環(huán)境修改

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

查看擴(kuò)展是否裝上

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

三、HttpServer的使用

http.php代碼如下:

?php
$http = new swoole_http_server('0.0.0.0', 8888);
//設(shè)置回調(diào)函數(shù),當(dāng)收到請求時(shí),會(huì)回調(diào)此函數(shù)
$http->on('request', function($request, $response) {
  //$request包含了客戶端請求的信息
  var_dump($request);
  //$response服務(wù)端響應(yīng)信息
  var_dump($response);
  //向客戶端發(fā)送404狀態(tài)碼
  $response->status(404);
  //向客戶端發(fā)送hello
  $response->end('hello');
});
//啟動(dòng)http服務(wù)
$http->start();

運(yùn)行該腳本

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

1、HttpServer如何處理靜態(tài)文件?

一般是分析客戶端發(fā)送的請求信息,如果是一個(gè)文件,那么讀取并發(fā)送給客戶端,如果不是則返回404。

?php
$http = new swoole_http_server('0.0.0.0', 8888);
//設(shè)置回調(diào)函數(shù),當(dāng)收到請求時(shí),會(huì)回調(diào)此函數(shù)
$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)) {
    //這里需要手動(dòng)設(shè)置文件MIME格式
    $response->header('Content-Type', $fileMime);
    $response->sendfile($file);
  } else {
    $response->status(404);
    $response->end('not found');
  }
});
//啟動(dòng)http服務(wù)
$http->start();

我們在http.php同目錄下放上一張1.jpg圖片,然后請求192.168.1.222:8888/1.jpg就可正常訪問。

2、HttpServer如何處理動(dòng)態(tài)php文件?

?php
$http = new swoole_http_server('0.0.0.0', 8888);
//設(shè)置回調(diào)函數(shù),當(dāng)收到請求時(shí),會(huì)回調(diào)此函數(shù)
$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');
  }
});
//啟動(dòng)http服務(wù)
$http->start();

我們在http.php同目錄下創(chuàng)建1.php腳本,然后請求192.168.1.222:8888/1.php就可正常訪問。

3、HttpServer的守護(hù)進(jìn)程化?

只需設(shè)置配置參數(shù)daemonize為1就可以了。

?php
$http = new swoole_http_server('0.0.0.0', 8888);
 
//設(shè)置進(jìn)程數(shù)量,和守護(hù)進(jìn)程化
$http->set(array(
  'worker_num' => 4,
  'daemonize' => 1,
));
 
//設(shè)置回調(diào)函數(shù),當(dāng)收到請求時(shí),會(huì)回調(diào)此函數(shù)
$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');
  }
});
//啟動(dòng)http服務(wù)
$http->start();

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php socket用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《php程序設(shè)計(jì)算法總結(jié)》

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • PHP擴(kuò)展Swoole實(shí)現(xiàn)實(shí)時(shí)異步任務(wù)隊(duì)列示例
  • PHP使用SWOOLE擴(kuò)展實(shí)現(xiàn)定時(shí)同步 MySQL 數(shù)據(jù)
  • linux下安裝openssl、swoole等擴(kuò)展的詳細(xì)步驟
  • linux平臺(tái)編譯安裝PHP7并安裝Redis擴(kuò)展與Swoole擴(kuò)展實(shí)例教程
  • PHP的swoole擴(kuò)展安裝方法詳細(xì)教程
  • php安裝swoole擴(kuò)展的方法
  • 使用swoole擴(kuò)展php websocket示例
  • CentOS7安裝PHP7 Redis擴(kuò)展的方法步驟
  • CentOS下安裝Memcached和PHP Memcached擴(kuò)展
  • CentOS環(huán)境下安裝Redis3.0及phpredis擴(kuò)展測試示例
  • Centos 6.5下PHP 5.3安裝ffmpeg擴(kuò)展的步驟詳解
  • Centos7安裝swoole擴(kuò)展操作示例

標(biāo)簽:江蘇 大理 萍鄉(xiāng) 蚌埠 廣元 衡水 衢州 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法分析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    陆川县| 华池县| 开原市| 天门市| 双辽市| 镇江市| 霍山县| 长岭县| 云林县| 五河县| 合川市| 苏尼特右旗| 临沂市| 尚义县| 宜昌市| 仪陇县| 灵丘县| 宁陵县| 洪雅县| 互助| 农安县| 钦州市| 三亚市| 渭源县| 绵阳市| 上犹县| 林州市| 嘉祥县| 芮城县| 塔城市| 宁津县| 杭锦旗| 云林县| 广元市| 曲周县| 武胜县| 铜陵市| 东源县| 泽普县| 新平| 赣榆县|