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:

php artisan remote cache:clear

# Raw ls command on the server
php artisan remote ls --raw

# Raw ls with flags
php artisan remote --raw "ls -a"


# Defining which host configuration to use
php 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:

return [
    'hosts' => [
        'default' => [
            'host' => env('REMOTE_HOST'),
            'port' => env('REMOTE_PORT', 22),
            'user' => env('REMOTE_USER'),
            'path' => env('REMOTE_PATH'),
        ],
        'example2' => [
            'host' => env('EXAMPLE2_REMOTE_HOST'),
            'port' => env('EXAMPLE2_REMOTE_PORT', 22),
            'user' => env('EXAMPLE2_REMOTE_USER'),
            'path' => env('EXAMPLE2_REMOTE_PATH'),
        ],
    ],
];

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.