I have the following entry under apache2.conf in my Debian box.

AddHandler cgi-script .cgi .pl
Options +ExecCGI
ScriptAlias /cgi-bin/ /var/www/mychosendir/cgi-bin/

<Directory /var/www/mychosendir/cgi-bin>
Options +ExecCGI -Indexes
allow from all

Then I have a perl cgi script stored under these directories and permissions:

nvs@somename:/var/www/mychosendir$ ls -lhR 
total 12K
drwxr-xr-x 2 nvs nvs 4.0K 2010-04-21 13:42 cgi-bin

total 4.0K
-rwxr-xr-x 1 nvs nvs 90 2010-04-21 13:40 test.cgi

However when I tried to access it in the web browser:


They gave me this error:

[Wed Apr 21 15:26:09 2010] [error] [client] (8)Exec format error: exec of '/var/www/mychosendir/cgi-bin/test.cgi' failed
[Wed Apr 21 15:26:09 2010] [error] [client] Premature end of script headers: test.cgi

What's wrong with it?


I also have the following entry in my apache2.conf:

<Files ~ "^\.ht">
    Order allow,deny
    Deny from all

And the content of test.cgi is this:

#!/usr/bin/perl -wT
print "Content-type: text/html\n\n";
print "Hello, world!\n";

migrated from stackoverflow.com Apr 21 '10 at 13:40

This question came from our site for professional and enthusiast programmers.


Make sure you have a <Directory> section for the cgi-bin directory. And make sure "allow from all" is in it.

<Directory /var/www/mychosendir/cgi-bin>
    Options +ExecCGI -Indexes
    allow from all

Also...your ScriptAlias is for /cgi-bin/. Your URL is /mychosendir/cgi-bin. Unless you have some rewrite magic going on, your url should probably be http://my.host.com/cgi-bin/test.cgi , or you'll need to change your ScriptAlias line to look like

ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin

The error you posted in your update, sounds like you don't have a #! line at the beginning of your script. You'll need one, and it should look like

  • 1
    The directory is there, but unless you have a section in the config file that grants access to it, Apache won't let you see it. – cHao Apr 21 '10 at 6:12
  • 1
    Updated my answer. Reread. :) – cHao Apr 21 '10 at 6:17
  • 1
    The first thing in the ScriptAlias directive is the url you want to refer to that directory. So you'll probably want to get rid of the first /var/www in your ScriptAlias line. – cHao Apr 21 '10 at 6:24
  • 1
    I mean like this: ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/ – cHao Apr 21 '10 at 6:49
  • 1
    I see it. I'm running the same code on one of my servers right now, so IF your perl is at /usr/bin/perl, then the script should be fine. – cHao Apr 21 '10 at 6:56

Are you sure the Apache user/group has read and execute access for the given files? Usually this user is called httpd or www-data.

  • How do I check and enable that? – neversaint Apr 21 '10 at 6:10
  • First find out as which user Apache is running. This can be done by checking your httpd.conf or apache2.conf - look for the User and Group settings. Next, ls -al /var/www/mychosendir/cgi-bin to see the permissions and file owner. The user under which Apache is running must have access to these files. They should also contain the execute bit. – Htbaa Apr 21 '10 at 6:44

"Premature end of script headers" means that the CGI script is not printing out the correct HTTP header. It needs to print out at least

Content-Type: text/html;

Also, you have

ScriptAlias /cgi-bin/ /var/www/mychosendir/cgi-bin/

and yet you are asking for


That should be


or say

ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/
  • It does have. See my update on the test.cgi code. – neversaint Apr 21 '10 at 6:38
  • I really want it to be accessed here: http://myhost.com/mychosendir/cgi-bin/test.cgi. Then how should I modify the ScriptAlias? I also tried this ScriptAlias /cgi-bin/ /mychosendir/cgi-bin/ but internal server error. – neversaint Apr 21 '10 at 6:44
  • 2
    You are too dependent on other people to solve all your problems. – Kinopiko Apr 21 '10 at 6:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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