当 PHP 失败时 Apache 会回退
-
09-06-2019 - |
题
我想知道是否有人知道配置 apache 回退到返回静态 HTML 页面的方法,它(Apache)是否能够确定 PHP 已经死亡?这将为开发人员提供一个优雅的解决方案来显示错误页面,而不是(最坏的情况)应该执行的 PHP 页面的源代码。
谢谢。
解决方案
仅当 apache 未正确配置来处理 php 文件时,才会显示 PHP 源代码。也就是说,当尚未定义正确的处理程序时。
关于错误,显示的内容可以在php.ini中配置,主要是display_errors变量。在生产环境中,应将其设置为关闭,并将 log_errors 设置为打开。
如果 php 实际上死掉了,apache 将返回适当的 HTTP 状态代码(通常是 500)以及由 ErrorDocument 指令定义的页面。如果它没有死,而是陷入了循环,据我所知,你无能为力。
您可以为不同的错误代码指定不同的页面。
其他提示
真正的问题是 PHP 致命错误不会导致 Apache 返回 500 代码。错误除了 E_FATAL
和 E_PARSE
可以按照您喜欢的方式处理 set_error_handler()
.
有两种使用 PHP 和 Apache 的方法。
1. Install PHP as an Apache module: this way the PHP execution is a thread inside the apache process. So if PHP execution fails, then Apache process fails too. there is no fallback strategy.
2. Install PHP as a CGI script handler: this way Apache will start a new PHP process for each request. If the PHP execution fails, then Apache will know that, and there might be a way to handle the error.
无论您以何种方式安装 PHP,当 PHP 执行失败时,您都可以在 php.ini
文件。
不隶属于 StackOverflow