1

I am running a server on Debian, using php-5

My php file is called pwrite.php, has 755 rights and is located at:

/usr/lib/cgi-bin

 <?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?> 

I am expecting it to write 'newfile.txt' in cgi-bin folder

Using the error logs for apache2 at

 tail -f /var/log/apache2/error.log

I see 2 errors;

[[cgid:error] [pid 1328:tid 1996410880] (8)Exec format error: AH01241: exec of '/usr/lib/cgi-bin/phptest.php' failed
[[cgid:error] [pid 1268:tid 1963979824] [client ::1:35573] End of script output before headers: phptest.php

Both of these errors only happen when I try and execute code on my server using CGI.

I have a working bash.sh script in the CGI folder that displays system information.

Didn't find a lot of info on 500 errors, except they suck. I did see this link and wonder if using cgi-bin is the issue here?

https://answers.stanford.edu/solution/error-cgi-exec-format-error-when-trying-access-data-file-my-script-created

I ultimately want my PHP to resolve the URL requested (site.com/ar1/arg2) and parse this into two variables in order to write pages dynamically (/var/www/html/ar1/arg2/result.json)

I'm sure that my php file has the correct permissions, and I'm pretty sure I've enabled .PHP files in my default cgi-bin folder to run without additional parsing.

  • 2
    Possible duplicate of ["End of script output before headers" error in Apache](http://stackoverflow.com/questions/22307610/end-of-script-output-before-headers-error-in-apache) – fvu Oct 14 '16 at 21:59
  • typically php scripts are not run from the cgi directory like normal cgi scripts. Apache is setup so that any file within the document root with a `.php` extension is run through the php parser. The issue you are seeing is that with cgi, the script needs to send out it's header information manually. With normal php operation, this is handled for you. – Jonathan Kuhn Oct 14 '16 at 21:59
  • @JonathanKuhn Actually that depends. "Apache is setup..." - that is only true _if_ it has been setup like that. So _if_ the apache php module has been installed, configured and enabled. That is not the case per default! Many sites use php via the cgi-bin mechanism for various reasons. – arkascha Oct 14 '16 at 22:06
  • @arkascha actually it is by default setup as an apache module. I just did this last week on an ubuntu system. Apt-get installed the module and enabled it automatically. This is also why i said "typically", as in not for every install. – Jonathan Kuhn Oct 14 '16 at 22:13
  • @JonathanKuhn No, what I meant is that just because you install the apache http server does not automatically mean that the php module is installed. That would be a pretty stupid thing. You have to actually install php for that. And even _if_ the Ubuntu distribution would chose such a strange setup that would be only one single distribution. One that is not that wide spread in professional usage, actually... – arkascha Oct 14 '16 at 22:19
  • I tried 'opening my cgi-bin file' from: http://stackoverflow.com/questions/10411272/running-php-file-outside-of-documentroot-cgi-bin-folder and also confirmed that prwrite.php has 755 settings.... Is it possible to confirm my current cgi-bin parsing status to confirm if this is indeed the cause? – Finn Geraghty Oct 14 '16 at 22:21
  • As it happens I did actually install php5 via .deb packages (and after doing the same with a half dozen dependencies) - as there was nothing in apt-store for my armhf device (Rpi)... Could this be relevant? – Finn Geraghty Oct 14 '16 at 22:26

0 Answers0