Execute Artisan commands on remote servers

Original – by Freek Van der Herten – 3 minute read

Using the newly released spatie/laravel-remote package, you can quickly execute Artisan commands on a remote server.

Here's an example that will clear the cache on the remote server.

php artisan remote cache:clear

In this blog post, I'd like to tell you all about it!

Using laravel-remote

You can install the package in any Laravel app using

composer require spatie/laravel-remote

No surprises there!

The package contains a remote command that will connect to your remote server and execute a given artisan command there.

By setting these environment variables in your .env file, the package knows where to connect to. Here's an example for a site hosted on Forge.

#in your .env file
REMOTE_HOST=example.com
REMOTE_USER=forge
REMOTE_PATH=/home/forge/example.com

With this in place, you can clear the cache on your server with:

php artisan remote cache:lear

Of course, you can use an Artisan command you'd like.

If you want to execute a bash command, use the --raw option.

Here we will get a list of files on the server.

php artisan remote ls --raw

You can define hosts in the config file. By default, the default host is used. To execute a command on another host, use the --host option.

php artisan remote cache:clear --host=my-other-host

Shorten the command using bash aliases

For many years I have been aliasing "php artisan" to "a". This allows me to execute any artisan command very quickly.

a clear:cache

It's a small improvement, but I'm sure that the alias has saved me quite some time summed up over all the years.

I've now added another alias, "ar," which maps to "php artisan remote".

ar clear:cache

It's pretty cool with how little efforts this allows me to execute commands on a remote server.

How it works under the hood

When executing remote, the package will connect to your server via SSH, cd to your app's directory, and execute the command.

In this streaming session, you can see me build up the package from scratch.

In closing

I think spatie/laravel-remote can save you a lot of time if you regularly want to execute commands on a remote server.

Be sure to take a look at this list of packages our team has created previously.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

duydinh liked on 11th March 2021
Victorhugo Pereira m liked on 11th March 2021
Ashish Dhamala liked on 11th March 2021
Blair Jersyer liked on 11th March 2021
Johan Alvarez liked on 11th March 2021
Kamau Wanyee liked on 11th March 2021
Cyrus liked on 11th March 2021
Philo Hermans liked on 11th March 2021
SouthDreamz liked on 11th March 2021
Zainal Hasan liked on 11th March 2021
Godwin Effiòng liked on 11th March 2021
Tom Witkowski liked on 11th March 2021
PEGU_Consulting liked on 11th March 2021
Khagesh liked on 11th March 2021
K. liked on 11th March 2021
Sadegh 🐘 liked on 11th March 2021
Cătălin Zmole 🔅 liked on 11th March 2021
Christian Klemp liked on 11th March 2021
Bhavesh Daswani #laravel liked on 11th March 2021
βliss Jäspis liked on 11th March 2021
Debjit Biswas replied on 11th March 2021
The first things come to my mind is what what about security.
C-Bass liked on 11th March 2021
Fagbenro Samuel liked on 11th March 2021
Thibault Lavoisey liked on 11th March 2021
Marco liked on 11th March 2021
belal majeed liked on 11th March 2021
Luca Grandicelli liked on 10th March 2021
Jānis Lācis liked on 10th March 2021
$MIGUEL Piedrafita 🥬 liked on 10th March 2021
Tony liked on 10th March 2021
Marc (rm -rf 202*) Hampson liked on 10th March 2021
Frode Knutsen liked on 10th March 2021
Khaled Al Wakeel liked on 10th March 2021
Brian Dillingham liked on 10th March 2021
richard_bell liked on 10th March 2021
Tim MacDonald liked on 10th March 2021
KGG liked on 10th March 2021
Bee business liked on 10th March 2021
Paul Redmond 🇺🇸 liked on 10th March 2021
Mateus Galasso liked on 10th March 2021
Derick Alangi 🌱 liked on 10th March 2021
Matthew Poulter liked on 10th March 2021
Helder Willian liked on 10th March 2021
Salman Zafar liked on 10th March 2021
Tim 🦕 liked on 10th March 2021
Joshua Kin liked on 10th March 2021
Aseem Lalfakawma liked on 10th March 2021
Claudio_Pereira liked on 10th March 2021
Nick Stanko liked on 10th March 2021
M. Vug liked on 10th March 2021
Aleksandar Mitić liked on 10th March 2021
Snr Apst. MAKANAKY liked on 10th March 2021
Tauseef shah liked on 10th March 2021
René Sinnbeck liked on 10th March 2021
Mike liked on 10th March 2021
Wyatt liked on 10th March 2021
Sietze Keuning ♛ liked on 10th March 2021
Andreeas Trattner liked on 10th March 2021
Brandon Surowiec retweeted on 10th March 2021
Sid 🏄 liked on 10th March 2021
Nick retweeted on 10th March 2021
Hadi Swais liked on 10th March 2021
Jonathan Page liked on 10th March 2021
Brandon Surowiec liked on 10th March 2021
Mohan Raj liked on 10th March 2021
Shafqat ali liked on 10th March 2021
Nick liked on 10th March 2021
Spatie retweeted on 10th March 2021
assoftTR retweeted on 10th March 2021
James liked on 10th March 2021
Andy Andrew retweeted on 10th March 2021
PHP Synopsis retweeted on 10th March 2021
Shane Lessard liked on 10th March 2021
Jigal Sanders liked on 10th March 2021
David Barker liked on 10th March 2021
/dev/prabakaran liked on 10th March 2021
José Cage liked on 10th March 2021
reformation liked on 10th March 2021
Maarten de Graaf liked on 10th March 2021
Christiano30 liked on 10th March 2021
Richard van Baarsen liked on 10th March 2021
Robin Dirksen liked on 10th March 2021
Asif Mulla liked on 10th March 2021
Bijaya Prasad Kuikel liked on 10th March 2021
Paras Malhotra liked on 10th March 2021
Marian Pop 🦄 liked on 10th March 2021
Abdulkadir Olatunji liked on 10th March 2021
Iain Poulson liked on 10th March 2021
Nuno Maduro 🧑🏼‍🚀 liked on 10th March 2021
Möhre liked on 10th March 2021
Haz 👨‍💻 ✈ 🇫🇮 liked on 10th March 2021
assoftTR liked on 10th March 2021
gary liked on 10th March 2021