How to Use PHP to Connect and Run Commands Over SSH - Codewolfy

One of the most common needs for developers who manage many test and production environments is to connect to a remote server securely. Fortunately, PHP makes it easy for you to connect SSH using PHP and execute commands directly from your web application. This approach is particularly helpful when you want to automate deployments or check the health of servers or transfer files securely without logging in each time.

By leveraging the SSH2 extension, PHP allows developers to interact with remote systems just like they would right from a terminal. Whether you manage Linux servers, handle cron jobs, or monitor server performance, learning how to use PHP to connect and run commands over SSH makes your workflow faster and more efficient.

Use Cases of Connecting SSH Using PHP

Here are a few practical use cases where PHP SSH connections come in handy.

  • Automating deployment processes on remote servers.
  • Managing backups or log files across multiple servers.
  • Running server health checks or maintenance scripts.
  • Securely sync or transfer files in PHP.
  • Building administration panels or dashboards that interact with servers.

SSH Connection in PHP Example

To get started, ensure the SSH2 extension is installed and enabled in your PHP setup. Once done, you can use a simple script to connect to a remote server and execute commands. Also make sure you have working credentials like username, password.

<?php
$connection = ssh2_connect('your-server-ip', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'ls -l');
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
echo $output;
?>

The above example will connects to a remote server over SSH, authenticates with a username and password, and runs a basic command to list files. You can replace ls -l with any command you want to execute.

This approach is ideal for developers managing cloud instances, VPS servers, or Linux-based applications. Sometimes you need to enable SSH on your server side before making any requests.

SSH Connection Using Public and Private Key Authentication

When working with sensitive servers or automated systems, using password-based SSH authentication is not always the safest choice. More secure and scalable method is to connect SSH using PHP through public and private key authentication. Itenhances security while allowing you to automate without exposing passwords in your scripts.

<?php
$connection = ssh2_connect('your-server-ip', 22);

$auth = ssh2_auth_pubkey_file(
    $connection,
    'username',
    '/path/to/id_rsa.pub',
    '/path/to/id_rsa',
    'optional-passphrase'
);

if ($auth) {
    $stream = ssh2_exec($connection, 'uptime');
    stream_set_blocking($stream, true);
    echo stream_get_contents($stream);
} else {
    echo 'Authentication failed';
}
?>

Above example will establish connection with SSH server using encryption key based authentication with PHP script.

Execute Multiple Commands Over SSH In PHP

With this approach you can even execute multiple commands on server using PHP script and SSH. Let’s take another example for that.

<?php
$connection = ssh2_connect('your-server-ip', 22);
ssh2_auth_password($connection, 'username', 'password');

$commands = [
    'cd /var/www/html',
    'git pull origin main',
    'php artisan cache:clear'
];

foreach ($commands as $command) {
    $stream = ssh2_exec($connection, $command);
    stream_set_blocking($stream, true);
    echo stream_get_contents($stream);
}
?>

This type of approach allows you to deploy your app using Git through your web porter. You can even customize this with forms to select which project should be deployed and much more.

Conclusion

Using PHP to connect SSH and run commands can simplify many server management tasks. It saves time, improves security, and allows you to automate common operations. In general, from building a web dashboard to deploying updates, and with multiple environments, having SSH connection experience in PHP will give you better control over your infrastructure.