2

How do I get apache2 to run binaries as cgi. I have a test page, that when I access it, it returns the error code 500 Internal Server Error, and there is an

  "End of script output before headers" 

error in /var/log/apache2/error.log.

This is not a script, it is an elf executable.

Lety
  • 6,089
  • 2
  • 32
  • 38
ratlink
  • 21

2 Answers2

1

According to Apache documentation:

First, all output from your CGI program must be preceded by a MIME-type header. This is HTTP header that tells the client what sort of content it is receiving. Most of the time, this will look like:

Content-type: text/html

Secondly, your output needs to be in HTML, or some other format that a browser will be able to display. Most of the time, this will be HTML, but occasionally you might write a CGI program that outputs a gif image, or other non-HTML content.

I guess that your program (elf executable) doesn't print out http header before content.


UPDATE

In order to execute CGI program in your home directory /var/www you shoud add:

<Directory /var/www>
    Options +ExecCGI
    AddHandler cgi-script .bin
</Directory>

Where AddHandler directive allow CGI program execution for all file ending with .bin extension in /var/www directory.

Another possibility is to create your cgi-bin directory and put your executable in it.

In this case, add in your Apache configuration file:

<Directory /var/www/mycgi-bin>
    Options ExecCGI
    SetHandler cgi-script
</Directory>
Alexis Wilke
  • 2,787
Lety
  • 6,089
  • 2
  • 32
  • 38
0

On Ubuntu 20.04, I ran in a problem which I don't think I resolved 100% yet. I just can't explain it. At first I had this ScriptAlias definition:

ScriptAlias /cgi-bin/ /var/www/project/cgi-bin/

After about 4h of research, I found out that somehow the cgi-bin alias path was in conflict for binary executables (.ELF files). Strangely enough, it worked just fine with a shell script, just not with a binary... So I finally tried with:

ScriptAlias /cgi/ /var/www/project/cgi-bin/

and it worked!!! Any other changes to my settings had no bearing on the results (i.e. everything was properly setup).

My definition is under a <VirtualHost ...> definition, so it should have no conflicts. But conflicts there were for sure.


Side Note

Make sure to restart each time you make changes to your settings to make sure they are effective and you are testing something expected:

$ sudo systemctl restart apache2

Not only that, if you mistype something, it's going to let you know right away.

Alexis Wilke
  • 2,787