Secure File Transfers to the remote server with PHP SSH2 Extension: Usage and Installation ‎on RHEL 8

Sami Uddin
3 min readOct 24, 2023

Secure Copy Protocol (SCP) is a network protocol that allows you to securely transfer files between a local host and a remote host. In PHP, you can use the ssh2_scp_send() function to transfer files using SCP. This function takes four parameters:

$connection: The SSH connection resource.
$srcFile: The path to the local file to transfer.
$dstFile: The path to the remote file to transfer.
$mode: The permissions to set on the remote file.

Here is an example of how to use ssh2_scp_send() to transfer a file:

$host = 'example.com';
$username = 'your_user_name';
$password = 'strongPassword';
$srcFile = 'path/to/relative/directory/filename';
$dstFile = 'path/to/destination/directory/filename';
$mode = 0775; // The permissions on the remote file are set to 0775, which means that the owner of the file can read, write, and execute the file, and the group and other users can read and execute the file

try {
// Create connection the the remote host
$connection = ssh2_connect($host, 22);
ssh2_auth_password($connection, $username, $password);
ssh2_scp_send($connection, $srcFile, $dstFile, $mode);

echo "Transfer Done";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}

This code will first create an SSH connection to the remote host using the ssh2_connect() function. Then, it will authenticate with the remote host using the ssh2_auth_password() function. Finally, it will transfer the file using the ssh2_scp_send() function.

SSH2 extension is a powerful tool to communicate with remote servers using SSH2 protocol. However it does not come by default on the system and requires installation. This post will guide you though the step by step process.

The Prerequisites:

You have to make sure you got the following prerequisites installed-

  • Make
  • GCC compiler
  • PHP development libraries
  • PHP PEAR

To install these prerequisites, you can use your system’s package manage dnf or yum using the following command:

yum install make gcc php-devel php-pear

Installing libssh2:

The SSH2 extension relies on the libssh2 library. To install libssh2 manually, follow these steps:

First download the libssh2 source code from https://libssh2.org/download/libssh2-1.10.0.tar.gz

Extract the downloaded archive using the following command:

tar -zxvf libssh2-1.10.0.tar.gz

Change into the extracted directory:

cd libssh2-1.10.0

Configure the build using the following command:

./configure

Build the libssh2 library:

make

Install the libssh2 library:

make install

Installing the SSH2 extension:

With libssh2 installed, proceed to install the SSH2 extension:

Download the SSH2 extension source code from https://pecl.php.net/get/ssh2-1.3.1.tgz into an appropriate folder.

Extract the downloaded archive using the following command:

tar -zxvf ssh2-1.3.1.tgz

Change into the extracted directory:

cd ssh2-1.3.1

Run the phpize command to prepare the build environment:

phpize

Configure the build using the following command:

./configure

Build the SSH2 extension:

make

Run the extension’s test suite to ensure it is functioning correctly:

make test

Install the SSH2 extension:

make install

Enabling the SSH2 extension:

Open the php.ini configuration file, typically located in /etc/php.ini. In order to edit in terminal use the following command:

nano /etc/php.ini

Nano is a simple yet powerful command line-based text editor.

Add the following line to the end of the file to enable the SSH2 extension:

extension=ssh2.so

Save the php.ini file.

Restarting PHP:

Finally, restart your PHP service to load the newly installed extension. The specific command for restarting PHP may vary depending on your system’s configuration. For example, on systems using systemd, you can use the following command:

systemctl restart php-fpm

After restarting PHP, the SSH2 extension should be successfully installed and ready for use. You can follow the above PHP script to check for any errors.

Thanks for reading! This is my first post!

--

--