PHP失败时Apache回退
时间:2020-03-05 18:53:12 来源:igfitidea点击:
我想知道是否有人知道一种配置apache的方法以回退到返回静态HTML页面,它(Apache)是否应该能够确定PHP已经死了?这将为开发人员提供一种优雅的解决方案来显示错误页面,而不是(最坏的情况)显示应该已经执行的PHP页面的源代码。
谢谢。
解决方案
回答
我假设这通常会导致500错误,并且我们可以配置apache 500处理程序以显示静态页面:
ErrorDocument 500 /500error.html
我们还可以在apaches文档站点上阅读有关错误处理程序的信息。
回答
仅当未正确配置apache来处理php文件时,才会显示PHP源代码。也就是说,当尚未定义适当的处理程序时。
关于错误,可以在php.ini上配置显示的内容,主要是display_errors变量。在生产环境中,应将其设置为off,将log_errors设置为on。
如果php实际死亡,则apache将返回由ErrorDocument指令定义的页面的相应HTTP状态代码(通常为500)。如果它没有死,而是陷入循环中,那么据我所知,我们将无能为力。
我们可以为不同的错误代码指定不同的页面。
回答
真正的问题是,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文件中的错误。