5

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

 2 years ago
source link: https://surest.cn/archives/195/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Published on Dec 3, 2021 in PHP with 0 comment

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

  • Nginx
  • PHP8 + Swoole4
  • Supervisor 管理器 2.2
  • iterm2

我们这里用的是 coding

cd /www/wwwroot
git clone [email protected]: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 服务。可以直接使用它

  • supervisorctl status // [当前运行服务状态]
  • supervisorctl stop service_name // [停止服务]
  • supervisorctl start service_name // [启动服务]
  • supervisorctl restart service_name // [重启服务]
  • supervisorctl reload // [重载配置文件并重启]
  • supervisorctl reread // [读取当前运行配置和 program 配置文件的差异]
  • supervisorctl update // [重载配置文件并重启]
[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 问题 ?

  • php.ini 配置

    swoole.use_shortname = 'Off'

本文由 邓尘锋 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Dec 3, 2021 at 10:05 am


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK