Install the PHP SSH2 extension
The SSH2 extension provides functions for accessing remote machines using the secure SSH and SFTP protocols.
The SSH2 extension supports PHP 5.6+.
Install the SSH2 extension
Installing the extension involves the following steps:
- Install build dependencies (packages that are required to build the extension).
- Build the extension’s library file.
- Enable the extension in the PHP configuration.
Install build dependencies
Set environment variables for the package manager.
export DEBIAN_FRONTEND=noninteractive
Install a compiler and other packages that are required for building PHP extensions.
sudo apt-get -y install gcc g++ make autoconf libc-dev pkg-config
Install additional packages that are required to build the SSH2 extension.
sudo apt-get -y install libssh2-1-dev
Build the extension
The PECL install
command compiles the extension’s .so
library file.
yes '' | sudo pecl8.4-sp install ssh2
yes '' | sudo pecl8.3-sp install ssh2
yes '' | sudo pecl8.2-sp install ssh2
yes '' | sudo pecl8.1-sp install ssh2
yes '' | sudo pecl8.0-sp install ssh2
yes '' | sudo pecl7.4-sp install ssh2
yes '' | sudo pecl7.3-sp install ssh2
yes '' | sudo pecl7.2-sp install ssh2
yes '' | sudo pecl7.1-sp install ssh2
yes '' | sudo pecl7.0-sp install ssh2
yes '' | sudo pecl5.6-sp install ssh2-0.13
Enable the extension
Configure PHP to load the extension.
sudo bash -c "echo extension=ssh2.so > /etc/php8.4-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php8.3-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php8.2-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php8.1-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php8.0-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php7.4-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php7.3-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php7.2-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php7.1-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php7.0-sp/conf.d/ssh2.ini"
sudo bash -c "echo extension=ssh2.so > /etc/php5.6-sp/conf.d/ssh2.ini"
Restart PHP-FPM.
sudo service php8.4-fpm-sp restart
sudo service php8.3-fpm-sp restart
sudo service php8.2-fpm-sp restart
sudo service php8.1-fpm-sp restart
sudo service php8.0-fpm-sp restart
sudo service php7.4-fpm-sp restart
sudo service php7.3-fpm-sp restart
sudo service php7.2-fpm-sp restart
sudo service php7.1-fpm-sp restart
sudo service php7.0-fpm-sp restart
sudo service php5.6-fpm-sp restart
Verify the extension is enabled in PHP.
php8.4-sp -i | grep ssh2
php8.3-sp -i | grep ssh2
php8.2-sp -i | grep ssh2
php8.1-sp -i | grep ssh2
php8.0-sp -i | grep ssh2
php7.4-sp -i | grep ssh2
php7.3-sp -i | grep ssh2
php7.2-sp -i | grep ssh2
php7.1-sp -i | grep ssh2
php7.0-sp -i | grep ssh2
php5.6-sp -i | grep ssh2
If the extension is enabled, you will see output like 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.sftpssh2libssh2 version => 1.11.0banner => SSH-2.0-libssh2_1.11.0
Uninstall the SSH2 extension
Uninstalling the extension involves the following steps:
- Disable the extension in the PHP configuration.
- Remove the extension’s library file.
Disable the extension
Remove the extension’s configuration from PHP.
sudo rm /etc/php8.4-sp/conf.d/ssh2.ini
sudo rm /etc/php8.3-sp/conf.d/ssh2.ini
sudo rm /etc/php8.2-sp/conf.d/ssh2.ini
sudo rm /etc/php8.1-sp/conf.d/ssh2.ini
sudo rm /etc/php8.0-sp/conf.d/ssh2.ini
sudo rm /etc/php7.4-sp/conf.d/ssh2.ini
sudo rm /etc/php7.3-sp/conf.d/ssh2.ini
sudo rm /etc/php7.2-sp/conf.d/ssh2.ini
sudo rm /etc/php7.1-sp/conf.d/ssh2.ini
sudo rm /etc/php7.0-sp/conf.d/ssh2.ini
sudo rm /etc/php5.6-sp/conf.d/ssh2.ini
Restart PHP-FPM.
sudo service php8.4-fpm-sp restart
sudo service php8.3-fpm-sp restart
sudo service php8.2-fpm-sp restart
sudo service php8.1-fpm-sp restart
sudo service php8.0-fpm-sp restart
sudo service php7.4-fpm-sp restart
sudo service php7.3-fpm-sp restart
sudo service php7.2-fpm-sp restart
sudo service php7.1-fpm-sp restart
sudo service php7.0-fpm-sp restart
sudo service php5.6-fpm-sp restart
Remove the extension
The PECL uninstall
command removes the extension’s .so
library file.
sudo pecl8.4-sp uninstall ssh2
sudo pecl8.3-sp uninstall ssh2
sudo pecl8.2-sp uninstall ssh2
sudo pecl8.1-sp uninstall ssh2
sudo pecl8.0-sp uninstall ssh2
sudo pecl7.4-sp uninstall ssh2
sudo pecl7.3-sp uninstall ssh2
sudo pecl7.2-sp uninstall ssh2
sudo pecl7.1-sp uninstall ssh2
sudo pecl7.0-sp uninstall ssh2
sudo pecl5.6-sp uninstall ssh2