Php – Apache not displaying custom 500 error page for php errors

500-errorapache-2.4PHP

Apache does not want to display a custom error document that I have setup.

In my vhost I have

    DocumentRoot /var/www/site/public
    ...
    ErrorDocument 500 /500.html

The 500.html is located in /var/www/site/public/500.html

To test this I have just created a broken php page that does return a 500 header but the page is blank. I suspect that this is a result of php generating the 500 page and not apache. So how do I get php to show my custom apache error document?

Best Answer

This is normal behavior, as PHP errors aren't considered 5xx Server Errors. According to RFC 7231

6.6. Server Error 5xx

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. - -

6.6.1. 500 Internal Server Error

The 500 (Internal Server Error) status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

PHP errors should always be fixed as they may pose you security problems. For the same reason best practise in handling php errors is to log them for the programmers to review and hide them from users. This kind of setup would be (in php.ini format):

error_reporting = E_ALL
log_errors = On
display_errors = Off

This will log errors in web server's error log or in other error log configured. Instead of reading them from the site you can perform tail -f on the log file during loading the page.