How to Install and Use the PHP SSH2 Extension for Secure Remote Operations

This guide walks you through installing libssh2 and the PHP SSH2 extension, configuring php.ini, verifying the installation, and using the extension to authenticate with passwords or SSH keys, execute remote commands, transfer files, and handle errors in PHP applications.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Install and Use the PHP SSH2 Extension for Secure Remote Operations

Installation

libssh2 installation

libssh2 is an open‑source C library that implements the SSH client protocol, providing APIs for remote command execution, file transfer, and port forwarding.
wget https://libssh2.org/download/libssh2-1.11.0.tar.gz

tar -zxvf libssh2-1.11.0.tar.gz

cd libssh2-1.11.0/

./configure
make
sudo make install

PHP‑SSH2 installation

Official package page: https://pecl.php.net/package/ssh2

wget https://pecl.php.net/get/ssh2-1.4.tgz

tar -zxvf ssh2-1.4.tgz

cd ssh2-1.4/

/usr/local/php-8.2.14/bin/phpize

./configure --with-php-config=/usr/local/php-8.2.14/bin/php-config

make
make install

Add the extension to php.ini:

extension=ssh2

Verify the installation:

/usr/local/php-8.2.14/bin/php -m|grep ssh2
ssh2

Usage

Connecting to a remote server is the first step when using the PHP SSH2 extension. You must provide the server address, port, username, and password (or an SSH key).

Username and password authentication

$connection = ssh2_connect('tinywan.com', 22);
$res = ssh2_auth_password($connection, "username", "password");
if ($res) {
    echo "Authentication Successful! ";
} else {
    echo "Authentication Failed! ";
    exit(255);
}

SSH key authentication

$connection = ssh2_connect('192.168.1.204', 22, ['hostkey' => 'ssh-rsa']);
$res = ssh2_auth_pubkey_file($connection, 'tinywan', '/home/tinywan/.ssh/id_rsa.pub', '/home/tinywan/.ssh/id_rsa');
if ($res) {
    echo "Public Key Authentication Successful
";
} else {
    echo "Public Key Authentication Failed";
}

Execute a remote command (e.g., ls -l) and capture its output:

$connection = ssh2_connect('tinywan.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'ls -l');
stream_set_blocking($stream, true);
echo stream_get_contents($stream);

Upload and download files with ssh2_scp_send and ssh2_scp_recv:

$connection = ssh2_connect('tinywan.com', 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local_file', '/remote_file');
ssh2_scp_recv($connection, '/remote_file', '/local_file');

Handle errors during connection, authentication, or file operations:

$connection = ssh2_connect('tinywan.com', 22);
if (!$connection) {
    die('Connection failed.');
}
$auth = ssh2_auth_password($connection, 'username', 'password');
if (!$auth) {
    die('Authentication failed.');
}

The PHP SSH2 extension provides a straightforward way to perform secure remote operations—such as command execution and file transfer—from PHP scripts, making it valuable for server management and deployment workflows, provided that proper error handling is implemented.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPfile transferremote executionSSH2
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.