在Ubuntu系统中安装PHP协程框架:Hyperf3.1

安装准备

Hyperf3.1 运行环境要求如下:

  • PHP >= 8.1

  • 以下任一网络引擎

    • Swoole PHP 扩展 >= 5.0,并关闭了 Short Name
    • Swow PHP 扩展 >= 1.4
  • JSON PHP 扩展

  • Pcntl PHP 扩展(仅在 Swoole 引擎时)

  • OpenSSL PHP 扩展(如需要使用到 HTTPS)

  • PDO PHP 扩展 (如需要使用到 MySQL 客户端)

  • Redis PHP 扩展 (如需要使用到 Redis 客户端)

  • Protobuf PHP 扩展 (如需要使用到 gRPC 服务端或客户端)

安装php8.3、redis扩展

为了方便,我采用的是宝塔面板安装php8.3redis扩展,也可以自行编译安装。

安装swoole5.1.3扩展

在安装之前要先安装php8.3,这里要用到 /www/server/php/83/bin/phpize 来初始化 ./configure,如果没有 /www/server/php/83/bin/phpize 这个文件可以用 sudo apt-get install php-dev 这个命令来安装 phpize 。安装Swoole扩展有几种方式,我采用的是到官方代码仓库下载压缩包进行安装(如果用Docker或其他方式安装可以查阅官方文档)。解压目录后进入目录,运行如下命令:

root@prajna_sea:/www/server/swoole/swoole-v5.1.3# /www/server/php/83/bin/phpize
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
root@prajna_sea:/www/server/swoole/swoole-v5.1.3# ./configure --with-php-config=/www/server/php/83/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for cc... cc
...
root@prajna_sea:/www/server/swoole/swoole-v5.1.3# make
/bin/sh /www/server/swoole/swoole-v5.1.3/libtool --tag=CXX --mode=compile g++ -I. -I/www/server/swoole/swoole-v5.1.3 -I/www/server/swoole/swoole-v5.1.3/include -I/www/server/swoole/swoole-v5.1.3/main -I/www/server/swoole/swoole-v5.1.3 -I/www/server/php/83/include/php -I/www/server/php/83/include/php/main -I/www/server/php/83/include/php/TSRM -I/www/server/php/83/include/php/Zend -I/www/server/php/83/include/php/ext -I/www/server/php/83/include/php/ext/date/lib -I/thirdparty -I/www/server/swoole/swoole-v5.1.3 -I/www/server/swoole/swoole-v5.1.3/include -I/www/server/swoole/swoole-v5.1.3/ext-src -I/www/server/swoole/swoole-v5.1.3/thirdparty -I/www/server/swoole/swoole-v5.1.3/thirdparty/hiredis  -DHAVE_CONFIG_H  -g -O2 -Wall -Wno-unused-function -Wno-deprecated -Wno-deprecated-declarations -std=c++11    -DENABLE_PHP_SWOOLE -DZEND_COMPILE_DL_EXT=1 -c /www/server/swoole/swoole-v5.1.3/ext-src/php_swoole.cc -o ext-src/php_swoole.lo  -MMD -MF ext-src/php_swoole.dep -MT ext-src/php_swoole.lo
mkdir ext-src/.libs
...
Build complete.
Don't forget to run 'make test'.
root@prajna_sea:/www/server/swoole/swoole-v5.1.3# make install
Installing shared extensions:     /www/server/php/83/lib/php/extensions/no-debug-non-zts-20230831/
Installing header files:          /www/server/php/83/include/php/
  • ./configure --with-php-config=/www/server/php/83/bin/php-config 这个配置用于指定对应 php 版本的 php-config 位置(路径需要配置为绝对路径),更多 swoole 配置选项可以运行 ./configure --help 查看

修改对应版本 phpphp.ini 配置文件,在文件里增加以下内容:

extension=swoole.so
  • 如果是宝塔面板安装的 php8.3 修改后还需要重启对应php 服务

运行 php --ri swoole 命令查看 swoole 扩展是否安装成功:

