Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 10:11 am

I am using an RPi4B with Raspbian Buster fully updated and with Apache2 installed.
I can show html and php files correctly in the browser so the webserver seems to work OK.

For a project I am doing I need to use a binary program as the form handler for a few forms used to configure an application.
After googling a lot I have deduced that I need to set aside a directory on the webserver as a cgi-bin handling directory and put my binary programs into that.

But I have failed to find out exactly how this is to be done...

Apache seems to be configured using a whole lot of conf files and I have lost my track there. :(
So I am running the server as is out of the box so to speak...
This means that it is serving out of directory /var/www/html

What I have done in order to see my web pages is the following:
- Installed apache2 and php via apt
- Created a symlink from my directory in /home/pi/www/config to /var/www/html/config
- Created a few html and php files in /home/pi/www/config, which can now be viewed via the web browser

But the install did not create any cgi-bin directory, which I can do and link as I did the config dir.
However, I don't know where to symlink this directory into the website.
Neither do I know how to enable the cgi-bin handling for this dir so it will be OK for apache to run my program as a form handler.

Can someone please tell me how to fix this?

PS: I do know how the binary program will get data from apache on form submissions and how it will send back information to the form submission client. This is not the problem, my issue is how to configure apache to accept my program as a form handler. DS
Bo Berglund
Sweden

User avatar
neilgl
Posts: 2111
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 11:32 am

Have you enabled cgi with this:

Code: Select all

sudo a2enmod cgi
sudo service apache2 restart

deepo
Posts: 574
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 11:34 am

Last I had to configure this I had to comment in a line:

Code: Select all

LoadModule cgid_module lib/apache2/modules/mod_cgid.so
Which is not active in a default configuration.

Then I had to add something like this:

Code: Select all

ScriptAlias /my-bin/ /somewhere/somewhere/
<Directory "/somewhere/somewhere">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		#Order allow,deny
		#Allow from all
		Require all granted
</Directory>
I could then call my cgi process executable as:

Code: Select all

/my-bin/myCgiProcess
with parameters

I hope some of this helps you.

/Mogens

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 2:44 pm

neilgl wrote:
Tue Dec 17, 2019 11:32 am
Have you enabled cgi with this:

Code: Select all

sudo a2enmod cgi
sudo service apache2 restart
No but this is now done:

Code: Select all

$ sudo a2enmod cgi
Enabling module cgi.
To activate the new configuration, you need to run:
  systemctl restart apache2
$ sudo systemctl restart apache2
Then I tried to access a test program in FireFox like this after copying the test program into cgi-bin:

Code: Select all

http://rpi4-gui/cgi-bin/getwebpage?namn=Olle&trafik=hemsk
Result was this:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.38 (Raspbian) Server at rpi4-gui Port 80
I have edited the following file:

Code: Select all

sudo nano /etc/apache2/sites-available/000-default.conf
and added the following close to the bottom:

Code: Select all

        #Below added by Bosse_B 2019-12-17
        ScriptAlias "/cgi-bin/" "var/www/html/cgi-bin/"
        <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Require all granted
        </Directory>
Still no go....
If I run my getwebpage program it will spit out a webpage on STDOUT that is complete with head, style, body etc and renders fine.
I have checked this by running the program from the command line and redirected output to a test.html file in the config dir.

So there seems to be another type of problem here.

This is what is inside the cgi-bin dir:

Code: Select all

pi@rpi4-gui:~ $ ls -l /var/www/html/cgi-bin/
total 1360
-rwxr-xr-x 1 pi pi 1390824 Dec 17 15:04 getwebpage
And here is the corresponding error in /var/log/apache2/error.log

Code: Select all

[Tue Dec 17 15:28:39.172811 2019] [cgi:error] [pid 23892] [client 192.168.119.166:57663] malformed header from script 'getwebpage': Bad header: <!DOCTYPE html>
The page that is generated by my program starts out like this:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title>Monitoring Config</title>
<meta charset="utf-8"/>
What is wrong with this? It renders just fine if it is located inside the config web dir.

EDIT:
I checked the source of this very page on the RPi forum and it starts out with exactly the same first line as is complained about!!!
Bo Berglund
Sweden

deepo
Posts: 574
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 2:48 pm

Your code:

Code: Select all

#Below added by Bosse_B 2019-12-17
        ScriptAlias "/cgi-bin/" "var/www/html/cgi-bin/"
        <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Require all granted
        </Directory>
I think that "var/www/html/cgi-bin/" is wrong, it needs a / at the start, but I also think that that path should be the same as the path listen in the Directory on the line below. But I could be wrong.

/Mogens

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 4:41 pm

deepo wrote:
Tue Dec 17, 2019 2:48 pm
Your code:

Code: Select all

#Below added by Bosse_B 2019-12-17
        ScriptAlias "/cgi-bin/" "var/www/html/cgi-bin/"
        <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Require all granted
        </Directory>
I think that "var/www/html/cgi-bin/" is wrong, it needs a / at the start, but I also think that that path should be the same as the path listen in the Directory on the line below. But I could be wrong.

/Mogens
Yes, you are right. I saw this after posting so I changed the config so it now reads:

Code: Select all

        ScriptAlias "/cgi-bin/" "/var/www/html/cgi-bin/"
        <Directory "/var/www/html/cgi-bin/">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Require all granted
        </Directory>
The error was because I copied the lines from the default file in the apache2 dir and forgot to modify the ScriptAlias and Directory directives.
I am not sure I restarted Apache after the last edit so I did so (again) now and then tried to load the page, same result as before: complaining about the bad header.
I am still not sure about the end of the path, if there should be a / or not.
But it seems to work kind-of since the complaint is about something my program spits out, so I know it is being called.

Here are the last lines of the Apache error log:

Code: Select all

[Tue Dec 17 17:28:11.340152 2019] [core:notice] [pid 24424] AH00094: Command line: '/usr/sbin/apache2'
[Tue Dec 17 17:28:28.428002 2019] [cgi:error] [pid 24428] [client 192.168.119.166:57961] malformed header from script 'getwebpage': Bad header: <!DOCTYPE html>
The first log line happened after I restarted Apache and the second when I tried to show the webpage...
Bo Berglund
Sweden

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

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 8:24 pm

Your cgi program has to output HTTP header lines, followed by an empty line, followed by content. "<!DOCTYPE html>" is content.
Maybe this thread helps to get started: https://www.raspberrypi.org/forums/view ... &p=1344292

deepo
Posts: 574
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 8:28 pm

I have it working now on my RPi 3B+ running Raspbian Buster.

I looked at these pages:
https://www.raspberrypi.org/forums/view ... p?t=148254
https://www.raspberrypi.org/documentati ... /apache.md
https://computer.howstuffworks.com/cgi3.htm
https://computer.howstuffworks.com/cgi4.htm

I installed Apache 2:

Code: Select all

sudo apt update
sudo apt install apache2 -y
I then enabled CGI, as neilgl suggested:

Code: Select all

sudo a2enmod cgi
sudo service apache2 restart
I created a small html file in /var/www/html/ called simpleform.html

Code: Select all

<html>
<body>
  <h1>A super-simple form<h1>
  <FORM METHOD=GET ACTION="https://www.howstuffworks.com/
cgi-bin/simpleform.cgi">
  Enter Your Name:
  <input name="Name" size=20 maxlength=50>
  <P>
  <INPUT TYPE=submit value="Submit">
  <INPUT TYPE=reset value="Reset">
  </FORM>
</body>
</html>
I created small CGI executable from this code: simpleform.c

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
  printf("Content-type: text/html\n\n");
  printf("<html>\n");
  printf("<body>\n");
  printf("<h1>The value entered was: ");
  printf("%s</h1>\n", getenv("QUERY_STRING"));
  printf("</body>\n");
  printf("</html>\n");
  return 0;
}
I then compiled it and installed the CGI executable:

