Run Artisan Commands on Remote Servers

Packages

March 11th, 2021

spatie-remote-featured.png

Laravel Remote is a package by Spatie that provides a command to execute Artisan commands on a remote server.

Here are some examples of remote commands from the documentation:

1php artisan remote cache:clear
2 
3# Raw ls command on the server
4php artisan remote ls --raw
5 
6# Raw ls with flags
7php artisan remote --raw "ls -a"
8 
9 
10# Defining which host configuration to use
11php artisan remote cache:clear --host=my-other-host

At the heart of the package's configuration, you define hosts for remote servers that you want to interact with. In order to execute a command on a given host, you can use the --host flag for the desired configuration:

1return [
2 'hosts' => [
3 'default' => [
4 'host' => env('REMOTE_HOST'),
5 'port' => env('REMOTE_PORT', 22),
6 'user' => env('REMOTE_USER'),
7 'path' => env('REMOTE_PATH'),
8 ],
9 'example2' => [
10 'host' => env('EXAMPLE2_REMOTE_HOST'),
11 'port' => env('EXAMPLE2_REMOTE_PORT', 22),
12 'user' => env('EXAMPLE2_REMOTE_USER'),
13 'path' => env('EXAMPLE2_REMOTE_PATH'),
14 ],
15 ],
16];

Under the hood, this package uses Spatie's ssh package, a lightweight PHP library to execute commands over SSH.

If you'd like to follow the author Freek Van der Herten to see how to build this package, he live-streamed it on YouTube. Learn practical examples from one of the most prolific Laravel package developers, where he shows you tips on how to build Laravel packages from scratch:

Learn More

To get started with spatie/laravel-remote, read the documentation available in the project's GitHub readme. Freek also wrote a blog post that goes into more detail about the package.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.