The SSH2 extension provides functions for accessing remote machines using the secure SSH and SFTP protocols.
Note: The SSH2 extension's support for PHP 7.x and 8.x is currently in beta.
To install this extension on PHP 7+, SSH in as root and run the following commands on your server:
sudo apt-get -y install gcc make autoconf libc-dev pkg-config sudo apt-get -y install libssh2-1-dev yes '' | sudo peclX.Y-sp install ssh2-beta
Once installed, create a configuration file for the extension and restart PHP.
sudo bash -c "echo extension=ssh2.so > /etc/phpX.Y-sp/conf.d/ssh2.ini" sudo service phpX.Y-fpm-sp restart
To install this extension on PHP 5.6, SSH in as root and run the following on your server:
sudo apt-get install gcc make autoconf libc-dev pkg-config sudo apt-get install libssh2-1-dev sudo pecl5.6-sp install ssh2
Once installed, create a configuration file for the extension and restart PHP.
sudo bash -c "echo extension=ssh2.so > /etc/php5.6-sp/conf.d/ssh2.ini" sudo service php5.6-fpm-sp restart
You can verify the SSH2 extension is installed with the command:
phpX.Y-sp -i | grep ssh2
If SSH2 is installed correctly, you will see the following:
/etc/phpX.Y-sp/conf.d/ssh2.ini, Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, zip, phar, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp ssh2 SSH2 support => enabled libssh2 version => 1.4.3 banner => SSH-2.0-libssh2_1.4.3
To uninstall this extension, as root run the commands:
sudo rm /etc/phpX.Y-sp/conf.d/ssh2.ini sudo peclX.Y-sp uninstall ssh2
Next, restart PHP-FPM with the command:
sudo service phpX.Y-fpm-sp restart