I am attempting to SSH from PHP but I get the following error:
Notice: Cannot connect to [host]. Error 13. Permission denied in /usr/share/php/Net/SSH2.php on line 875
Here is the code:
<?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->login($id, $pw) )
{
error_log("logged");
$result['data'] = $ssh->exec('dir');
}
else
{
error_log( $ssh->getLog() );
}
?>
But when I run this same code from the command line with apache out of the mix it runs fine.
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.
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.
UPDATE:
AAaarrrggh! Bitten again by SELinux. This page had the solution: php run git got "ssh Permission denied"
The answer is:
setsebool -P httpd_can_network_connect=1
Basically, apache was not authorized to initiate network connections!?!?
I neglected to mention that this is on a CentOS system and as such fell under the watchful caring eye of SELinux, my mistake.
The answer is:
setsebool -P httpd_can_network_connect=1
Basically, apache is not authorized to initiate network connections
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With