acassan/remoteserver
Laravel/PHP package to connect to a remote server and execute actions or processes. Useful for running commands, triggering tasks, or managing remote operations from your application.
Installation
composer require acassan/remoteserver
Add the service provider to config/app.php under providers:
Acassan\RemoteServer\RemoteServerServiceProvider::class,
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="Acassan\RemoteServer\RemoteServerServiceProvider"
Update config/remoteserver.php with your remote server details (host, port, credentials, etc.).
First Use Case: Connecting to a Remote Server
Inject the RemoteServer facade or service into your controller/service:
use Acassan\RemoteServer\Facades\RemoteServer;
public function connectToRemote()
{
$connection = RemoteServer::connect();
return $connection->isConnected() ? 'Connected!' : 'Failed.';
}
Running a Simple Command
$result = RemoteServer::execute('ls -la');
dd($result); // Outputs the command result
// Execute a command and get output
$output = RemoteServer::execute('php artisan migrate');
// Execute with error handling
try {
$output = RemoteServer::execute('npm install');
} catch (\Acassan\RemoteServer\Exceptions\RemoteServerException $e) {
Log::error("Remote execution failed: " . $e->getMessage());
}
// Upload a file
RemoteServer::upload('local/path/file.txt', '/remote/path/file.txt');
// Download a file
$remoteFile = RemoteServer::download('/remote/path/file.txt', 'local/path/');
Use the authenticate() method for SSH keys or passwords:
RemoteServer::authenticate([
'username' => 'deploy',
'password' => env('REMOTE_PASSWORD'),
// OR for SSH keys:
'private_key' => file_get_contents('/path/to/key'),
'passphrase' => env('KEY_PASSPHRASE'),
]);
$commands = [
'cd /var/www/project',
'git pull origin main',
'composer install --no-dev',
];
foreach ($commands as $command) {
RemoteServer::execute($command);
}
Offload remote tasks to a queue job:
use Acassan\RemoteServer\Jobs\RemoteCommandJob;
RemoteCommandJob::dispatch('php artisan optimize:clear')
->onQueue('remote-tasks');
Trigger actions post-remote execution:
RemoteServer::execute('deploy:update')->then(function ($output) {
event(new DeploymentCompleted($output));
});
Use Laravel's binding to dynamically configure remote servers:
$this->app->bind('remote.server.staging', function () {
return RemoteServer::make()
->setHost('staging.example.com')
->setPort(22);
});
Connection Timeouts
'timeout' => 30, // seconds
RemoteServer::setDebug(true);
Permission Issues
sudo cautiously (may require password input).SSH Key Authentication
~/.ssh/ directory is properly configured on the remote server.passphrase option.Command Output Buffering
$stream = RemoteServer::stream('tail -f /var/log/app.log');
while ($line = $stream->read()) {
Log::info($line);
}
Enable Verbose Logging
RemoteServer::setVerbose(true);
Check Laravel logs for detailed SSH/connection output.
Test Locally First
Use phpseclib's built-in tests to validate credentials:
use phpseclib\Net\SSH2;
$ssh = new SSH2(env('REMOTE_HOST'));
if (!$ssh->login(env('REMOTE_USER'), env('REMOTE_PASSWORD'))) {
die('Login failed');
}
Handle Partial Failures
Use try-catch blocks to gracefully handle transient failures:
try {
RemoteServer::execute('failing-command');
} catch (\Acassan\RemoteServer\Exceptions\ConnectionException $e) {
// Retry logic or notify admin
}
Custom Command Classes
Extend the base RemoteCommand class for reusable logic:
namespace App\RemoteCommands;
use Acassan\RemoteServer\RemoteCommand;
class DeployCommand extends RemoteCommand {
protected $commands = [
'git pull',
'composer install',
'php artisan cache:clear',
];
}
Middleware for Remote Calls Add middleware to log or transform remote requests:
RemoteServer::getMiddleware()->push(function ($request) {
Log::info("Remote call: {$request->command}");
return $request;
});
Dynamic Server Selection Use Laravel's container to switch servers dynamically:
$server = app('remote.server.' . $environment);
$server->execute('command');
Webhook Triggers
Combine with Laravel's webhook package to trigger remote actions via HTTP:
Route::post('/deploy', function () {
RemoteServer::execute('deploy:latest');
return response()->json(['status' => 'deployed']);
});
How can I help you explore Laravel packages today?