Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I copied my file into /usr/lib/cgi-bin and ran:

chmod +rx

In my apache2.conf file, I added the following lines:

<Files ~ "\.pl$">
    Options +ExecCGI

AddHandler cgi-script .pl

And restarted the Apache server:

/etc/init.d/apache2 start

But when I go to http://localhost/cgi-bin/ I get the following error:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.

Apache/2.2.17 (Ubuntu) Server at localhost Port 80

Here's my error log file:

[Thu Jul 28 19:28:43 2011] [error] [client] (2)No such file or directory: exec of '/usr/lib/cgi-bin/' failed
[Thu Jul 28 19:28:43 2011] [error] [client] Premature end of script headers:

But this is what my /usr/lib/cgi-bin directory looks like:

/usr/lib/cgi-bin$ ls -l
total 8
-rwxr-xr-x 1 root root 331 2011-07-28 19:01
-rwxr-xr-x 1 root root 335 2011-07-28 18:50

Can you please help me fix this... thanks!

share|improve this question
Can you add the contents of that file to your question? Seems like it's having trouble running it. – Shane Madden Aug 12 '11 at 21:07
hi Shane, here's the code: #!usr/bin/perl ## ## printenv -- demo CGI program which just prints its environment ## use CGI qw(:standard escapeHTML); $| = 1; print "Content-type: text/html; charset=iso-8859-1\n\n"; foreach $var (sort(keys(%ENV))) { $val = $ENV{$var}; $val =~ s|\n|\\n|g; $val =~ s|"|\\"|g; print "${var}=\"${val}\"<br>\n"; } – ba-apache Aug 13 '11 at 3:52
Leading slash in interpreter's path is missing, replace #!usr/bin/perl with #!/usr/bin/perl – Vladimir Blaskov Aug 13 '11 at 8:10
did that, still no difference. is there a series of steps i should follow in a specific order bcoz I could try to do the whole thing all over again – ba-apache Aug 13 '11 at 14:26

Double-check the first line of your script.

If the line does not begin with #! or perl is installed in a different location than the path following the #! (e.g., /usr/local/bin/perl vs /usr/bin/perl), you will get that error.

If the path to perl appears correct, and you've copied over the script from a Windows machine, you may be being tripped up by a carriage return character at the end of the line.

You can use cat -v /usr/lib/cgi-bin/ | head -1 to verify that there isn't a ^M at the end of the line.

If there is, you can use perl -pi.bak -e 's/\r+$//' /usr/lib/cgi-bin/ to remove it.

share|improve this answer
hi Kanji, I double checked the path to perl and it is correct. I used the cat function and there isn't a ^M. here's the output of the cat: cat -v /usr/lib/cgi-bin/ | head -1 #!usr/bin/perl – ba-apache Aug 13 '11 at 3:35
Is that a straight cut and paste? If so, you are missing a / before usr. If it isn't, are you able to run the script directly from the shell? – Kanji Aug 13 '11 at 4:09
yes indeed, I ran it from the shell and it executes fine, here's the beginning of the output from that : Content-type: text/html; charset=iso-8859-1 COLORTERM="gnome-terminal"<br> COMPIZ_CONFIG_PROFILE="ubuntu"<br> DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-WtKdwSEdHu,guid=49985f2d6dfd68‌​26a291778300000015"<br> DEFAULTS_PATH="/usr/share/gconf/gnome.default.path"<br> DESKTOP_SESSION="gnome"<br> DISPLAY=":0"<br> – ba-apache Aug 13 '11 at 4:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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