title: Unable to connect with Net_SSH2 from inside Apache
body: <p>I am attempting to SSH from PHP but I get the following error:</p>
  
  <p>Notice: Cannot connect to [host]. Error 13. 
  Permission denied in /usr/share/php/Net/SSH2.php on line 875 </p>
  
  <p>Here is the code:</p>
  
  <pre><code>&lt;?PHP
  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);
  include_once('Net/SSH2.php');
  define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
  
  $ssh = new Net_SSH2($host);
  
  if( $ssh-&gt;login($id, $pw) )
  {
    error_log("logged");
    $result['data'] = $ssh-&gt;exec('dir');
  }
  else
  {
    error_log( $ssh-&gt;getLog() );
  }
  ?&gt;
  </code></pre>
  
  <p>But when I run this same code from the command line with apache out of the mix it runs fine.</p>
  
  <p>I have the EXACT same problem with a python script that uses paramiko to SSH and is called from apache.  It runs fine from command line but fails with a permission error when called from PHP in apache.  Using this script was just a test; python will not be used in the final solution.</p>
  
  <p>So, why is SSH working outside apache but not from within?  I have used su to run the PHP code as apache from the command line and that also works, so it is not a user permission problem.</p>
  
  <p>UPDATE:</p>
  
  <p>AAaarrrggh!  Bitten again by SELinux.  This page had the solution:
  <a href="https://stackoverflow.com/questions/12370746/php-run-git-got-ssh-permission-denied">php run git got &quot;ssh Permission denied&quot;</a></p>
  
  <p>The answer is:</p>
  
  <pre><code>setsebool -P httpd_can_network_connect=1
  </code></pre>
  
  <p>Basically, apache was not authorized to initiate network connections!?!?</p>
  
  <p>I neglected to mention that this is on a CentOS system and as such fell under the watchful caring eye of SELinux, my mistake.</p>