Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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().

There are 2 ways to use 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, 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.

regardless of the way you install PHP, when PHP execution fails you can handle errors in the php.ini file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top