Page 1 of 1

Apache2 not working with CGI enabled

Posted: Mon Oct 15, 2018 2:32 pm
by lrhorer
I am running Apache2.4.25 under Raspbian. Prior to enabling CGI, everything was working fine, but I need to implement some scripts, so I ran a2enmod, and the system enabled CGI. Now I can't get anythng to work, however. I can't even get a flat html page to load. Apache thinks it is a script and complains:

[Mon Oct 15 09:02:02.196180 2018] [cgid:error] [pid 17010:tid 1995407360] (8)Exec format error: AH01241: exec of '/usr/lib/cgi-bin/Thermostat/index.html' failed
[Mon Oct 15 09:02:02.198189 2018] [cgid:error] [pid 16450:tid 1945105456] [client] End of script output before headers: index.html

I can't even get to /var/www. What's worse, when I try to run a simple script it croaks on the header, no matter what I try for !DOCTYPE.

So, for example, if I take the following file, index.htnl:

Code: Select all

<TITLE> Aquarium Relay Control </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Leslie Rhorer">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<BODY style="background-color:#00EE66">
<blockquote><b>Aquarium Equipment Suspend</b></blockquote>
<form action="/cgi-bin/">
	Relay 1 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R1" value="15"> 15 Minutes <input type="radio" name="R1" value="30"> 30 Minutes <input type="radio" name="R1" value="60"> 60 Minutes <input type="radio" name="R1" value="120"> 120 Minutes
	Relay 2 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R2" value="15"> 15 Minutes <input type="radio" name="R2" value="30"> 30 Minutes <input type="radio" name="R2" value="60"> 60 Minutes <input type="radio" name="R2" value="120"> 120 Minutes
	Relay 3 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R3" value="15"> 15 Minutes <input type="radio" name="R3" value="30"> 30 Minutes <input type="radio" name="R3" value="60"> 60 Minutes <input type="radio" name="R3" value="120"> 120 Minutes
	Relay 4 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R4" value="15"> 15 Minutes <input type="radio" name="R4" value="30"> 30 Minutes <input type="radio" name="R4" value="60"> 60 Minutes <input type="radio" name="R4" value="120"> 120 Minutes
	Relay 5 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R5" value="15"> 15 Minutes <input type="radio" name="R5" value="30"> 30 Minutes <input type="radio" name="R5" value="60"> 60 Minutes <input type="radio" name="R5" value="120"> 120 Minutes
	Relay 6 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R6" value="15"> 15 Minutes <input type="radio" name="R6" value="30"> 30 Minutes <input type="radio" name="R6" value="60"> 60 Minutes <input type="radio" name="R6" value="120"> 120 Minutes
	Relay 7 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R7" value="15"> 15 Minutes <input type="radio" name="R7" value="30"> 30 Minutes <input type="radio" name="R7" value="60"> 60 Minutes <input type="radio" name="R7" value="120"> 120 Minutes
	Relay 8 &nbsp &nbsp &nbsp &nbsp <input type="radio" name="R8" value="15"> 15 Minutes <input type="radio" name="R8" value="30"> 30 Minutes <input type="radio" name="R8" value="60"> 60 Minutes <input type="radio" name="R8" value="120"> 120 Minutes
	<input type="submit" value="Submit">
and run the simple script, index.cgi:

#! /bin/bash
cat /usr/lib/cgi-bin/Thermostat/index.html

I get:

[Mon Oct 15 09:26:43.080333 2018] [cgid:error] [pid 17196:tid 1971319856] [client] malformed header from script 'index.cgi': Bad header: <!DOCTYPE HTML>

I get either that or else and "Invalid character" report no matter what I try:

#<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
#<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">

etc. Nothing works. What should the header be?

Re: Apache2 not working with CGI enabled

Posted: Mon Oct 15, 2018 8:31 pm
by fbe

Code: Select all


<!DOCTYPE html>
Maybe you need to specify a charset if you use non ascii characters in your HTML.

Re: Apache2 not working with CGI enabled

Posted: Tue Oct 16, 2018 3:17 am
by lrhorer
No, I don't, plus it fails before it even gets to scanning the body of the page.

Re: Apache2 not working with CGI enabled

Posted: Tue Oct 16, 2018 2:52 pm
by fbe
Please read this:, starting with "Writing a CGI program"

Apache2 is working with CGI enabled, but a CGI program must output a HTTP header line first, e.g. "Content-Type:text/html". Your index.cgi doesn't do that and this is, what Apache2 is complaining about. "<!DOCTYPE html>" is part of the content.

Your index.cgi content should be:

Code: Select all

echo -e "Content-Type:text/html\n"
cat /usr/lib/cgi-bin/Thermostat/index.html
If Apache executes it, the output is as shown above.

Re: Apache2 not working with CGI enabled

Posted: Sun Oct 21, 2018 1:56 pm
by lrhorer
I could have sworn I tried that already. Either I am mis-remembering, or else I had something else wrong, as well. In any case, thanks.