root@Enthusiasm:~# php --ri swoole

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.1.3
Built => Sep  7 2024 10:51:52
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
http2 => enabled
json => enabled
pcre => enabled
zlib => 1.2.11
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

Hyperf3.1 需要关闭 swoole.use_shortname ,修改对应版本 phpphp.ini 配置文件,在文件里增加以下内容:

swoole.use_shortname=Off

安装hyperf

我采用的是 composer 进行安装,所以首先确保系统是否已安装 composer,如果是 Docker 安装,请查看官方文档。如果是宝塔面板安装的 php8.3 需要删除这些禁用函数:putenvproc_openpcntl_forkpcntl_wait

先将 composer 的镜像设置为阿里云的镜像,以免下载速度缓慢:

root@prajna_sea:~# composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

下载 hyperf/hyperf-skeleton 项目:

root@prajna_sea:/www/wwwroot# composer create-project hyperf/hyperf-skeleton
...
 What time zone do you want to setup ?
  [n] Default time zone for php.ini
Make your selection or type a time zone name, like Asia/Shanghai (n):
...
  • 安装步骤会有其他一些确认项,用不到的可以直接敲回车
  • 如果用到 redis 组件,需要先安装 redis 扩展否则会提示错误

启动hyperf
root@prajna_sea:/www/wwwroot/hyperf-skeleton# php ./bin/hyperf.php start
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
....
  • php ./bin/hyperf.php start 注意运行此命令时所在目录,还有 php 的版本是否是对应的 php8.3,否则启动不成功。

启动成功后可以访问 9501端口,如需外网访问,还要在安全组和防火墙放行 9501端口

root@prajna_sea:~# curl 127.0.0.1:9501
{"method":"GET","message":"Hello Hyperf."}

启动Hyper小技巧

在本地开发时,修改 php 文件内容后要经常重启服务(如果配置了热更新可以忽略此部分内容),这样每次得输入启动命令,要是服务没有正确关闭还要查看进程的PID,先 kill -9 PID 再启动,这样还是有点麻烦。所以可以自己写一个启动的 shell 脚本,然后配合 Vscode 的自定义任务实现快捷键启动服务,例如我配置启动服务快捷键是 alt + f ,只需要按快捷键就能启动服务。

/www/wwwroot/hyperf-skeleton/bin 目录下创建一个 start_server.sh 脚本文件,将以下内容复制到 start_server.sh 脚本文件中

#!/bin/bash

_echo() {
    local tag="$1"
    local color_code="\033[34m"
    local input_string="$2"
    local colored_string="${color_code}${tag}\033[0m${input_string}"

    # 返回带颜色的字符串
    echo -e "$colored_string"
}

hyperf_options=$1;
#获取skeletion:Master进程的PID
pid=$(pidof skeleton.Master)
SCRIPT_DIR=$(pwd -P -- "$(dirname "$0")")
_echo "[pwd] " $SCRIPT_DIR
if [[ -z "$hyperf_options" ]]; then
_echo "[use hyperf.php command options] " "start"
hyperf_options="start"
else
_echo "[use hyperf.php command options] " $hyperf_options
fi

if [[ -n "$pid" ]]; then
_echo "[running pid] " $pid
fi
#检查是否找到了PID
if [[ -n "$pid" ]]; then
_echo "[running command] " "kill -9 "$pid
kill -9 $pid
php hyperf.php $hyperf_options
else
php hyperf.php $hyperf_options
fi 

将以下内容添加到 Vscodetask.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "run hyperf.php start",
            "type": "shell",
            "command": "sh start_server.sh start",
            "options": {
                "cwd": "${workspaceFolder}/bin"
            }
        }
    ]
}

将以下内容添加到 Vscodekeybindings.json

[
    {
        "key": "alt+f",
        "command": "workbench.action.tasks.runTask",
        "args":"run hyperf.php start",
        "when": "editorTextFocus"
    }
]

使用快捷键 alt + f 启动服务 Vscode控制台 将输出以下信息:

 *  正在执行任务: sh start_server.sh start 

[pwd] /www/wwwroot/hyperf-skeleton/bin
[use hyperf.php command options] start
[running pid] 19034
[running command] kill -9 19034
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值