title: PHP shell_exec() unable to run part of shell script
body: <p>I have encountered a strange problem. I have created a gitlab custom webhook. The webhook service is written in php (gitlab.php). The php script, when called with proper payload, should start an automated deployment. The deployment script is basically a shell script (deploy.sh)</p>
  <p><strong>Code of gitlab.php</strong></p>
  <pre><code>&lt;?php
  $json = file_get_contents('php://input');
  // Converts it into a PHP object
  $data = json_decode($json, true);
  $secret = &quot;&quot;;
  foreach (getallheaders() as $name =&gt; $value) { 
      if($name=='X-Gitlab-Token') {
          $secret = $value;
      }
  }
  
  if($secret==&quot;mysecretcode&quot; &amp;&amp; $data['object_kind']==&quot;push&quot;) {
      file_put_contents(&quot;/opt/lampp/htdocs/autodeploy/autodeploy.log&quot;, &quot;Webhook auto deployment started at &quot;. date(&quot;Y-m-d H:i:s&quot;) . &quot;\n&quot;. shell_exec(&quot;sh /opt/lampp/htdocs/autodeploy/deploy.sh&quot;).&quot;\n**********************************************\n\n&quot;, FILE_APPEND);
      die(&quot;Deployment success&quot;);
  }
  else {
   die(&quot;Auth token invalid or no triggerable event found&quot;);
  }
  ?&gt;
  </code></pre>
  <p><strong>Code of deploy.sh</strong></p>
  <pre><code>#!/bin/bash
  cd &quot;/opt/lampp/htdocs/nodejs/myapp&quot;
  echo &quot;Stopping application...&quot;
  forever stop index.js
  cd ..
  sudo rm -rf myapp
  echo &quot;Pulling latest code from gitlab...&quot;
  git clone -b &quot;master&quot; https://myusername%40domain.com:MyEncodedPass123@gitlab.com/myusername/myapp.git
  cd myapp
  echo &quot;Installing dependencies...&quot;
  npm install
  echo &quot;Starting application...&quot;
  forever start index.js
  echo &quot;Application started on port 3000.&quot;
  </code></pre>
  <p>When I push code to gitlab, it triggers the webhook and initiates the deployment process and I see my webhook file gitlab.php returned success response to Gitlab and has written following output in autodeploy.log file</p>
  <pre><code>Webhook auto deployment started at 2021-10-03 13:32:07
  Stopping application...
  Pulling latest code from gitlab...
  Installing dependencies...
  Starting application...
  Application started on port 3000.
  
  **********************************************
  </code></pre>
  <p>But, the deployment actually never happens. It just runs the <code>echo</code> statements (and may be <code>cd</code>, <code>rm</code> etc as well) and rest of the shell commands are kind of ignored or not executed. It for sure doesn't run the <code>git clone</code> command, because I do not see latest code getting refreshed on my server from gitlab. Not sure though what happens to the <code>npm</code> and <code>forever</code> commands.</p>
  <p>By the way, this is a Ubuntu VPS server with latest ApacheFriends XAMPP server installed, php v8. All the three files mentioned above reside at the same path i.e. <code>/opt/lampp/htdocs/autodeploy</code> and my deploy.sh script tries to deploy a separate nodejs application in <code>/opt/lampp/htdocs/nodejs/myapp</code> folder. Strangely enough, when I run deploy.sh directly from terminal it deploys the latest code successfully. Which means, all the statements in the script gets executed as expected. It only fails when executed from the php script using shell_exec() function.</p>
  <p>Any clue what could be the reason?</p>