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.
Installing OpenSSL
Run any of the following commands to determine if OpenSSL is already installed on your machine:
Debian
1 | dpkg -l | grep -i openssl |
RPM
1 | 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.
1 | 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
1 | apt-get install openssl |
RedHat based machine
1 | yum install openssl |
Installing libssh2
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.
Install SSH2
Download the ssh2 package (check the latest version here):
1 | wget http://pecl.php.net/get/ssh2-0.12.tgz |
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):
1 | cp modules/ssh2.so /usr/lib/php/modules/ssh2.so |
Copy the module in the system (For 64 bit machine):
1 | cp modules/ssh2.so /usr/lib64/php/modules/ssh2.so |
Confirm that this file
/etc/php.d/ssh2.ini
includes this line:
1 | extension=ssh2.so |
You may now delete the build directory:
1 2 | cd ../../ rm -f build-dir |
Restart Apache
(Debian based machine):
1 | /etc/init.d/apache2 restart |
Restart Apache
(RedHat based machine):
1 | /etc/init.d/httpd restart |
Now make sure that ssh2.so is really loaded by executing the following command:
1 | 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
*UPDATE:
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:
1 2 3 | 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.
Meh – I prefer phpseclib, a pure PHP SSH implementation:
http://phpseclib.sourceforge.net/
It offers a number of advantages over the libssh2 solution you’re proposing:
http://stackoverflow.com/a/14305875/569976
Also, although some apps might have built-in libssh2 support, there may well be phpseclib plugins for those apps. eg.
http://wordpress.org/plugins/ssh-sftp-updater-support/
This is useful information clakoman.
The plugin seems like a nice hassle free method.
I’ll definitely have a look. Thanks.