hyperf 实战之安装
首先,我本地的环境是 Mac + PHP 7.4.21 + Swoole Version => 4.6.7 + Composer version 2.4.4,作为学习,建议大家统一选择官方的 docker 镜像。
我们先在本地创建一个目录,这个目录会直接映射到 hyperf 容器的 /data/project 目录。
~ » cd /data/work /data/work » mkdir questions_learn
执行下面的命令安装 hyperf 镜像,可能需要等待几分钟等待时间(如果本地没有这个镜像的话)。
docker run --name hyperf-php-8.0 \
-v /data/work/questions_learn:/data/project \
-e APP_ENV=dev \
-e SCAN_CACHEABLE=false \
-p 9501:9501 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:8.0-alpine-v3.15-swoole
在这一步中,大家记得把 -v 后面的 "/data/work/questions_learn" 改成你本地的目录,其他的保持不变。
上面的安装命令执行成功后,我们就已经在 hyperf 容器内了,来一起看下 hyperf 容器内的环境。
/ # php -v PHP 8.0.18 (cli) (built: Apr 22 2022 22:53:40) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.18, Copyright (c) Zend Technologies with Zend OPcache v8.0.18, Copyright (c), by Zend Technologies / # php --ri swoole swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 5.0.0 Built => Aug 2 2022 03:09:01 coroutine => enabled with boost asm context epoll => enabled eventfd => enabled signalfd => enabled spinlock => enabled rwlock => enabled openssl => OpenSSL 1.1.1q 5 Jul 2022 dtls => enabled http2 => enabled json => enabled curl-native => enabled pcre => enabled zlib => 1.2.12 brotli => E16777225/D16777225 mutex_timedlock => enabled pthread_barrier => enabled async_redis => enabled Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => Off => Off swoole.unixsock_buffer_size => 8388608 => 8388608 / # composer -V Composer version 2.2.1 2021-12-22 22:21:31 / # composer config -gl [repositories.packagist.org.type] composer [repositories.packagist.org.url] https://repo.packagist.org ......(省略了其他代码)
可以看到,官方镜像默认支持了很多扩展,如果你选择自己本地的环境,请至少确保你的运行环境达到了以下要求:
- PHP >= 8.0 and <= 8.1
- Swoole PHP 扩展 >= 4.5,并关闭了 Short Name
- JSON PHP 扩展
- Pcntl PHP 扩展
- enabled swoole coroutine、openssl、curl-native、async_redis、rwlock、pcre、zlib、brotl
另外,由于 Hyperf 基于 Swoole 协程实现,而 Swoole 4 带来的协程功能是 PHP 前所未有的,所以与不少扩展都仍存在兼容性的问题。
以下扩展(包括但不限于)都会造成一定的兼容性问题,不能与之共用或共存:
- xhprof
- xdebug
- blackfire
- trace
- uopz
这些我们基本也用不上,推荐大家按照文中的操作,选择官方镜像。
下面我们继续在容器内操作。
创建一个项目,并命名 questions
# cd /data/project/ /data/project # composer create-project hyperf/hyperf-skeleton questions
执行 composer create-project 命令的时候,开始会一直提示我们安装一大堆东西,直接全部回车,后面需要我们再手动进行安装。
安装好之后,安装的 hyperf 是 3.0 版本,通过 composer info 命令可以查看已安装的 hyperf 相关的版本信息。
/data/project/questions # composer info | grep hyperf hyperf/cache v3.0.0 A cache component for hyperf. hyperf/command v3.0.0 Command for hyperf hyperf/config v3.0.0 An independent component that provides configuration container. hyperf/context v3.0.0 A coroutine context library. hyperf/contract v3.0.0 The contracts of Hyperf. hyperf/coordinator v3.0.0 Hyperf Coordinator hyperf/database v3.0.0 A flexible database library. hyperf/db-connection v3.0.0 A hyperf db connection handler for hyperf/database. hyperf/devtool v3.0.0 A Devtool for Hyperf. hyperf/di v3.0.0 A DI for Hyperf. hyperf/dispatcher v3.0.0 A HTTP Server for Hyperf. hyperf/engine v2.1.1 hyperf/engine-contract v1.2.0 Contract for Coroutine Engine hyperf/event v3.0.0 an event manager that implements PSR-14. hyperf/exception-handler v3.0.0 Exception handler for hyperf hyperf/framework v3.0.0 A coroutine framework that focuses on hyperspeed and flexible, specifically use for build microservices and middlewares. hyperf/guzzle v3.0.0 Swoole coroutine handler for guzzle hyperf/http-message v3.0.0 microservice framework base on swoole hyperf/http-server v3.0.0 A HTTP Server for Hyperf. hyperf/logger v3.0.0 A logger component for hyperf. hyperf/macroable v3.0.0 Hyperf Macroable package which come from illuminate/macroable hyperf/memory v3.0.0 An independent component that use to operate and manage memory. hyperf/model-listener v3.0.0 A model listener for Hyperf. hyperf/pool v3.0.0 An independent universal connection pool component. hyperf/process v3.0.0 A process component for hyperf. hyperf/redis v3.0.0 A redis component for hyperf. hyperf/server v3.0.0 A base server library for Hyperf. hyperf/testing v3.0.0 Testing for hyperf hyperf/utils v3.0.0 A tools package that could help developer solved the problem quickly.
启动项目测试下是否安装成功
/data/project/questions # php bin/hyperf.php start [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener. [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener. [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener. [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener. [DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener. [DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [INFO] Worker#3 started. [INFO] Worker#2 started. [DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener. [INFO] Worker#0 started. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [INFO] HTTP Server listening at 0.0.0.0:9501 [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener. [INFO] Worker#1 started. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener. [INFO] Worker#4 started. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
这里注意,更改过框架内的任何代码,都需要手动执行 php bin/hyperf.php start 命令,已经启动的服务可以在终端按住 Ctrl + C 键停止服务。
本地我们先按照这个模式走,后期上线会全自动操作,这点无需担心。
窗口卡着不动即表示安装成功。
以上,我们都是在容器内执行的命令操作。
回到宿主机内,对本地的 9501 端口请求测试。
/data/work » curl 127.0.0.1:9501 {"method":"GET","message":"Hello Hyperf."}%
至此,hyperf 框架已经安装完毕。
教程中演示的所有代码,都会上传在 https://github.com/bailangzhan/questions_learn。
安装上如果有任何问题,可以在文章下面或者在 http://funqas.com/ 上提问留言。
- 评论区