How to Install the PHP SSH2 Extension for cPanel/CyberPanel

This tutorial shows how to install ssh2 for lsphp for Litespeed/Openlitespeed servers for control panels like Cyberpanel and cPanel.

Installation via pecl is easiest so first step is ensuring pecl is updated.


Installation for CyberPanel based servers

Update pecl

for version in $(ls /usr/local/lsws|grep lsphp); do /usr/local/lsws/${version}/bin/pecl channel-update pecl.php.net; done

Example:

[root@testalmalinux ~]# 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"
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"
Channel "pecl.php.net" is up to date
Updating channel "pecl.php.net"
Channel "pecl.php.net" is up to date
[root@testalmalinux ~]#

Install via pecl for Centos/RHEL/Almalinux and derivatives

yum install -y libssh2-devel;
Extension='ssh2';specific_channel_url='channel://pecl.php.net/ssh2-1.3.1';
for version in $(ls /usr/local/lsws|grep lsphp5); do php_ini="/usr/local/lsws/${version}/etc/php.d/zzzzzzz-pecl.ini"; printf '\n' |/usr/local/lsws/${version}/bin/pecl install ${Extension} && echo "extension=${Extension}.so" >> ${php_ini}; done; 
for version in $(ls /usr/local/lsws|grep lsphp7); do php_ini="/usr/local/lsws/${version}/etc/php.d/zzzzzzz-pecl.ini"; printf '\n' |/usr/local/lsws/${version}/bin/pecl install ${specific_channel_url} && echo "extension=${Extension}.so" >> ${php_ini}; done; 
for version in $(ls /usr/local/lsws|grep lsphp8); do php_ini="/usr/local/lsws/${version}/etc/php.d/zzzzzzz-pecl.ini"; printf '\n' |/usr/local/lsws/${version}/bin/pecl install ${specific_channel_url} && echo "extension=${Extension}.so" >> ${php_ini}; done; 
killall lsphp ; systemctl restart lsws||service lsws restart; systemctl status lsws||service lsws status;
Example of installation output from lsphp80

Install via pecl Ubuntu

sudo apt-get install -y php-ssh2;
Extension='ssh2';specific_channel_url='channel://pecl.php.net/ssh2-1.3.1';
for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp5 | sed 's/lsphp//g'); do php_ini="/usr/local/lsws/lsphp${phpver}/etc/php/$(echo $phpver | sed 's/^(.{1})/\1./')/litespeed/php.ini"; printf '\n' |/usr/local/lsws/lsphp${phpver}/bin/pecl install ${Extension} && echo "extension=${Extension}.so" >> ${php_ini}; done; 
for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp7 | sed 's/lsphp//g'); do php_ini="/usr/local/lsws/lsphp${phpver}/etc/php/$(echo $phpver | sed 's/^(.{1})/\1./')/litespeed/php.ini"; printf '\n' |/usr/local/lsws/lsphp${phpver}/bin/pecl install ${specific_channel_url} && echo "extension=${Extension}.so" >> ${php_ini}; done; 
for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp8 | sed 's/lsphp//g'); do php_ini="/usr/local/lsws/lsphp${phpver}/etc/php/$(echo $phpver | sed 's/^(.{1})/\1./')/litespeed/php.ini"; printf '\n' |/usr/local/lsws/lsphp${phpver}/bin/pecl install ${specific_channel_url} && echo "extension=${Extension}.so" >> ${php_ini}; done; 
killall lsphp ; systemctl restart lsws||service lsws restart; systemctl status lsws||service lsws status;

Check if its installed

Extension='ssh2'; 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 ${Extension}; done

Example if NOT installed:

[root@testalmalinux ~]# Extension='ssh2'; 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 ${Extension}; done

LSPHP 71

LSPHP 72

LSPHP 73

LSPHP 74

LSPHP 80
[root@testalmalinux ~]#

Example of successful installation:

[root@testalmalinux ~]# Extension='ssh2'; 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 ${Extension}; done

LSPHP 71
ssh2

LSPHP 72
ssh2

LSPHP 73
ssh2

LSPHP 74
ssh2

LSPHP 80
ssh2
[root@testalmalinux ~]#

Example of successful installation for all lsphp versions on cyberpanel alamalinux

How to uninstall for Centos CyberPanel

Extension="ssh2"; for version in $(ls /usr/local/lsws|grep lsphp); do php_ini="/usr/local/lsws/${version}/etc/php.d/zzzzzzz-pecl.ini"; printf '\n' |/usr/local/lsws/${version}/bin/pecl uninstall ${Extension} && sed -i "/${Extension}.so/d" ${php_ini}; done; killall lsphp ; systemctl restart lsws||service lsws restart; systemctl status lsws||service lsws status;

How to uninstall for Ubuntu CyberPanel

Extension="ssh2"; for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp | sed 's/lsphp//g'); do php_ini="/usr/local/lsws/lsphp${phpver}/etc/php/$(echo $phpver | sed 's/^(.{1})/\1./')/litespeed/php.ini"; printf '\n' |/usr/local/lsws/lsphp${phpver}/bin/pecl uninstall ${Extension} && sed -i "/${Extension}.so/d" ${php_ini}; done; 

Installation for cPanel servers

yum install -y libssh2-devel;
Extension='ssh2'; specific_channel_url='channel://pecl.php.net/ssh2-1.3.1';
for version in $(ls /opt/cpanel|grep ea-php); do /opt/cpanel/${version}/root/usr/bin/pecl channel-update pecl.php.net; done;
for PHP5 in $(ls -h /opt/cpanel |grep ea-php5); do printf '\n' |/opt/cpanel/$PHP7/root/usr/bin/pecl install ${Extension}; done;
for PHP7 in $(ls -h /opt/cpanel |grep ea-php7); do printf '\n' |/opt/cpanel/$PHP7/root/usr/bin/pecl install ${specific_channel_url}; done;
for PHP8 in $(ls -h /opt/cpanel |grep ea-php8); do printf '\n' |/opt/cpanel/$PHP8/root/usr/bin/pecl install ${specific_channel_url}; done;
systemctl restart httpd||service httpd restart;
Example of installation output from ea-php80 on cPanel

Check if installed:

Extension='ssh2';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 "${Extension}"; echo ""; done

Example of successful installation:

[root@dev ~]# Extension='ssh2';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 "${Extension}"; echo ""; done
PHP 70
ssh2

PHP 71
ssh2

PHP 72
ssh2

PHP 73
ssh2

PHP 74
ssh2

PHP 80
ssh2

[root@dev ~]#

To uninstall use the below

Extension='ssh2';for version in $(ls /opt/cpanel|grep ea-php); do /opt/cpanel/${version}/root/usr/bin/pecl uninstall ${Extension}; done; systemctl restart httpd||service httpd restart;

Other helpful resources:
https://hackerdise.com/how-to-install-the-php-ssh2-extension/

Hopefully this is helpful for anyone else looking to setup libssh2 php-ssh2 on their own server.

Leave a Comment

Item added to cart.
0 items - $0.00