今天就跟大家聊聊有關(guān)怎么在centos7環(huán)境中安裝swoole1.9與HttpServer,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為望城企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),望城網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。一、下載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下,請(qǐng)自行修改
> /data/php56/bin/phpize > ./configure > make && make install
修改php.ini文件添加如下兩行
> vi /data/php56/lib/php.ini
以下路徑請(qǐng)根據(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)收到請(qǐng)求時(shí),會(huì)回調(diào)此函數(shù) $http->on('request', function($request, $response) { //$request包含了客戶端請(qǐng)求的信息 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ā)送的請(qǐng)求信息,如果是一個(gè)文件,那么讀取并發(fā)送給客戶端,如果不是則返回404。
<?php $http = new swoole_http_server('0.0.0.0', 8888); //設(shè)置回調(diào)函數(shù),當(dāng)收到請(qǐ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();
我們?cè)趆ttp.php同目錄下放上一張1.jpg圖片,然后請(qǐng)求192.168.1.222:8888/1.jpg就可正常訪問(wèn)。
2、HttpServer如何處理動(dòng)態(tài)php文件?
<?php $http = new swoole_http_server('0.0.0.0', 8888); //設(shè)置回調(diào)函數(shù),當(dāng)收到請(qǐ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();
我們?cè)趆ttp.php同目錄下創(chuàng)建1.php腳本,然后請(qǐng)求192.168.1.222:8888/1.php就可正常訪問(wèn)。
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)收到請(qǐ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();
看完上述內(nèi)容,你們對(duì)怎么在centos7環(huán)境中安裝swoole1.9與HttpServer有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享文章:怎么在centos7環(huán)境中安裝swoole1.9與HttpServer-創(chuàng)新互聯(lián)
分享URL:http://bm7419.com/article42/ceohhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站收錄、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、服務(wù)器托管、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容