🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[danger] 该服务依赖 Swoole 扩展,Windows下无法执行。 ## `mix-httpd` 服务器 官方开发的 HTTP 服务器,用于执行 "HTTP 服务",基于 Swoole 扩展的 `swoole_http_server`,拥有比 Apache/PHP-FPM 更高的性能。 >[info] mix-httpd 其实就是一个使用 MixPHP 开发的一个控制台程序。 ## 启动 启动服务器: ~~~ php mix-httpd start -c ../applications/http/config/httpd.php ~~~ 以上命令的各部分拆解如下: - `mix-httpd` 为入口文件。 - `start` 为命令。 - `-c ../applications/http/config/httpd.php` 服务器使用的配置文件,必填 ## 配置文件 每个服务器都有一个单独的配置文件: [>> 到 GitHub 查看配置文件 <<](https://github.com/mix-php/mix/blob/v2/applications/http/config/httpd.php) ## 命令管理 服务器全部命令如下: - `start `: 启动服务器。 - `stop` : 停止服务器。 - `restart` : 重启服务器。 - `reload` : 重启所有工作进程,用于刷新代码。 - `status` : 查看服务器状态。 `start` 命令的参数: - -d : 后台运行 为了方便错误调试,不建议在开发阶段使用该参数,因为终端会打印执行错误信息,避免频繁到日志文件查看错误的烦恼。 - -u : 代码热更新 开发阶段使用,需关闭 PHP 的 OPcache,该参数会使 Worker 进程只处理一次请求就销毁,所以:**1. 不要在生产环境中使用 2. 会严重影响 ab 测试结果** ## 运行模式 服务器可选择以下模式: >[info] 理论上只要关闭服务器的 enable_coroutine 配置,不使用协程或异步客户端,就是常驻同步模式。 - 常驻同步模式 (默认): > 常驻内存带来传统框架无法比拟的高性能,同时对团队技术要求不会太高,对 Composer 和其他三方库兼容性高。 修改 `applications/http/config/httpd.php` 文件的以下配置项开启该模式: ``` // 配置文件 'config_file' => __DIR__ . '/main_permanent.php', // 开启协程 'enable_coroutine' => false, ``` - 常驻协程模式: > 除了具有常驻内存的优势,协程带来的并行优势让总体并发性能提升N倍,适合技术能力较强的团队使用。 修改 `applications/http/config/httpd.php` 文件的以下配置项开启该模式: ``` // 配置文件 'config_file' => __DIR__ . '/main_coroutine.php', // 开启协程 'enable_coroutine' => true, ``` ## 事件钩子 >[info] mix-http-server >= 2.0.2 支持 通过事件钩子能在服务器的事件中执行一些代码,比如:在 Worker 进程启动前的 hook_worker_start 中清理 opcache 缓存,功能与 [Swoole 事件回调函数](https://wiki.swoole.com/wiki/page/41.html) 一致。 ``` applications/http/config/httpd.php ``` ~~~ // 运行参数:https://wiki.swoole.com/wiki/page/274.html 'setting' => [ // 主进程启动事件回调 'hook_start' => function (\Swoole\Http\Server $server) { }, // 主进程停止事件回调 'hook_shutdown' => function (\Swoole\Http\Server $server) { }, // 管理进程启动事件回调 'hook_manager_start' => function (\Swoole\Http\Server $server) { }, // 工作进程错误事件 'hook_worker_error' => function (\Swoole\Http\Server $server, int $workerId, int $workerPid, int $exitCode, int $signal) { }, // 管理进程停止事件回调 'hook_manager_stop' => function (\Swoole\Http\Server $server) { }, // 工作进程启动事件回调 'hook_worker_start' => function (\Swoole\Http\Server $server) { }, // 工作进程停止事件回调 'hook_worker_stop' => function (\Swoole\Http\Server $server) { }, // 工作进程退出事件回调 'hook_worker_exit' => function (\Swoole\Http\Server $server, int $workerId) { }, // 请求成功回调 'hook_request_success' => function (\Swoole\Http\Server $server, \Swoole\Http\Request $request) { }, // 请求错误回调 'hook_request_error' => function (\Swoole\Http\Server $server, \Swoole\Http\Request $request) { }, ], ~~~ ## 自动重启 (仅限开发阶段使用) 为了提升开发效率,我们提供了 [https://github.com/mix-php/mix-inotifycmd](https://github.com/mix-php/mix-inotifycmd) 工具,能监控文件系统变化,通过设置的命令自动重启服务器,可用于修改代码后自动重启各种 Swoole 常驻服务器 (仅限开发阶段使用)