A general overview of SSH2 for PHP can be found at the official Secure Shell2 reference. If you look at the requirements section, you will see that two other libraries are required for it to work. They are OpenSSL and libssh2. I explain here how to install both of these first. And then we proceed to actually install ssh2 for PHP shell connections.
Run any of the following commands to determine if OpenSSL is already installed on your machine:
dpkg -l | grep -i openssl
rpm -qa | grep -i openssl
If the output includes a line like this (version numbers might differ), you already have OpenSSL installed. You may proceed to Installing libssh2.
ii openssl 1.0.1-4ubuntu5.9 Secure Socket Layer (SSL) binary and related cryptographic tools
Depending on your screen width, you may have to scroll to see the full text of the above line.
(It was copied directly from the terminal with no formatting changes).
If OpenSSL is not installed, use any of the following commands to install it:
Debian based machine
apt-get install openssl
RedHat based machine
yum install openssl
Create a temporary directory to download the source files to and then cd to the newly created temporary directory.
The whole process is completed with the following commands:
wget http://sourceforge.net/projects/libssh2/files/old-libssh2-releases/1.1/libssh2-1.1.tar.gz tar -zxvf libssh2-1.1.tar.gz cd libssh2-1.1 ./configure make make install
Once these steps have been successfully completed delete the previously created directory.
Download the ssh2 package (check the latest version here):
Create a build directory and untar the package:
mkdir build-dir mv ssh2-0.12.tgz build-dir cd build-dir tar xzvf ssh2-0.12.tgz
Prepare the compile step and compile:
phpize ./configure --with-ssh2 make make install
Copy the module in the system (For 32 bit machine):
cp modules/ssh2.so /usr/lib/php/modules/ssh2.so
Copy the module in the system (For 64 bit machine):
cp modules/ssh2.so /usr/lib64/php/modules/ssh2.so
Confirm that this file
includes this line:
You may now delete the build directory:
rm -f build-dir
(Debian based machine):
(RedHat based machine):
Now make sure that ssh2.so is really loaded by executing the following command:
php -i | grep ssh2
You should see something like this:
/etc/php5/cli/conf.d/ssh2.ini, Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp ssh2 libssh2 version => 1.2.6 banner => SSH-2.0-libssh2_1.2.6
I found a quicker method for installing ssh2. After installing (or confirming that you already have) openssl, just run the following commands to get ssh2 up and running:
sudo apt-get install libssh2-1-dev libssh2-php
sudo php5enmod ssh2
sudo service apache2 restart
I have tested this on Ubuntu 14.04 LTS.