一、下载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的使用的更多相关文章

  1. SVN CentOS7 下配置svn的安装及基础配置介绍

    CentOS7 下配置svn的安装及基础配置介绍 by:授客 QQ:1033553122 目录 一. 二. 三. 四. 五. 六. 七. 一.      实践环境 CentOS 7操作系统(CentO ...

  2. centos7下源码方式安装gitlab8.9+发送邮件+ldap

    CentOS7下源码方式安装gitlab 环境描述 操作系统: centos7 redis: >=2.8 mysql >=5.5.14 git >=2.7.4 架构设计 一台gitl ...

  3. centos7下mysql5.7的安装与配置

    centos7下MySQL5.7的安装与配置 下载 下载地址 根据系统和版本选择红框中的四个RPM包下载即可,然后放到centos7系统中的/opt目录下,等待稍后安装. 安装前的准备 1. 检查系统 ...

  4. inux centos7下源码 tar安装5.7.26详解

    inux centos7下源码 tar安装5.7.26图文详解 官网地址 https://dev.mysql.com/downloads/mysql/ 1.卸载Linux系统上自带的mysql插件(o ...

  5. CentOS7下MySQL5.7的安装-RPM方式

    Installing MySQL on Linux Using RPM Packages 下载安装包 mysql下载地址:https://dev.mysql.com/downloads/mysql/ ...

  6. CentOS7下通过rpm方式安装MySQL及插入中文问题解决 [原创]

    一 CentOS下通过rpm方式安装MySQL CentOS版本:CentOS-7 MySQL版本:MySQL-5.6.22 在网上搜了一下,Linux下安装MYSQL有三种方式: 1) 通过yum命 ...

  7. centos7下keepalived1.3.4安装与使用

    keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. 一.下载keepalived http://www.keepalived.org/ 如 ...

  8. 【CentOS】在Centos7 下无图形界面安装 Oracle11g

    目标 - 在虚拟机CentOS7中无图形界面安装Oracle11G R2版本 ① 系统要求以及准备 1. 物理内存不小于1G: 查看方式: # grep MemTotal /proc/meminfo ...

  9. centos7下redis和php-redis安装

    centos7下redis安装和php-redis扩展安装 //一直yes就可以了 yum install redis //配置 whereis redis.conf vi /etc/redis.co ...

随机推荐

  1. numpy-Randow

    Randow使用 http://blog.csdn.net/pipisorry/article/details/39508417 概率相关使用 转:http://www.cnblogs.com/Nau ...

  2. VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  3. Java学习01 (第一遍)

    java se - 桌面 java ee - 网页 Jdk :Jre 区别Jdk-开发环境必要Jre-运行环境需要Jdk包含Jre,安装完Jdk就可以了 Javac Demo1.java-Javac ...

  4. Python自动化运维开发实战 一、初识Python

    导语 都忘记是什么时候知道python的了,我是搞linux运维的,早先只是知道搞运维必须会shell,要做一些运维自动化的工作,比如实现一些定时备份数据啊.批量执行某个操作啊.写写监控脚本什么的. ...

  5. Docker 批量启动

    批量配置IP for i in `docker ps -a|awk 'NR>1 {print $NF}'`;do IP=`echo $i|awk -F_ '{print "192.16 ...

  6. Elasticsearch搜索异常-------org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: parse_exception

    异常问题: Caused by: org.elasticsearch.index.query.QueryShardException: Failed to parse query [LOL: Uzi和 ...

  7. Java NIO Overview

    Java NIO Overview Channels and Buffers Selectors   Jakob JenkovLast update: 2014-06-23

  8. linux下WIFI的AP搜索、连接方法

    wpa_supplicant -Dwext -ieth1 -c/etc/wpa_supplicant.conf  &wpa_cli save_configwpa_cli reconfigure ...

  9. hadoop zookeeper高可用

    原文地址: https://blog.csdn.net/dingchenxixi/article/details/51131493 core-site.xml yarn-site.xml

  10. CentOS7下解决yum install mysql-server没有可用包

    # wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm --  http://repo.mysql.com/mysq ...