Ssh2 connect php install

How To Install php-ssh2 on Ubuntu 20.04

In this tutorial we learn how to install php-ssh2 on Ubuntu 20.04.

What is php-ssh2

Provides PHP bindings to the functions of libssh2 which implements the SSH2 protocol.

WARNING: The PHP 7.0 is an unreleased git snapshot of current ssh2 that implements support for PHP 7.0. USE WITH CAUTION! Description-md5: 8adc0199b55f9ddf168edba900604566

There are three ways to install php-ssh2 on Ubuntu 20.04. We can use apt-get , apt and aptitude . In the following sections we will describe each method. You can choose one of them.

Install php-ssh2 Using apt-get

Update apt database with apt-get using the following command.

After updating apt database, We can install php-ssh2 using apt-get by running the following command:

sudo apt-get -y install php-ssh2 

Install php-ssh2 Using apt

Update apt database with apt using the following command.

After updating apt database, We can install php-ssh2 using apt by running the following command:

sudo apt -y install php-ssh2 

Install php-ssh2 Using aptitude

If you want to follow this method, you might need to install aptitude first since aptitude is usually not installed by default on Ubuntu. Update apt database with aptitude using the following command.

After updating apt database, We can install php-ssh2 using aptitude by running the following command:

sudo aptitude -y install php-ssh2 

How To Uninstall php-ssh2 on Ubuntu 20.04

To uninstall only the php-ssh2 package we can use the following command:

sudo apt-get remove php-ssh2 

Uninstall php-ssh2 And Its Dependencies

To uninstall php-ssh2 and its dependencies that are no longer needed by Ubuntu 20.04, we can use the command below:

sudo apt-get -y autoremove php-ssh2 

Remove php-ssh2 Configurations and Data

To remove php-ssh2 configuration and data from Ubuntu 20.04 we can use the following command:

sudo apt-get -y purge php-ssh2 

Remove php-ssh2 configuration, data, and all of its dependencies

We can use the following command to remove php-ssh2 configurations, data and all of its dependencies, we can use the following command:

sudo apt-get -y autoremove --purge php-ssh2 

References

Summary

In this tutorial we learn how to install php-ssh2 package on Ubuntu 20.04 using different package management tools: apt, apt-get and aptitude.

Читайте также:  Register page using php

Источник

How to Install the PHP SSH2 Extension

In the examples shown, replace «X.Y» with your app’s PHP version (for example, «7.4» or «8.2»). To install a PECL extension for multiple PHP versions, repeat the PECL extension installation for each PHP version.

The SSH2 extension provides functions for accessing remote machines using the secure SSH and SFTP protocols.

Installing the SSH2 Extension on PHP 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, or 8.2

Note: The SSH2 extension’s support for PHP 7.x and 8.x is currently in beta.

To install this extension on PHP 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, or 8.2 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 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

Installing the SSH2 Extension on PHP 5

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

Verifying the Installation

You can verify the SSH2 extension is installed with the command:

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

Uninstalling the SSH2 Extension

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

Источник

Ssh2 connect php install

Installation For Centos 5.11 x86_64

rpm -K rpmforge-release-0.5.3-1.el5.rf.*.rpm

rpm -i rpmforge-release-0.5.3-1.el5.rf.*.rpm

2. yum install gcc php-devel php-pear libssh2 libssh2-devel
3. pecl install -f ssh2
4. Enable extension from php.ini configuration

Steps for installing the extension package on Debian systems:

> sudo apt-get install libssh2-php
> sudo /etc/init.d/apache2 restart

The SSH2 binary for Windows (php_ssh2.dll) can be found here: http://pecl.php.net/package/ssh2/1.1.2/windows

[Editor’s note: unless you have special demands, use the appropriate deps package from .]

SSH2 for Windows is not available from the authors of the extension. But compiled binaries are available from the Apache Lounge (which is recommended by the php.net website too).

This binaries contain a lot (maybe even all?) for Windows compiled PHP extensions, like the SSH2 extension.

Please note that it is also highly recommended to use their Windows binary builds for the Apache HTTP server from here: https://www.apachelounge.com/download/

Читайте также:  Java arraylist сложность алгоритма

Older versions can be choosen from the left menu (VC11, VC10, etc.) if required.

php.net also recommends to use the binary builds from the Apache Lounge.

Maybe this helps developers, who work with Windows and can’t find or compile some extensions themself.

Steps for installing on CentOS Linux 7

After having a full webserver running, install packages libssh2.x86_64 and php-pecl-ssh2.x86_64

1. yum install libssh2.x86_64 php-pecl-ssh2.x86_64
2. systemctl restart httpd.service

on linux debian like (ubuntu) a simple install will also load ‘libssh2-1’
$ sudo apt-get update && sudo apt-get install php-ssh2

$ sudo apache2ctl graceful
should make it usable

