Phalcon4框架入门踩坑,502解决方案

问题描述

   Phalcon 官方的小例子入门Phalcon时,发现一个很奇怪的问题:涉及到 models 的操作均无法正常执行models下面定义了一个 Users 类,IndexController 中的 indexAction 使用了 Users::find() 方法,SignupController 中的registerAction创建了一个 Users 类型的对象。
  浏览器中访问 localhost/index与 lcoalhost/signup/register时都报错502 Bad Gateway并且signup中只要U大写,localhost/signup便不能正常访问。(Linux区分大小写所致)

环境:
  Ubuntu 20.04.2 LTS
  php 7.4.3 (通过apt安装,ubuntu官方源)
  nginx 1.18.0
  php 7.4-fpm及扩展若干 (包括php-pear)
  phalcon4 (通过添加apt source安装)
  phalcon4采用phalcon官方给出的Ubuntu安装方案


原因查找

查日志

NGINX: recv() failed (104: Connection reset by peer) while reading response header from upstream

PHP-FPM: WARNING: [pool www] child xxxx exited with code 1 after 0.xxxxx seconds from start

child异常退出导致连接重置,可见问题不在NGINX,而在PHP这边。

这时联想到在安装Phalcon时,最初使用pecl安装失败(php-pear版本1.10.9),频繁报错

Notice: Trying to access array offset on value of type bool in PEAR/REST.php on line 187
PHP Notice:  Trying to access array offset on value of type bool in /usr/share/php/PEAR/REST.php on line 187

此条报错不影响正常运行,安装新版pear即可解决,见 “解决方案” “2” 中的"# 安装新版pear"

重装Phalcon4试试吧


解决方案

重新安装Phalcon

1. 清除旧版本PHP

如果php是从PPA安装的,若要卸载可参考另一篇文章

apt purge php*
apt autopurge
apt clean
apt update
apt install php及其需要的php组件

2. 安装依赖

推 荐 操 作

sudo apt install php-psr

如果上述操作依然报错,可尝试如下:
此方案为手动编译。较为耗时

sudo apt install php7.4-xml php7.4-dev
######################################
# 安装新版pear
sudo apt purge php-pear php-psr
wget https://pear.php.net/go-pear.phar
sudo php go-pear.phar
sudo pecl channel-update pecl.php.net
######################################
sudo pecl uninstall PSR
sudo pecl install PSR

编译完成后,在配置文件中添加psr

vim /etc/php/7.4/mods-available/psr.ini

The PSR extension is required to be loaded before Phalcon. Please ensure that it is available in your system

添加如下内容

; priority=15
[psr]
extension = psr.so

3. 安装Phalcon4

参考:https://packagecloud.io/phalcon/stable-41/install

curl -s https://packagecloud.io/install/repositories/phalcon/stable-41/script.deb.sh | sudo bash
sudo apt install php7.4-phalcon
# 进行第四步即可
# 不推荐此种方式

也可采用pecl编译安装(推荐方式),注意选中版本
sudo pecl install http://pecl.php.net/get/phalcon-4.1.2.tgz
最后在配置文件/etc/php/7.4/mods-available/phalcon.ini中添加相应内容

; This file is part of the Phalcon.
; 
; (c) Phalcon Team <team@phalcon.io>
;
; For the full copyright and license information, please view
; the LICENSE.txt file that was distributed with this source code.
;
; If you did not receive a copy of the license it is available
; through the world-wide-web at the following url:
; https://license.phalcon.io

; configuration for php phalcon module
; priority=50
[phalcon]
extension = phalcon.so

; ----- Options to use the Phalcon Framework

; phalcon.db.escape_identifiers = On
; phalcon.db.force_casting = Off

; phalcon.orm.events = On
; phalcon.orm.virtual_foreign_keys = On
; phalcon.orm.column_renaming = On
; phalcon.orm.not_null_validations = On
; phalcon.orm.exception_on_failed_save = Off
; phalcon.orm.enable_literals = On
; phalcon.orm.late_state_binding = Off
; phalcon.orm.enable_implicit_joins = On
; phalcon.orm.cast_on_hydrate = Off
; phalcon.orm.ignore_unknown_columns = Off
; phalcon.orm.update_snapshot_on_save = On
; phalcon.orm.disable_assign_setters = Off
; phalcon.orm.resultset_prefetch_records = Off
; phalcon.orm.cast_last_insert_id_to_int = Off

4. 启用扩展

sudo phpenmod -v 7.4 -s ALL psr phalcon

5. 测试问题是否出现

经测试,请求正常响应
P A S S

总结反思

  多参考 官方教程 和 github issue 中的解决方案
  善用搜索引擎

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旨酒当歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值