SSH

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 app/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:

  1. SSH::run(array(
  2. 'cd /var/www',
  3. 'git pull origin master',
  4. ));

Running Commands On A Specific Connection

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

  1. SSH::into('staging')->run(array(
  2. 'cd /var/www',
  3. 'git pull origin master',
  4. ));

Catching Output From Commands

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

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

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:

  1. SSH::into('staging')->define('deploy', array(
  2. 'cd /var/www',
  3. 'git pull origin master',
  4. 'php artisan migrate',
  5. ));

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

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

SFTP Downloads

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

  1. SSH::into('staging')->get($remotePath, $localPath);
  2. $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:

  1. SSH::into('staging')->put($localFile, $remotePath);
  2. 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:

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

Envoy Task Runner

Laravel Envoy provides a clean, minimal syntax for defining common tasks you run on your remote servers. Using a Blade style syntax, you can easily setup tasks for deployment, Artisan commands, and more.

Note: Envoy requires PHP version 5.4 or greater, and only runs on Mac / Linux operating systems.

Installation

First, install Envoy using the Composer global command:

  1. composer global require "laravel/envoy=~1.0"

Make sure to place the ~/.composer/vendor/bin directory in your PATH so the envoy executable is found when you run the envoy command in your terminal.

Next, create an Envoy.blade.php file in the root of your project. Here's an example to get you started:

  1. @servers(['web' => '192.168.1.1'])
  2. @task('foo', ['on' => 'web'])
  3. ls -la
  4. @endtask

As you can see, an array of @servers is defined at the top of the file. You can reference these servers in the on option of your task declarations. Within your @task declarations you should place the Bash code that will be run on your server when the task is executed.

The init command may be used to easily create a stub Envoy file:

  1. envoy init [email protected]

Running Tasks

To run a task, use the run command of your Envoy installation:

  1. envoy run foo

If needed, you may pass variables into the Envoy file using command line switches:

  1. envoy run deploy --branch=master

You may use the options via the Blade syntax you are used to:

  1. @servers(['web' => '192.168.1.1'])
  2. @task('deploy', ['on' => 'web'])
  3. cd site
  4. git pull origin {{ $branch }}
  5. php artisan migrate
  6. @endtask

Bootstrapping

You may use the @setup directive to declare variables and do general PHP work inside the Envoy file:

  1. @setup
  2. $now = new DateTime();
  3. $environment = isset($env) ? $env : "testing";
  4. @endsetup

You may also use @include to include any PHP files:

  1. @include('vendor/autoload.php');

Multiple Servers

You may easily run a task across multiple servers. Simply list the servers in the task declaration:

  1. @servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])
  2. @task('deploy', ['on' => ['web-1', 'web-2']])
  3. cd site
  4. git pull origin {{ $branch }}
  5. php artisan migrate
  6. @endtask

By default, the task will be executed on each server serially. Meaning, the task will finish running on the first server before proceeding to execute on the next server.

Parallel Execution

If you would like to run a task across multiple servers in parallel, simply add the parallel option to your task declaration:

  1. @servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])
  2. @task('deploy', ['on' => ['web-1', 'web-2'], 'parallel' => true])
  3. cd site
  4. git pull origin {{ $branch }}
  5. php artisan migrate
  6. @endtask

Task Macros

Macros allow you to define a set of tasks to be run in sequence using a single command. For instance:

  1. @servers(['web' => '192.168.1.1'])
  2. @macro('deploy')
  3. foo
  4. bar
  5. @endmacro
  6. @task('foo')
  7. echo "HELLO"
  8. @endtask
  9. @task('bar')
  10. echo "WORLD"
  11. @endtask

The deploy macro can now be run via a single, simple command:

  1. envoy run deploy

Notifications

HipChat

After running a task, you may send a notification to your team's HipChat room using the simple @hipchat directive:

  1. @servers(['web' => '192.168.1.1'])
  2. @task('foo', ['on' => 'web'])
  3. ls -la
  4. @endtask
  5. @after
  6. @hipchat('token', 'room', 'Envoy')
  7. @endafter

You can also specify a custom message to the hipchat room. Any variables declared in @setup or included with @include will be available for use in the message:

  1. @after
  2. @hipchat('token', 'room', 'Envoy', "$task ran on [$environment]")
  3. @endafter

This is an amazingly simple way to keep your team notified of the tasks being run on the server.

Slack

The following syntax may be used to send a notification to Slack:

  1. @after
  2. @slack('team', 'token', 'channel')
  3. @endafter

Updating Envoy

To update Envoy, simply run the self-update command:

  1. envoy self-update

If your Envoy installation is in /usr/local/bin, you may need to use sudo:

  1. composer global update