Installing SSH2 for PHP to run on a Xampp Apache server on a OSX 10.9.1

Mona Paun picture Mona Paun · Dec 23, 2013 · Viewed 9.4k times · Source

I am running Xampp on a Mac with OS X 10.9.1 and I am trying to develop a web page that calls a PHP script through an Ajax call. Xampp is used here for development purposes and it is not intended to form the backend of an online page.

A PHP script needs to connect though ssh to a remote machine, reason for which I
plan to use the ssh2 library. When I try to run the commands:

$connection = ssh2_connect('my.remote.machine.address', 5432);

I get the following error:

Fatal error: Call to undefined function ssh2_connect() in /Applications/XAMPP/xamppfiles/htdocs/Project/getdata.php on line 8

and, by calling 'phpinfo()', ssh2 does not seem to be installed. I successfully managed to use ssh2_connect on Windows, Ubuntu and Scientific Linux and after a week I can't seem to figure out what I'm not doing right on a OSX I suspect there could be a problem with broken paths or interaction with the Apache server OSX came with. Here is what I have tried:

I. The MacPorts and PECL method (reference: http://lassebunk.dk/2011/05/26/installing-ssh2-for-php-on-mac-os-x/):

  • stuff already on my computer: xcode (from iStore), MacPorts (installer from their site), PHP 5.4.17 and apparently an Apache server that comes with OSX, that I had to stop with:

sudo apachectl stop

so that the Xampp one worked.

  • ran the command:

sudo port install libssh2

as the libssh2 and openssl libraries are prerequisites to using ssh2_connect in php (reference: http://www.php.net/manual/en/ssh2.requirements.php); openssl appears to be already installed on Xampp's according to phpinfo().

  • installed Pear:

sudo php /usr/lib/php/install-pear-nozlib.phar

  • installed Homebrew (otherwise the 'pecl install' commands fail):

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

  • ran:

brew install autoconf

  • now I was finally able to run:

$ sudo pecl install ssh2-0.12

with "/opt/local" as prefix

  • now I got a nice ssh2.so file, which I add to the relevant php.ini:

/Applications/XAMPP/xamppfiles/etc

by adding the line:

extension=/opt/local/lib/php54/extensions/no-debug-non-zts-20100525/ssh2.so

  • restarted the Xampp Apache Web Server from the GUI that comes with Xampp and still ssh2 was not found.

II. The compile the ssh2.so from the terminal method (reference: zizifu's comment http://www.php.net/manual/en/ssh2.installation.php):

./configure

make

make install

phpize

./configure --with-ssh2

make

make install

  • got a ssh2.so file again, added it to the relevant php.ini, restarted server and it still doesn't work.

Sorry if this has too much detail, hopefully the information I gave can point what I'm doing wrong; I would really appreciate any solution/hint/thing to try to solve this issue.

Answer

neubert picture neubert · Dec 23, 2013

Have you considered phpseclib, a pure PHP SSH2 implementation? It has a number of advantages over libssh2, including improved portability and speed. Example:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>