lrhorer
Posts: 22
Joined: Sun Feb 22, 2015 6:35 pm

Apache2 not working with CGI enabled

Mon Oct 15, 2018 2:32 pm

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 192.168.1.21:59329] 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

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

<BODY style="background-color:#00EE66">
<blockquote><b>Aquarium Equipment Suspend</b></blockquote>
<form action="/cgi-bin/Timer.sh">
	<p><b><blockquote>
	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
	<br><br>
	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
	<br><br>
	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
	<br><br>
	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
	<br><br>
	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
	<br><br>
	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
	<br><br>
	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
	<br><br>
	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
	<br><br>
	<input type="submit" value="Submit">
	</blockquote></b></p>
</form>
</BODY>
</HTML>
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 192.168.1.21:59451] 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" "http://www.w3.org/TR/html4/loose.dtd">

etc. Nothing works. What should the header be?

fbe
Posts: 347
Joined: Thu Aug 17, 2017 9:08 pm

Re: Apache2 not working with CGI enabled

Mon Oct 15, 2018 8:31 pm

https://en.wikipedia.org/wiki/Common_Gateway_Interface

Code: Select all

Content-Type:text/html

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

lrhorer
Posts: 22
Joined: Sun Feb 22, 2015 6:35 pm

Re: Apache2 not working with CGI enabled

Tue Oct 16, 2018 3:17 am

No, I don't, plus it fails before it even gets to scanning the body of the page.

fbe
Posts: 347
Joined: Thu Aug 17, 2017 9:08 pm

Re: Apache2 not working with CGI enabled

Tue Oct 16, 2018 2:52 pm

Please read this: https://httpd.apache.org/docs/2.4/howto/cgi.html, 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

#!/bin/bash
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.

lrhorer
Posts: 22
Joined: Sun Feb 22, 2015 6:35 pm

Re: Apache2 not working with CGI enabled

Sun Oct 21, 2018 1:56 pm

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.

Return to “Advanced users”