记录一下Mac系统搭建hyperf开发环境
1. 首先确认php版本要大于7.4
$ php -vPHP 7.4.21 (cli) (built: Sep 30 2021 13:37:02) ( NTS )Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies
如果php版本小于7.4,通过以下步骤切换php版本:
$ which php$ open ~
打开.bash_profile文件,修改 export MAMP_HOME=/Applications/MAMP/bin/php/php7.4.21/bin(换成自己的路径),保存。
$ source ~/.bash_profile
使系统环境变量立即生效,再查看php版本。
2. 查看是否已经安装swoole扩展
$ php --ri swooleExtension 'swoole' not present.
出现 Extension 'swoole' not present. 说明 swoole 扩展未安装
3. 安装swoole扩展
使用wget安装swoole:
$ wget https://github.com/swoole/swoole-src/archive/refs/tags/v4.6.6.tar.gz
如果出现 wget: command not found 说明未安装wget,则需要先安装wget:
$ brew install wget
如果发现 brew也未安装,则先安装brew:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
都安装好后重新执行wget,接下来,解压:
$ tar -xzvf v4.6.6.tar.gz
解压之后进入swoole目录:
$ cd swoole-src-4.6.6
然后编译:
$ phpize
查看openssl安装地址:
$ brew info openssl
之后设置配置打开http2、openssl,指定openssl地址(地址换成自己的):
$ ./configure --enable-http2 --enable-openssl --with-openssl-dir=/usr/local/etc/openssl@3
设置好后测试一下,看有没有报错:
$ make -j12
最后安装:
$ make install
安装完成后修改php.ini:
$ php -i | grep iniConfiguration File (php.ini) Path => /Applications/MAMP/bin/php/php7.4.21/confLoaded Configuration File => /Applications/MAMP/bin/php/php7.4.21/conf/php.ini$ cd /Applications/MAMP/bin/php/php7.4.21/conf$ vim php.ini
将下面两行添加到php.ini文件最后并保存:
extension="swoole.so"swoole.use_shortname="Off"
最后查看swoole扩展是否安装成功:
$php --ri swooleswooleSwoole => enabledAuthor => Swoole Team Version => 4.6.6Built => Sep 19 2022 10:43:35coroutine => enabled with boost asm contextkqueue => enabledrwlock => enabledopenssl => OpenSSL 1.1.1n 15 Mar 2022dtls => enabledhttp2 => enabledpcre => enabledzlib => 1.2.11brotli => E16777225/D16777225async_redis => enabledDirective => Local Value => Master Valueswoole.enable_coroutine => On => Onswoole.enable_library => On => Onswoole.enable_preemptive_scheduler => Off => Offswoole.display_errors => On => Onswoole.use_shortname => Off => Offswoole.unixsock_buffer_size => 262144 => 262144
如果出现上面的信息,则说明swoole安装成功,至此swoole扩展安装完成。
4. 最后就可以通过composer创建hyperf项目了:
$ composer create-project hyperf/hyperf-skeleton
安装过程中有询问是否安装各种扩展的一直按n不安装,安装完成后cd到 hyperf-skeleton目录,启动hyperf服务:
$ php bin/hyperf.php start[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#1 started.[INFO] Worker#2 started.
出现以上信息后访问 127.0.0.1:9501,返回:
{"method":"GET","message":"Hello Hyperf."}
说明hyperf服务启动成功了,开发环境搭建完成~
来源地址:https://blog.csdn.net/qq_35063487/article/details/126932492