Loading...

Menu

SSH

Installation

If you have changed the top-level namespace to something like 'MyCompany', then you would use the new namespace instead of 'App'.

Begin by installing this package through Composer.

$ composer require laravelcollective/remote

Finally, publish the config file:

  php artisan vendor:publish --provider="Collective\Remote\RemoteServiceProvider"

Configuration

Laravel includes a simple way to SSH into remote servers and run commands, allowing you to easily build Artisan tasks that work on remote servers. The SSH facade provides the access point to connecting to your remote servers and running commands.

The configuration file is located at config/remote.php, and contains all of the options you need to configure your remote connections. The connections array contains a list of your servers keyed by name. Simply populate the credentials in the connections array and you will be ready to start running remote tasks. Note that the SSH can authenticate using either a password or an SSH key.

Note: Need to easily run a variety of tasks on your remote server? Check out the Envoy task runner!

Basic Usage

Running Commands On The Default Server

To run commands on your default remote connection, use the SSH::run method:

SSH::run([
    'cd /var/www',
    'git pull origin master',
]);

Running Commands On A Specific Connection

Alternatively, you may run commands on a specific connection using the into method:

SSH::into('staging')->run([
    'cd /var/www',
    'git pull origin master',
]);

Catching Output From Commands

You may catch the "live" output of your remote commands by passing a Closure into the run method:

SSH::run($commands, function($line)
{
    echo $line.PHP_EOL;
});

Tasks

If you need to define a group of commands that should always be run together, you may use the define method to define a task:

SSH::into('staging')->define('deploy', [
    'cd /var/www',
    'git pull origin master',
    'php artisan migrate',
]);

Once the task has been defined, you may use the task method to run it:

SSH::into('staging')->task('deploy', function($line)
{
    echo $line.PHP_EOL;
});

SFTP Downloads

The SSH class includes a simple way to download files using the get and getString methods:

SSH::into('staging')->get($remotePath, $localPath);

$contents = SSH::into('staging')->getString($remotePath);

SFTP Uploads

The SSH class also includes a simple way to upload files, or even strings, to the server using the put and putString methods:

SSH::into('staging')->put($localFile, $remotePath);

SSH::into('staging')->putString($remotePath, 'Foo');

Tailing Remote Logs

Laravel includes a helpful command for tailing the laravel.log files on any of your remote connections. Simply use the tail Artisan command and specify the name of the remote connection you would like to tail:

php artisan tail staging

php artisan tail staging --path=/path/to/log.file

Make something worth sharing.