How to Install the PHP SSH2 Extension

In this guide, we will show you how to rapidly install SSH2 aka libssh2 PECL PHP Extension for PHP5.x and PHP7.x series for cPanel and Cyberpanel.

Installing SSH2 on PHP 5.x and 7.0, 7.1, 7.2, 7.3 and 7.4(Cyberpanel Only) can be done via the below commands for cPanel Servers. It is a very similar process for Cyberpanel so skip to the bottom to see that process.

This is the all in one command set to do this for cPanel

yum install -y libssh2-devel
for version in $(ls /opt/cpanel|grep ea-php); do /opt/cpanel/${version}/root/usr/bin/pecl channel-update pecl.php.net; done;
printf '\n' |/opt/cpanel/ea-php56/root/usr/bin/pecl install ssh2
for PHP7 in $(ls -h /opt/cpanel |grep ea-php7); do /opt/cpanel/$PHP7/root/usr/bin/pecl install https://pecl.php.net/get/ssh2-1.2.tgz; done

If no errors you will want to restart the HTTP service after modules are installed to activate them.

systemctl restart httpd||service httpd restart 

If you get the below error:

Could not download from "https://pecl.php.net/get/ssh2-1.2.tgz" (File https://pecl.php.net:443/get/ssh2-1.2.tgz not valid (received: HTTP/1.0 500 Internal Server Error
))
Invalid or missing remote package file
install failed

Use this workaround.
https://bugs.php.net/bug.php?id=78560

wget https://pecl.php.net/get/ssh2 -O /tmp/ssh2-1.2.tgz
printf '\n' |/opt/cpanel/ea-php56/root/usr/bin/pecl install ssh2
printf '\n' |/opt/cpanel/ea-php70/root/usr/bin/pecl install /tmp/ssh2-1.2.tgz
printf '\n' |/opt/cpanel/ea-php71/root/usr/bin/pecl install /tmp/ssh2-1.2.tgz
printf '\n' |/opt/cpanel/ea-php72/root/usr/bin/pecl install /tmp/ssh2-1.2.tgz
printf '\n' |/opt/cpanel/ea-php73/root/usr/bin/pecl install /tmp/ssh2-1.2.tgz

This command lets you check to see if all PHP versions show the module is installed.

for phpver in $(ls -1 /opt/cpanel/ |grep ea-php | sed 's/ea-php//g') ; do echo "PHP $phpver" ; /opt/cpanel/ea-php$phpver/root/usr/bin/php -m |grep -iE 'ssh2'; echo ""; done

This is a breakdown of what each line does for the curious.

The first command updates pecl for all PHP versions installed.

for version in $(ls /opt/cpanel|grep ea-php); do /opt/cpanel/${version}/root/usr/bin/pecl channel-update pecl.php.net; done;

The second command installs ssh2 for PHP 5.6 which is the latest version that supports the PHP 5.6 series.

printf '\n' |/opt/cpanel/ea-php56/root/usr/bin/pecl install ssh2

The third command installs ssh2 latest version that supports the PHP 7.x series.

for PHP7 in $(ls -h /opt/cpanel |grep ea-php7); do /opt/cpanel/$PHP7/root/usr/bin/pecl install https://pecl.php.net/get/ssh2-1.2.tgz; done

This is the Guide for Cyberpanel

Here is how we can install the PECL extension for all PHP 5.x and 7.x versions for Cyberpanel lsphp.

yum install -y libssh2-devel
for PHP5 in $(ls /usr/local/lsws|grep lsphp5); do printf '\n' |/usr/local/lsws/$PHP5/bin/pecl install ssh2 && echo "extension=ssh2.so" >> /usr/local/lsws/$PHP5/etc/php.d/zzzzzzz-pecl.ini; done;
for PHP7 in $(ls /usr/local/lsws|grep lsphp7); do printf '\n' |/usr/local/lsws/$PHP7/bin/pecl install https://pecl.php.net/get/ssh2-1.2.tgz && echo "extension=ssh2.so" >> /usr/local/lsws/$PHP7/etc/php.d/zzzzzzz-pecl.ini; done;
systemctl restart lsws||service lsws restart; systemctl status lsws||service lsws status;

Please note the beauty of the above command is that it automatically adds the PHP extension to the ini file for each version for to its “zzzzzzz-pecl.ini”. The warning about the “You should add “extension=oauth.so” to php.ini” can be ignored. As this script handles it for you. If you do it manually without this script then that would be applicable.

Pecl update all Versions

for version in $(ls /usr/local/lsws|grep lsphp); do /usr/local/lsws/${version}/bin/pecl channel-update pecl.php.net; done
[root@server ~]# for version in $(ls /usr/local/lsws|grep lsphp); do /usr/local/lsws/${version}/bin/pecl channel-update pecl.php.net; done
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
Updating channel "pecl.php.net"
Channel "pecl.php.net" is up to date
Updating channel "pecl.php.net"
Channel "pecl.php.net" is up to date
Updating channel "pecl.php.net"
Channel "pecl.php.net" is up to date
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
[root@server ~]#

This command lets you check to see if all PHP versions show the module is installed.

for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp | sed 's/lsphp//g') ; do echo ""; echo "LSPHP $phpver" ; /usr/local/lsws/lsphp$phpver/bin/php -m | grep -i ssh2; done
[root@server:~]# for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp | sed 's/lsphp//g') ; do echo ""; echo "LSPHP $phpver" ; /usr/local/lsws/lsphp$phpver/bin/php -m | grep -i ssh2; done

LSPHP 53
ssh2

LSPHP 54
ssh2

LSPHP 55
ssh2

LSPHP 56
ssh2

LSPHP 70
ssh2

LSPHP 71
ssh2

LSPHP 72
ssh2

LSPHP 73
ssh2

LSPHP 74
ssh2
[root@server:~]#

Some screenshots of this on the Cyberpanel Server installation process.