3

As the Question title suggests, I want to setup a custom 500 error. In fact, I want to use a .php file for the error, and ideally send a 503 response (SE Reasons).

The problem is that the only way I know to generate a 500 response is via .htaccess, and it shows the server's built-in/default 500 error page. (I am assuming this, since the error occurs in .htaccess; it isn't processed and can't show the custom one.)

So my questions are: 1) Is it actually possible to generate a custom 500 page that will show up for users? 2) How can I test the response/error using non-.htaccess methods?

Yes, I have searched here on Server Fault. Yes, I tried to Google it as well. No, I haven't found anything on how to do a simple test. (Every time I found something causing a 500 response, it seemed to require me loading up lord-knows-what systems.)

Any help would be appreciated.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
Clueless
  • 33
  • 3

1 Answers1

5

Within your virtual host conf (or httpd.conf/apache2.conf) you can use the ErrorDocument directive, as follows:

ErrorDocument 500 /errordocs/500error.php

It's inadvisable to do too much in your 500 error page, as you could hit the same error condition that caused the 500 response in the first place!

Steve Mayne
  • 1,001
  • 6
  • 5
  • Thanks for the response Steve. I admit - I've been thinking much the same about problems repeating, such as if PHP goes down, then using PHP in the error file is a liability. So your suggestion would work fine for static - with little/no risk. The reason for wanting to use PHP in the error doc is to send a different response - a 503 is much more preferable than a 500 as far as Google goes. – Clueless Mar 16 '11 at 12:15
  • Further - does anyone have any ideas on how to "test" the 500 error? I've looked around the Net, and not seen a single method for "testing" such an error. :( – Clueless Mar 16 '11 at 12:16
  • @Clueless - "anyone have any ideas on how to "test" the 500 error?" `telnet localhost 80` + (Enter) + `HEAD /path/to/500/error/page HTTP/1.1` + (Enter) + `Host: yourhost.com` + (Enter) + (Enter) – danlefree Mar 16 '11 at 13:35
  • 1
    @Clueless if PHP blows up to the point where it can't handle running a simple "custom error page" script you have bigger problems than your error page -- That Should Never Happen kind of problems. :-) – voretaq7 Mar 16 '11 at 15:40
  • @voretaq7 - you could feasibly have a database failure which leads to the original 500 - and if your 500-handling php script attempts to read from the same database... well, you get the idea. – Steve Mayne Mar 16 '11 at 16:54
  • 1
    @Steve Mayne True, but there's no good reason I can think of for your 500 error page to read from a database (and if there's a good reason I didn't think of you can certainly check for that kind of failure and degrade gracefully -- you can also do so in your original script, obviating the need for the error page in the first place ;) – voretaq7 Mar 16 '11 at 17:02
  • Well - I'll be using a CMS - and that permits customised error handling (with correct/alterable Respons codes. But a 500 is Server based ... and I'm just not seeing any way for a normal user to alter it/customise it. It looks like the only way so far is as per ...Steve... and altering the Conf - which will require the Host to make the changes in this case - and no gaurantee they will comply :( I see tons of "custom 500" topics - but No One seems to verify it actually works, nor how they tested it (Good old Internet C&P :D). – Clueless Mar 16 '11 at 17:23
  • ...danlefree... if I understand the telnet suggestion - that is basically the equivelenet of my browsing to the custom 500 error page. If so - that isn'exactly what I'm after. I'm after a sure fire way (without using htaccess) to create a 500 error on a lamp server using something like php ... so I can see if my custom 500 page shows up, or if the default one occurs. – Clueless Mar 17 '11 at 16:00
  • @Clueless - just create a page with a PHP syntax error? – Steve Mayne Mar 17 '11 at 19:59
  • That won't work - it generates a php error - not a server one. – Clueless Mar 18 '11 at 08:42
  • Guess the only option is to alter the server setup for this scenario, then mess with htaccess to cause the fault - as it seems there isn't a way to generate server errors easily/sufficiently :( – Clueless Mar 18 '11 at 08:43
  • Isn't a php error a 500 error if you have error reporting turned off? What's your error reporting set to in your php.ini file? – Steve Mayne Mar 18 '11 at 09:07
  • I've turned it on/off : left it blank, used E_All and -E_All etc. I've turn Display_Errors on/off as well. The Only time I get a 500 is with Display off and Errors blank/-E_All. And no custom page shows up, nor does the Default - just a blank white page. And I've then gone through it all again without .htaccess being around - to ensure it isn't something in htaccess playing up. – Clueless Mar 19 '11 at 12:52
  • 1
    Update : Okay - the host has the server set so the CHMod of 666 results in an Error - and I've managed to test the 500 response. Indeed - my little php 500 Error page works fine (now giving a 503 response instead). Still leaves the issue of generally testing such a response - really am surprised no simple method exists (Will re-attempt the various Error setting computations, and see if I can get it to flag that way now I know it works). – Clueless Mar 19 '11 at 19:47