title: PHP exec() under Apache returns null and 255 when running a script
body: <p>Ok, after having bashed my head on this for hours I decided to ask for help. I have a Windows Server 2008 running Apache 2.4 and PHP 7.1. My application must run a PHP script on the server when the user clicks a button on the browser.</p>
  
  <p>This is working fine on my desktop with Windows 10. However, on the server, exec() returns "null" and an exit code of 255.</p>
  
  <p>I read all I could find on exec() issues and tried the following:</p>
  
  <pre><code>exec("C:\\PHP7\\php.exe -v", $output);
  </code></pre>
  
  <p>I got the proper response containing PHP's version information.</p>
  
  <p>Then I decided to check the configuration files:</p>
  
  <pre><code>exec("C:\\PHP7\\php.exe --ini", $output);
  </code></pre>
  
  <p>All files were in place.</p>
  
  <p>Then I decided to perform a syntax check on my script:</p>
  
  <pre><code>exec("C:\\PHP7\\php.exe -d display_errors=1 -l C:\\Apache24\\htdocs\\script.php", $output);
  </code></pre>
  
  <p>No errors were found.</p>
  
  <p>Finally I decided to check the user account:</p>
  
  <pre><code>exec("whoami", $output);
  </code></pre>
  
  <p>Got "NT Authority\SYSTEM" as expected. To make sure that the script was able to run under the SYSTEM account I used SysInternals psexec:</p>
  
  <pre><code>psexec -s C:\PHP7\php.exe C:\Apache24\htdocs\script.php
  </code></pre>
  
  <p>Everything ran smoothly.</p>
  
  <p>In other words, the script shows no problems when executed from the command line, either under a user account or the system account. I have also proven that PHP is being properly invoked by exec().</p>
  
  <p>So, then I decided to check for "hidden" errors in my code adding the following two lines to the very beginning of the script:</p>
  
  <pre><code>error_reporting(E_ALL);
  ini_set('display_errors', 1);
  </code></pre>
  
  <p>But, no joy. And I'm out of ideas.</p>
  
  <p>Can any good soul help me on this?</p>
  
  <p>Thanks a bunch,</p>
  
  <p>Miguel.</p>