Installing “Secure Shell2 (SSH2)” PHP Extension

Installing the libssh2 and Secure Shell2 libraries is a real pain in the ass, but I have to do it time to time, when I’m setting up new environments for the software I work on.

Before we begin, please note this is on a CentOS machine with the yum package manager install.

Note: Before you begin, make sure you have all the proper developer packages installed.

yum install gcc gcc-c++ autoconf automake (c compiler)
yum install php-devel (phpize)

Installing OpenSSL on CentOS 5.2

  • Run the following two yum commands to install OpenSSL & it’s developer’s package. (libssh2 has dependencies on the OpenSSL developer package)
  • yum install openssl
  • yum install openssl-devel

Installing libssh2 libraries on CentOS 5.2

  • Enter the following commands to download and install the latest libssh2 package.
  • wget http://voxel.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.18.tar.gz
  • gunzip libssh2-0.18.tar.gz
  • tar -xvf libssh2-0.18.tar.gz
  • cd libssh2-0.18.tar.gz
  • ./configure && make all install

Compiling libssh2 PHP Extension

  • Use these commands to download and compile the libssh2 PHP module.
  • wget http://pecl.php.net/get/ssh2-0.10.tgz
  • gunzip ssh2-0.10.tgz
  • tar -xvf ssh2-0.10.tgz
  • cd ssh2-0.10.tgz
  • phpize && ./configure –with-ssh2 && make « Generates ssh2.so, the compiled PHP extension.
  • (Note: If you encounter a make: *** [ssh2.lo] Error 1 error, run Bill Pitz’s libssh2 patch.)
  • cp ./modules/ssh2.so /usr/lib/php/modules/ssh2.so
  • Open your PHP configuration file by typing vi /etc/php.ini and add extension=ssh2.so to the configuration, and save the file when you are complete.

Restart Web Server on CentOS 5.2

  • service httpd restart

You should be all gravy, and your web server can now ssh out and perform remote tasks for you!

8 Responses to “Installing “Secure Shell2 (SSH2)” PHP Extension”

  1. chook  on February 2nd, 2009

    thanks for pointing on libssh2 (yes, you are right, installing it is real pain …)

  2. moi1504  on February 12th, 2009

    Hi,

    I have these errors when i try to do ssh2_connect

    Warning: ssh2_connect() [function.ssh2-connect]: Error starting up SSH connection(-5): Unable to exchange encryption keys in /var/www/vhosts/commanderpileje.fr/httpdocs/interface/lib/client/sftp.php on line 161

    Warning: ssh2_connect() [function.ssh2-connect]: Unable to connect to 62.193.246.149 in /var/www/vhosts/commanderpileje.fr/httpdocs/interface/lib/client/sftp.php on line 161

    Any idea ?

    Thanks

  3. Duncan Robertson  on February 17th, 2009

    Nice one mate! Spent most of the day trying to get this to work. After reading your instructions – first time! Cheers!

  4. inurinternet  on February 21st, 2009

    It looks like the ssh2 module can’t exchange keys with the remote server, how are you authing?

  5. hairreach  on June 20th, 2009

    i’ve been trying for days and have been totally unable to get the ssh2 extension installed. been trying the php secure communications library instead and that seems to work. maybe it’ll help some of you:

    http://phpseclib.sourceforge.net/

  6. Balan.Mahadevan  on July 6th, 2009

    Life saver…spent a lot of time to fix this issue.
    Thanks.

  7. baseddoes  on December 20th, 2009

    I spent days trying to get libssh2 installed and when I succeeded I found that it doesn’t even work half the time, anyway. ssh2_exec() returns prematurely very frequently and not even calling socket_set_blocking() helps. I then turned to phpSecLib and it works like a charm:

    http://phpseclib.sourceforge.net/

    Maybe it’ll help you as well.

  8. inurinternet  on January 6th, 2010

    Interesting, seems like that could have saved me a lot of headaches, I will probably use that library in the future with other projects.

    Thanks!


Leave a Reply