Often, the developer struggles to run php artisan commands on shared hosting servers because shared hosting does not provide any terminal or command-line tool to run a php artisan commands. So let’s see how to do it.
When Do You Need To Run php artisan Commands On Hosting?
Well, we all know that we can easily run all php artisan commands in our local development environment using different command-line tools, but it seems tough to remove the cache, compiled views, routes, etc. on the shared hosting servers because they do not provide a shell, terminal or any other command-line tool. While all that you get into the dedicated hosting and VPS servers.
Let’s crack it together. We presume that you know all about the Laravel basics.
Run php artisan Commands Using Route
To run any php artisan commands by using route you just need to create a route and need to add command by using the following syntax:
Artisan::call(your-command);
01 Run config:clear in Shared Hosting Server
Route::get('/config-clear', function() {
Artisan::call('config:clear');
// Do whatever you want either a print a message or exit
});
02 Run config:cache in Shared Hosting Server
Route::get('/config-cache', function() {
Artisan::call('config:cache');
// Do whatever you want either a print a message or exit
});
03 Clear Cache in Shared Hosting Server
Route::get('/cache-clear', function() {
Artisan::call('cache:clear');
// Do whatever you want either print a message or exit
});
04 Clear View in Shared Hosting Server
Route::get('/view-clear', function() {
Artisan::call('view:clear');
// Do whatever you want either print a message or exit
});
How To Test Commands?
Let’s assume that your website domain is example.com
, then If you want to clear the cache then you need to run example.com/cache-clear
as defined above.
A blank screen will appear as per the above examples and if you add any messages, dd(), exit(),
etc. in the above routes then it will behave according to that.
Additionally, read our guide:
- Best Way to Remove Public from URL in Laravel
- Error After php artisan config:cache In Laravel
- Specified Key Was Too Long Error In Laravel
- Active Directory Using LDAP in PHP or Laravel
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
That’s it for now. We hope this article helped you to learn how to run php artisan commands on live servers.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!
Mention that drafting too often aid from the administrators may pass to the damage state unconcealed and unmoving before you and your friends change really purloined booming plus.
to protect the command, Install auth and run it through middleware after proper login
Route::middleware([‘auth:sanctum’, ‘verified’])->get(‘/config-cache’, function() {
$status = Artisan::call(‘config:Cache’);
return ‘Configurations cache cleared’;
});
Nice info,
I would also create a generic route like `/artisan/request`, were it’s possible to send a request with:
– command name
– arguments
– options
That would make the route more similar to how artisan behaves in a shell.
wim suggestion shall be really taken in consideration, I wouldn’t leave this route exposed to public.