centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法分析
本文實例講述了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
以下路徑請根據自的環(huán)境修改
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); //向客戶端發(fā)送404狀態(tài)碼 $response->status(404); //向客戶端發(fā)送hello $response->end('hello'); }); //啟動http服務 $http->start();
運行該腳本
> /data/php56/bin/php http.php
1、HttpServer如何處理靜態(tài)文件?
一般是分析客戶端發(fā)送的請求信息,如果是一個文件,那么讀取并發(fā)送給客戶端,如果不是則返回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如何處理動態(tài)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同目錄下創(chuàng)建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();
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP網絡編程技巧總結》、《php socket用法總結》、《php面向對象程序設計入門教程》、《PHP數據結構與算法教程》及《php程序設計算法總結》
希望本文所述對大家PHP程序設計有所幫助。
- PHP擴展Swoole實現實時異步任務隊列示例
- PHP使用SWOOLE擴展實現定時同步 MySQL 數據
- linux下安裝openssl、swoole等擴展的詳細步驟
- linux平臺編譯安裝PHP7并安裝Redis擴展與Swoole擴展實例教程
- PHP的swoole擴展安裝方法詳細教程
- php安裝swoole擴展的方法
- 使用swoole擴展php websocket示例
- CentOS7安裝PHP7 Redis擴展的方法步驟
- CentOS下安裝Memcached和PHP Memcached擴展
- CentOS環(huán)境下安裝Redis3.0及phpredis擴展測試示例
- Centos 6.5下PHP 5.3安裝ffmpeg擴展的步驟詳解
- Centos7安裝swoole擴展操作示例
相關文章
使用git遷移Laravel項目至新開發(fā)環(huán)境的步驟詳解
這篇文章主要介紹了遷移Laravel項目至新開發(fā)環(huán)境的步驟詳解,需要的朋友可以參考下2020-04-04