Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure apache to send an email on every HTTP error 500

I have some server applications running on apache2; Ruby on Rails, PHP and others.

In all cases I would like apache to send me an email whenever apache responds a HTTP error 500 Internal server error.

How can I do that?

like image 609
Jarl Avatar asked Aug 31 '12 06:08

Jarl


People also ask

Where are Apache default error pages?

The location of Apache 2.4 standard error messages is inside the executable. You can easily find them if you peek inside the executable with some hex editor.

Is the error code that is sent by Apache as a response?

This error message indicates that a website visitor tried to access a password protected page without authenticating (with valid username and password). Where 1.2. 3.4 is the IP address of the visitor.

What is Apache server error?

Apache gives 500 Internal Server Error when there is a server-side error that prevents Apache from processing a request and returning a proper response. This can be due to various reasons such as faulty code, inadequate file permissions, missing files referenced in code, etc.


3 Answers

One way you can do this is use a php script that sends email, as well as output some sort of 500 message. Then use the ErrorDocument directive:

ErrorDocument 500 /path/to/script/that/sends/email.php
like image 121
Jon Lin Avatar answered Oct 05 '22 03:10

Jon Lin


It is not possible to configure apache to send emails.

There are some things you can do to achieve the same result:

  1. You can use the ErrorDocument directive to let it point to some CGI script, that be a perl, PHP, or any other server-side executed script. The script can then send an email and respond a Error 500 document.

  2. You can use some external tool to watch your apache log files and configure theese tools to send you emails. There are several tools and ways to do this. For Linux many tools and methods are suggested on https://serverfault.com/questions/45246/linux-monitor-logs-and-email-alerts

  3. Write an apache module. I think that mod_tee is a good starting point.

like image 40
Jarl Avatar answered Oct 05 '22 04:10

Jarl


Create a script called log_monitor.sh:

#!/usr/bin/perl -w

use strict;

my $cachefile="/var/cache/lastpos-apache2-scan4maxclntOrSigKill";
my $logfile="/var/log/httpd/error_log";
my $searchstr=" 500 ";

my $lastpos=0;
if (-f $cachefile) {
    open FH,"<".$cachefile;
    $lastpos=<FH>;
    close FH;
};

my $newpos=(stat $logfile)[7];

open FH,"<".$logfile;
seek FH,$lastpos,0;
while (<FH>) {
    print if m/$searchstr/i;
};
close FH;

open FH,">".$cachefile;
print FH $newpos;
close FH;

modify $searchstr as needed. note the spaces around "500", since you don't want to match errors which contain a 404 on a file that has "500" in its path or filename (among other places).

configure the script to run every X minutes via cron. the greater the value of X, the less emails you will get (only 1 email every X minutes for all the errors that match the strings you supply). the results of the cron job will get emailed to you automatically (if cron is setup properly)

like image 31
Gaia Avatar answered Oct 05 '22 04:10

Gaia