centos7下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
以下路径请根据自的环境修改
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的使用的更多相关文章
- SVN CentOS7 下配置svn的安装及基础配置介绍
CentOS7 下配置svn的安装及基础配置介绍 by:授客 QQ:1033553122 目录 一. 二. 三. 四. 五. 六. 七. 一. 实践环境 CentOS 7操作系统(CentO ...
- centos7下源码方式安装gitlab8.9+发送邮件+ldap
CentOS7下源码方式安装gitlab 环境描述 操作系统: centos7 redis: >=2.8 mysql >=5.5.14 git >=2.7.4 架构设计 一台gitl ...
- centos7下mysql5.7的安装与配置
centos7下MySQL5.7的安装与配置 下载 下载地址 根据系统和版本选择红框中的四个RPM包下载即可,然后放到centos7系统中的/opt目录下,等待稍后安装. 安装前的准备 1. 检查系统 ...
- inux centos7下源码 tar安装5.7.26详解
inux centos7下源码 tar安装5.7.26图文详解 官网地址 https://dev.mysql.com/downloads/mysql/ 1.卸载Linux系统上自带的mysql插件(o ...
- CentOS7下MySQL5.7的安装-RPM方式
Installing MySQL on Linux Using RPM Packages 下载安装包 mysql下载地址:https://dev.mysql.com/downloads/mysql/ ...
- CentOS7下通过rpm方式安装MySQL及插入中文问题解决 [原创]
一 CentOS下通过rpm方式安装MySQL CentOS版本:CentOS-7 MySQL版本:MySQL-5.6.22 在网上搜了一下,Linux下安装MYSQL有三种方式: 1) 通过yum命 ...
- centos7下keepalived1.3.4安装与使用
keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. 一.下载keepalived http://www.keepalived.org/ 如 ...
- 【CentOS】在Centos7 下无图形界面安装 Oracle11g
目标 - 在虚拟机CentOS7中无图形界面安装Oracle11G R2版本 ① 系统要求以及准备 1. 物理内存不小于1G: 查看方式: # grep MemTotal /proc/meminfo ...
- centos7下redis和php-redis安装
centos7下redis安装和php-redis扩展安装 //一直yes就可以了 yum install redis //配置 whereis redis.conf vi /etc/redis.co ...
随机推荐
- linux mce的一些相关内容和用户态监控的设计方法
之所以想起写一点关于mce的东西,倒不是因为遇到mce的异常了,之前遇到过很多mce的异常,内存居多,但没有好好记录下来,写这个是因为参加2018 clk南京会议的一点想法. void __init ...
- mssqlservers数据嗅探
SQL Server - 最佳实践 - 参数嗅探问题 转. 文章来自:https://yq.aliyun.com/articles/61767 先说我的问题,最近某个存储过程,暂定名字:sp_a ...
- [福大2018高级软工教学]团队Beta阶段成绩汇总
一.作业地址: https://edu.cnblogs.com/campus/fzu/AdvancedSoftwareEngineerning2018/homework/2465 二.Beta阶段作业 ...
- 如何杀死oracle死锁进程
方法一:Oracle的死锁非常令人头疼,总结了一些点滴经验作为学习笔记 1.查哪个过程被锁查V$DB_OBJECT_CACHE视图: '; 2. 查是哪一个SID,通过SID可知道是哪个SESSION ...
- 删除node_modules文件夹
老版本的npm对有node_modules文件夹太长的问题,新版本就没有这个问题.2.7? npm install rimraf -g rimraf node_modules
- mysql explain预估剖析
http://www.cnblogs.com/LBSer/p/3333881.html 引子: 使用MySQL建立了一张表country,总共有才3121行记录. 但是使用explain select ...
- GIS工具-shp浏览器
GIS工具-shp浏览器 软件特点: 1. 单个文件,windows平台 2. 绿色,不用安装 3.C语言系列开发,非vb,.net,Java等,无需虚拟机,无需运行时,无需第三方工具 获取方法: 十 ...
- C# HttpWebRequest 模拟下载
C# web 获取服务端cookie 原文地址:https://www.cnblogs.com/louby/p/5569536.html C#多线程环境下调用 HttpWebRequest 并发连接限 ...
- Conscription-最小生成树-Kruskal
Windy has a country, and he wants to build an army to protect his country. He has picked up N girls ...
- node+mysql简单注册
1前台文件 <!doctype html> <html> <head> <meta charset="UTF-8" /> <t ...