Home » Run PHP Artisan Commands On Shared Hosting Servers

Run PHP Artisan Commands On Shared Hosting Servers

Last updated on December 24, 2020 by

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.

Table of Contents
1. When Do You Need To Run php artisan Commands On Hosting?
2. Run php artisan Commands Using Route
3. Run config:clear in Shared Hosting Server
4. Run config:cache in Shared Hosting Server
5. Clear Cache in Shared Hosting Server
6. Clear View in Shared Hosting Server
7. How To Test Commands?

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:

  1. Best Way to Remove Public from URL in Laravel
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. Active Directory Using LDAP in PHP or Laravel
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. 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!

 
 

3 thoughts on “Run PHP Artisan Commands On Shared Hosting Servers”

  1. 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.

    Reply
  2. 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’;
    });

    Reply
  3. 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.

    Reply

Leave a Comment