Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

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

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.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month 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.


What are your thoughts on "Execute Artisan commands on remote servers"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.