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 ...
随机推荐
- 记录在Centos下安装和使用Git的过程,从github上克隆仓库和提交。
1 安装git yum install git 2配置DNS vi /etc/resolv.conf nameserver 8.8.8.8nameserver 8.8.4.4 3 设置网关 vi /e ...
- (转)JPA + SpringData
jpa + spring data 约定优于配置 convention over configuration http://www.cnblogs.com/crawl/p/7703679.html 原 ...
- thymeleaf 的标准表达式
- spring boot 程序打jar包及运行
- 根据获取的窗口句柄遍历窗口Edit控件
网上说遍历窗口控件有两种方法: 1),使用EnumChildWindows,没有深究, 学习网址如下:http://blog.sina.com.cn/s/blog_60ac1c4b010116 ...
- 线上问题!----------org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe
1.问题出现 昨晚项目在上线的时候因为推广的原因,新增的大量请求.在八点的时候. org.apache.catalina.connector.ClientAbortException: java.io ...
- linux mysqlERROR 1045 (28000): linux忘记数据库密码
已验证没问题 #1.停止mysql数据库(确定能停止掉,不然第二部有问题) /etc/init.d/mysqld stop #2.执行如下命令 mysqld_safe --user=mysql - ...
- Android Studio: Application Installation Failed
[Android Studio: Application Installation Failed] 参考:http://stackoverflow.com/questions/32718044/and ...
- Photoshop Keynote
[Photoshop Keynote] 1.Tab:隐藏.显示所有面板. 2.Sihft+Tab:隐藏.显示右侧面板. 3.F:全屏切换. 4.选择并遮住: 参考:http://www.51shipi ...
- git库上传
1.第一步,拉下项目 2.添加 3.提交到本地和仓库