如何使用 Nginx + supervisord 部署 Hyperf 项目

in PHP with 0 comment
前情提要
这里我们使用宝塔面板安装,我们需要从软件商店中准备如下工具

拉取代码

我们这里用的是 coding
cd /www/wwwroot
git clone git@e.coding.net:dotdotbear/kwh/project.git
cd project

# composer 配置 aliyun
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
# composer 安装 vendor 包
composer install -vvv
# 出现异常 ? 或
composer install -vvv --ignore-platform-reqs

# 或许这个地方,会出现类似的问题: putenv() has been disabled for security reasons
# 进入php管理界面,打开禁用函数 删除对应函数即可

# 配置 Env 配置
cp .env.example .env

Supervisord 安装与配置

基本上,在宝塔中已经提供了基础的 Supervisord 服务。可以直接使用它

常用命令

启动配置

[program:hyperf]
command=/www/server/php/80/bin/php bin/hyperf.php start
directory=/www/wwwroot/hyperf-project/
autorestart=true
startsecs=3
startretries=3
stdout_logfile=/www/server/panel/plugin/supervisor/log/hyperf.out.log
stderr_logfile=/www/server/panel/plugin/supervisor/log/hyperf.err.log
stdout_logfile_maxbytes=2MB
stderr_logfile_maxbytes=2MB
user=root
priority=999
numprocs=1
process_name=%(program_name)s_%(process_num)02d

Hyperf 常用函数

获取 Driver 队列

/**
 * 获取一个队列实例
 * DateTime: 2021/11/30 9:34 上午
 */
public static function getDriver($driver = 'default')
{
    return ApplicationContext::getContainer()->get(DriverFactory::class)->get($driver);
}

获取 Logger 实例

/**
 * 获取 Logger 实例
 * DateTime: 2021/12/3 9:47 上午
 * @param string $name
 * @return LoggerInterface
 */
public static function get(string $name = 'default'): LoggerInterface
{
    return ApplicationContext::getContainer()->get(LoggerFactory::class)->get($name, $name);
}

获取 Redis 实例

public static function get($app = 'default')
{
    $container = ApplicationContext::getContainer();
    $redis = $container->get(RedisFactory::class)->get($app);
    return $redis;
}

其他问题

如何修改宝塔默认 PHP 版本

ln -sf /www/server/php/74/bin/php /usr/bin/php

putenv() has been disabled for security reasons

进入 php 管理界面,打开禁用函数, 删除即可

或者进入对应 php 版本,查看 disable_functions

版本问题 ?

安装 PHP8 且 安装 > Swoole4

可以使用自定义 php 版本执行

/www/server/php/74/bin/php bin/hyperf.php start
/www/server/php/80/bin/php bin/hyperf.php start

composer 问题 ?

# 显示下载情况
composer install -vvv

# 忽略版本问题
omposer install -vvv --ignore-platform-reqs

# 重新加载 composer 映射文件
composer dump

swoole 问题 ?

参考文章

Comments are closed.