use phpinfo() function, some sections are now showing additional «ssh2» parts:
— Intro section with new items for «ssh2»
— — Additional .ini files parsed
— — Registered PHP Streams
— ssh2 (new section)
— — showing «SSH2 support — enabled»

Using PECL it must be specified the channel, since the beta version has to be choosen instead of stable one:

$ pecl install ssh2 channel://pecl.php.net/ssh2-version

Steps for installing the extension package on Ubuntu 14.04.

Very similar to dreadwestern at gmail dot com recipe, but enable ssh2 in PHP:

> sudo apt-get install libssh2-php
> php5enmod ssh2
> sudo /etc/init.d/apache2 restart

Since version 0.12 (released 2012-10-15) there is a binary version for Windows.

I tested it with PHP 5.5.29 (32-bit, thread-safe) on my Windows 7 machine.

Installing on Ubuntu 14.0.4

sudo pecl channel-update pecl.php.net
sudo apt-get install libssh2-1-dev
sudo pecl install -a ssh2-0.12
echo ‘extension=ssh2.so’ | sudo tee /etc/php5/mods-available/ssh2.ini > /dev/null
sudo php5enmod ssh2

On centos 8 and rockylinux:

Installing this module for use with CentOS 5. or 6.?

1. Install your favorite PHP RPM packages. Make sure that one of your favorite packages is pear, so pecl gets installed in-tow.

2. Find and install *RPMS* of libssh2 of a version >= 1.2, get both the base lib package (libssh2-1.2.*.rpm) and the devel package that includes headers (libssh2-devel-1.2.*.rpm) of the EXACT SAME VERSION. Finding these packages took a few minutes of googling, and I invite an editor that has a trusted source for these to attach URLs as appropriate. (Yes, the version below is ancient)

# rpm -ivh libssh2-1.2.6-1.el5.rf.x86_64.rpm libssh2-devel-1.2.6-1.el5.rf.x86_64.rpm

3. Install the PHP module via pecl
# pecl install ssh2-0.12

4. Make PHP see the newly-installed extension
# echo «extension=ssh2.so» > /etc/php.d/ssh2.ini

To help someone avoid the headaches I did for a week. SSH2 functions have problems with the ssh2-beta & libssh2-0.18 even after patching with said patches on the internet.

ssh_exec will work fine
ssh2_shell will not work

In order to use ssh2_shell I had to roll back to libssh2-0.14 in order to get ssh2_shell function to work properly

Just a little note: when compiling this package after modifying the ssh2.c, do not use pecl build as it will not create an ssh2.so file (although it says it succeeded), use:

Читайте также:  Python событийно ориентированное программирование

you then need to copy the ssh2.so file into your extension dir if it didn’t do so already.

Источник

Creating SSH connections with PHP

php

SSH connections are very popular when you have VPS (Virtual Private Servers), all installations and configurations of our servers can be performed directly from the terminal with a root connection.

With PHP we can make SSH connections to these servers and execute commands from our local application or from another server, or we can schedule Cron Jobs to run a cleaning or download VPS backups to our machine.

Installing libssh2-php

We will install soport for SSH for PHP in Ubuntu 12.04 or later.

[prism:php]sudo apt-get install libssh2-1-dev libssh2-php[/prism:php]

Para verificar que está correctamente instalado:

[prism:php]php m |grep php[/prism:php]

If everything went right, it should return: ssh2

Our first connection

The first thing we should do is to verify the connection wiith ssh to php:

If the function exists, we turn to verify that there can be an SSH connection to the server:

For this example, we will create the connection by username and password, remember that in this way we might have sensitive information available within our code and that is considered a security problem:

php

Our first command

If the connection is successful and we are within our VPS, we can execute our first command:

If everything is correct, our first command ran on the server, but how do we know if it really worked?

To do this, let us first see how we can display VPS messages in our PHP console:

In the above code we use ssh_exec to execute the commands in the VPS terminal, but we can also use shel_exec:

php

Connecting to the VPS with a public key

To make a little more secure our connection to our VPS we can use a public key.

In our computer terminal we run the following commands to create a pair of keys (private / public): [prism:php]cd ~/.ssh ssh-keygen[/prism:php]

We leave all the default values Once created our keys, we add an array as the third parameter to the connection and replace the ssh2_auth_password by the following:

if (ssh2_auth_pubkey_file($connection, ‘root’, ‘home/nombredeusuario/.ssh/id_rsa.pub’, ‘home/nombredeusuario/.ssh/id_rsa’)) < die('No se pudo realizar la conexión con el servidor.') >[/prism:php] With this we can run our VPS connections with SSH keys from PHP.

Conclusion

We can schedule and run many commands through SSH connections to maintain one or more servers from a PHP application located on our local server or another VPS.

Extra: SSH connection libraries

With these commands we can create SSH connections directly from our PHP, but there are already some libraries that ensure or improve these connections:

  • phpseclib: A library created in PHP that requires no additional libraries to create SSH connections.
  • Net_SSH2: PEAR connector.

We recommend you on video

Источник

Оцените статью