views:

439

answers:

5

I was wondering if anybody knew of a method to configure apache to fall back to returning a static HTML page, should it (Apache) be able to determine that PHP has died? This would provide the developer with a elegant solution to displaying an error page and not (worst case scenario) the source code of the PHP page that should have been executed.

Thanks.

+3  A: 

I would assume that this typically results in a 500 error, and you can configure apaches 500 handler to show a static page:

ErrorDocument 500 /500error.html

You can also read about error handlers on apaches documentation site

Staale
+7  A: 

The PHP source code is only displayed when apache is not configured correctly to handle php files. That is, when a proper handler has not been defined.

On errors, what is shown can be configured on php.ini, mainly the display_errors variable. That should be set to off and log_errors to on on a production environment.

If php actually dies, apache will return the appropriate HTTP status code (usually 500) with the page defined by the ErrorDocument directive. If it didn't die, but got stuck in a loop, there is not much you can do as far as I know.

You can specify a different page for different error codes.

Vinko Vrsalovic
+1  A: 

The real problem is that PHP fatal errors don't cause Apache to return a 500 code. Errors except for E_FATAL and E_PARSE can be handled however you like using set_error_handler().

AdamTheHutt
A: 

There are 2 ways to user PHP and 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, the apache process fails. 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. I the PHP execution fails, apache will know that and there might be a way to handle the error. regardless of the way you install PHP, when PHP execution fails, you can handle erros in the php.ini file.

farzad
A: 

AdamTheHutt is correct, at least this is what I have observed with Debian, Apache2 and mod_php. While the PHP may return a 500 Server error, this is set from within PHP, not Apache. Hence Apache does not apply the ErrorDocument 500 option. ErrorDocument works when PHP is used with Apache via CGI. I have not found a suitable method for re-displaying these errors.

A possible solution is to use register_shutdown_function as listed at: How do I catch a php fatal error

Jim