Friday, April 15, 2016

Set PHP to always redirect on error 500s

Leave a Comment

On my website I would like it to redirect all error 500 errors to /500.html. As of current some of the page displays with GET http://localhost/ [HTTP/1.0 500 Internal Server Error 1ms] in the Developer Tools. I have tried implementing this by editing /etc/apache2/sites-enabled/000-default.conf. The file looks like this:

<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com  ServerAdmin webmaster@localhost DocumentRoot /var/www/html  # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn  ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined  # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf     ErrorDocument 500 /500.html </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet 

I have also tried editing apache2.conf and .htaccess but I get the same result.

P.S: I have tried restarting Apache every time!

3 Answers

Answers 1

If you are trying to intercept 500 errors from PHP in apache using mod_php with SetHandler (A very common way), then the answer is that you can't. To the best of my knowledge it's not a supported feature.

However there are ways such as using PHP via it's FastCGI interface (PHP-FPM) and the Apache proxy module to achieve what you require.

First, you may need to ensure that mod_proxy and proxy_fcgi_module are installed on your server and enabled in the Apache config like so:

LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 

Also ensure that PHP-FPM is installed and running as a service, the default port it uses is 9000.

Then replace your apache PHP configuration (possibly in /etc/httpd/conf.d/php.conf) with the following.

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1 DirectoryIndex /index.php index.php 

This will mean that you are now using the Apache Proxy module to send traffic to PHP. It is possible to instruct Apache to intercept error responses from the proxy module and use custom error handlers with the following in your VirtualHost section.

ProxyErrorOverride On ErrorDocument 500 /500.html 

Answers 2

Custom error documents are configured using the ErrorDocument directive, which may be used in global, virtualhost, or directory context. It may be used in .htaccess files if AllowOverride is set to FileInfo.

ErrorDocument 500 "Sorry, our script crashed. Oh dear" ErrorDocument 500 /errors/not_found.html  ErrorDocument 500 http://error.example.com/server_error.html 

The syntax of the ErrorDocument directive is:

ErrorDocument <3-digit-code> <action> 

where the action will be treated as:

  1. A local URL to redirect to (if the action begins with a "/").
  2. An external URL to redirect to (if the action is a valid URL).
  3. Text to be displayed (if none of the above). The text must be wrapped in quotes (") if it consists of more than one word. When redirecting to a local URL, additional environment variables are set so that the response can be further customized. They are not sent to external URLs.

Apache documentation

Keep in mind that the Apache looks for the 404 page located within the site's server root. Meaning that if you place the new error page in a deeper subdirectory, you need to include that in the line.

Answers 3

I found this code here:

<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ErrorDocument 500 /custom_500.html </VirtualHost> 

then the custom pages go here: /var/www/html and the file you need to edit is this or similar:

   /etc/apache2/sites-enabled/000-default.conf 

also enable these 2 mods, and then check for syntax errors and restart apache

sudo a2enmod proxy sudo a2enmod proxy_http sudo apache2ctl configtest sudo service apache2 restart 

tell me if this works for your server, because on my server after some tries, it worked

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment