[apache] "End of script output before headers" error in Apache

Apache on Windows gives me the following error when I try to access my Perl script:

Server error!

The server encountered an internal error and was unable to complete your request.

Error message: 
End of script output before headers: sample.pl

If you think this is a server error, please contact the webmaster.

Error 500

localhost
Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.3

this is my sample script

#!"C:\xampp\perl\bin\perl.exe"
print "Hello World";

but not working on browser

This question is related to apache perl

The answer is


Basing above suggestions from all, I was using xampp for running cgi scripts. Windows 8 it worked with out any changes, but Cent7.0 it was throwing errors like this as said above

AH01215: (2)No such file or directory: exec of '/opt/lampp/cgi-bin/pbsa_config.cgi' failed: /opt/lampp/cgi-bin/pbsa_config.cgi, referer: http://<>/MCB_HTML/TestBed.html

[Wed Aug 30 09:11:03.796584 2017] [cgi:error] [pid 32051] [client XX:60624] End of script output before headers: pbsa_config.cgi, referer: http://xx/MCB_HTML/TestBed.html

Try:

Disabled selinux

Given full permissions for script, but 755 will be ok

I finaly added like -w like below

#!/usr/bin/perl -w*

use CGI ':standard';
{

        print header(),
         ...
         end_html();


}

**-w** indictes enable all warnings.It started working, No idea why -w here.

In my case I had a similar problem but with c ++ this in windows 10, the problem was solved by adding the environment variables (path) windows, the folder of the c ++ libraries, in my case I used the codeblock libraries:

C:\codeblocks\MinGW\bin

Probably this is an SELinux block. Try this:

# setsebool -P httpd_enable_cgi 1
# chcon -R -t httpd_sys_script_exec_t cgi-bin/your_script.cgi

If using Suexec, ensure that the script and its directory are owned by the same user you specified in suexec.

In addition, ensure that the user running the cgi script has permissions execute permissions to the file AND the program specified in the shebang.

For example if my cgi script starts with

#! /usr/bin/cgirunner

Then the user needs permissions to execute /usr/bin/cgirunner.


Had the same error on raspberry-pi. I fixed it by adding -w to the shebang

#!/usr/bin/perl -w

If this is a CGI script for the web, then you must output your header:

#!"C:\xampp\perl\bin\perl.exe"

print "Content-Type: text/html\n\n";
print "Hello World";

The following error message tells you this End of script output before headers: sample.pl

Or even better, use the CGI module to output the header:

#!"C:\xampp\perl\bin\perl.exe"

use strict;
use warnings;

use CGI;

print CGI::header();
print "Hello World";

You may be getting this error if you are executing CGI files out of a home directory using Apache's mod_userdir and the user's public_html directory is not group-owned by that user's primary GID.

I have been unable to find any documentation on this, but this was the solution I stumbled upon to some failing CGI scripts. I know it sounds really bizarre (it doesn't make any sense to me either), but it did work for me, so hopefully this will be useful to someone else as well.


Since no answer is accepted, I would like to provide one possible solution. If your script is written on Windows and uploaded to a Linux server(through FTP), then the problem will raise usually. The reason is that Windows uses CRLF to end each line while Linux uses LF. So you should convert it from CRLF to LF with the help of an editor, such Atom, as following enter image description here


So for everyone starting out with XAMPP cgi

change the extension from pl to cgi
change the permissions to 755

mv test.pl test.cgi
chmod 755 test.cgi

It fixed mine as well.


For future reference:

This is typically an error that occurs when you are unable to view or execute the file, the reason for which is generally a permissions error. I would start by following @Renning 's suggestion and running chmod 755 test.cgi (obviously replace test.cgi with the name of your cgi script here).

If that doesn't work there are a couple other things you can try. I once got this error when I created test.cgi as root in another user's home. The fix there was to run chmod user:user test.cgi where user is the name of the user who's home you're in.

The last thing I can think of is making sure that your cgi script is returning the proper headers. In my ruby script I did it by putting puts "Content-type: text/html" before I actually outputted anything to the page.

Happy coding!


Internal error is due to a HIDDEN character at end of shebang line !! ie line #!/usr/bin/perl

By adding - or -w at end moves the character away from "perl" allowing the path to the perl processor to be found and script to execute.

HIDDEN character is created by the editor used to create the script


Check file permissions.

I had exactly the same error on a Linux machine with the wrong permissions set.

chmod 755 myfile.pl

solved the problem.