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.

7
  • 2
    Possible duplicate of "End of script output before headers" error in Apache Commented Oct 14, 2016 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. Commented Oct 14, 2016 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. Commented Oct 14, 2016 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. Commented Oct 14, 2016 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... Commented Oct 14, 2016 at 22:19

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.