Code: Select all

gcc simpleform.c -o simpleform.cgi
sudo cp simpleform.cgi /usr/lib/cgi-bin
sudo chown www-data:www-data /usr/lib/cgi-bin/simpleform.cgi
I could then open http://raspberrypi.local/simpleform.html in my browser:
CGI1.png
CGI1.png (6.55 KiB) Viewed 455 times
I entered my name and got this:
CGI2.png
CGI2.png (8.97 KiB) Viewed 455 times
Notice the URL:

Code: Select all

http://raspberrypi.local/cgi-bin/simpleform.cgi?Name=Mogens
/Mogens

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Configuring apache to use my program as cgi handler?

Tue Dec 17, 2019 11:28 pm

I tried your supersimple way and it worked fine here too.

Then I started putting back my complex style code including the style sheet (needed for the configuration form I will show once I got this basic stuff working) and it still worked even when I put the following lines at the top of the generated page output:

Code: Select all

Content-type: text/html

<!DOCTYPE html public >
<html>
<head>
<title>Monitoring Config</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
... etc ...
Note that the presence of the DOCTYPE line was what caused the error output all the time before!
I have no idea why it works now and not before.....

But I guess I should be happy and move forward! :D :lol:
Bo Berglund
Sweden

deepo
Posts: 574
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Configuring apache to use my program as cgi handler?

Wed Dec 18, 2019 12:02 pm

Bosse_B wrote:
Tue Dec 17, 2019 11:28 pm
I tried your supersimple way and it worked fine here too.

Then I started putting back my complex style code including the style sheet (needed for the configuration form I will show once I got this basic stuff working) and it still worked even when I put the following lines at the top of the generated page output:

Code: Select all

Content-type: text/html

<!DOCTYPE html public >
<html>
<head>
<title>Monitoring Config</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
... etc ...
Note that the presence of the DOCTYPE line was what caused the error output all the time before!
I have no idea why it works now and not before.....

But I guess I should be happy and move forward! :D :lol:
I don't think you are aware of the HTTP headers that precede the HTML code you are used to.
The HTTP headers describe what is in this specific telegram, i.e. the Content-type.
The headers section ends when two newline characters are found. After that you see the HTML code.

You can read about the HTTP headers here - there are many...
https://developer.mozilla.org/en-US/doc ... TP/Headers

/Mogens

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Configuring apache to use my program as cgi handler?

Wed Dec 18, 2019 1:07 pm

I thought that headers are the stuff between head and /head.
( I cannot find the lt and gt chars on the phone keyboard...).
After all I am just doing a config utility....
Bo Berglund
Sweden

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Configuring apache to use my program as cgi handler?

Wed Dec 18, 2019 3:08 pm

The bits between the <head> and </head> are the HTML page headers. The HTTP file headers come before any HTML and (can) tell the browser lots of things about the file that it is about to receive, such as the file format (text, html, jpg, compressed, filesize, etc.)
Unreadable squiggle

Return to “General discussion”