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.

  • If you properly configured cgi-bin directory and your elf executable is in that directory, I guess that problem is inside your executable. Could you update your question with the exact error that you got complete with the reason phrase?
    – Lety
    Nov 9, 2014 at 17:31

2 Answers 2


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.


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

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

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
  • It can't output anything, because it is not executing the program, it just sends the raw binary program to the client.
    – ratlink
    Mar 18, 2015 at 2:14
  • Could you please check if your executable prints out http header before content?
    – Lety
    Mar 18, 2015 at 18:07
  • An other possibility is that your elf executable is not in cgi-bin directory. If this is true, try to move executable in default cgi-bin directory.
    – Lety
    Mar 18, 2015 at 18:21
  • It dosen't. I am just trying to get it to output plain text until I can find a way to get apache to execute the file.
    – ratlink
    Mar 18, 2015 at 18:21
  • I have set +ExecCGI on the /var/www directory.
    – ratlink
    Mar 18, 2015 at 18:23

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.

  • ScriptAlias is equivalent to the directive described above, but It is safer to avoid placing CGI scripts under the DocumentRoot in order to avoid accidentally revealing their source code if the configuration is ever changed. If you do choose to place your CGI scripts in a directory already accessible from the web, do not use ScriptAlias. Your problem was relative on how your web application send request probably it use example.com/cgi/foo instead of example.com/cgi-bin/foo. this link could help httpd.apache.org/docs/2.4/mod/mod_alias.html#scriptalias
    – Lety
    Aug 9, 2022 at 8:00
  • @Lety The root document path, in my case, is /var/www/project/public_html, so the CGIs are not public and can only be accessed through the special path in ScriptAlias. But I am 100% sure my path was /cgi-bin/ in the HTML. I actually changed it back and forth to test once I thought that could be the culprit. A shell script worked with either path but not my .ELF file... Aug 9, 2022 at 16:02
  • You should check apache configuration, it could be a rule in the main configuration file that it is applied to your Virtualhost and verify that your executable print out header before content.
    – Lety
    Aug 9, 2022 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.