如何在centos7系统上安装swoole1.9并使用它搭建HttpServer?

2026-04-01 08:581阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1010个文字,预计阅读时间需要5分钟。

如何在centos7系统上安装swoole1.9并使用它搭建HttpServer?

本文简要介绍了在CentOS 7环境下安装Swoole 1.9及HttpServer的使用方法,供大家参考。

一、下载Swoole源码包

1.访问Swoole官方GitHub仓库:https://github.com/swoole/swoole-src/releases

2.下载Swoole 1.9版本源码包,例如:swoole-src-1.9.6.tar.gz

二、安装Swoole

1.解压源码包:tar -zxvf swoole-src-1.9.6.tar.gz

2.进入解压后的目录:cd swoole-src-1.9.6

3.编译安装:./configure --prefix=/usr/local/swoole && make && make install

三、配置PHP环境

1.确保PHP已安装,且版本大于5.5

2.修改PHP配置文件:vi /etc/php.ini

3.添加以下配置:

extension=swoole.so swoole.use_shortname=Off swoole.log_file=/var/log/swoole.log

四、测试Swoole

1.创建一个名为test.php的文件,内容如下:

on('request', function($request, $response) { $response->end(Hello, Swoole!); }); $swoole_server->start(); ?>

2.在终端运行:php test.php

3.在浏览器访问:http://localhost:9501,应显示Hello, Swoole!

至此,Swoole 1.9及HttpServer已成功安装并运行。

本文实例讲述了centos7环境下swoole1.9的安装与HttpServer的使用方法。分享给大家供大家参考,具体如下:

一、下载swoole源码包

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();

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《php socket用法总结》、《php面向对象程序设计入门教程》、《PHP数据结构与算法教程》及《php程序设计算法总结》

希望本文所述对大家PHP程序设计有所帮助。

如何在centos7系统上安装swoole1.9并使用它搭建HttpServer?

标签:安装

本文共计1010个文字,预计阅读时间需要5分钟。

如何在centos7系统上安装swoole1.9并使用它搭建HttpServer?

本文简要介绍了在CentOS 7环境下安装Swoole 1.9及HttpServer的使用方法,供大家参考。

一、下载Swoole源码包

1.访问Swoole官方GitHub仓库:https://github.com/swoole/swoole-src/releases

2.下载Swoole 1.9版本源码包,例如:swoole-src-1.9.6.tar.gz

二、安装Swoole

1.解压源码包:tar -zxvf swoole-src-1.9.6.tar.gz

2.进入解压后的目录:cd swoole-src-1.9.6

3.编译安装:./configure --prefix=/usr/local/swoole && make && make install

三、配置PHP环境

1.确保PHP已安装,且版本大于5.5

2.修改PHP配置文件:vi /etc/php.ini

3.添加以下配置:

extension=swoole.so swoole.use_shortname=Off swoole.log_file=/var/log/swoole.log

四、测试Swoole

1.创建一个名为test.php的文件,内容如下:

on('request', function($request, $response) { $response->end(Hello, Swoole!); }); $swoole_server->start(); ?>

2.在终端运行:php test.php

3.在浏览器访问:http://localhost:9501,应显示Hello, Swoole!

至此,Swoole 1.9及HttpServer已成功安装并运行。

本文实例讲述了centos7环境下swoole1.9的安装与HttpServer的使用方法。分享给大家供大家参考,具体如下:

一、下载swoole源码包

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();

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《php socket用法总结》、《php面向对象程序设计入门教程》、《PHP数据结构与算法教程》及《php程序设计算法总结》

希望本文所述对大家PHP程序设计有所帮助。

如何在centos7系统上安装swoole1.9并使用它搭建HttpServer?

标签:安装