今天就跟大家聊聊有關怎么在centos7環境中安裝swoole1.9與HttpServer,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
一、下載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:http://vcdvsql.cn/article42/ceohhc.html
成都網站建設公司_創新互聯,為您提供自適應網站、網站收錄、營銷型網站建設、網站內鏈、服務器托管、網站營銷
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