Run PHP Artisan  Commands on Shared Hosting

Run PHP Artisan Commands on Shared Hosting

Admin
Admin・ 6 Juli 2021
7 min read ・ 4467 views

PHP Artisan on Shared Hosting - Shared Hosting Service is a hosting service where many sites are on one web server that is connected to the internet. Shared hosting is generally the most economical hosting option, as the overall cost of maintaining the server is spread over many customers.

Source: wikipedia

Some hosting service providers, do not provide terminal features on their shared hosting services. Indeed, some have terminal features, but they are usually differentiated based on the price of the package offered. This terminal feature on shared hosting functions to run commands like those usually run on local development.

When we upload or deploy a laravel project to shared hosting, we cannot use terminal features to run artisan commands from laravel such as config:cache, config:clear, cache:clear, storage:link, and others. Then the question is, how to run artisan commands on shared hosting if there is no terminal feature? Don't worry, we can still run artisan commands from laravel even though we don't go through the terminal, namely by route or it could be with cronjob.

In this article, I will share an article that will discuss how to run php artisan commands on shared hosting without going through the terminal. Read on until the end of the article.

Running Artisan Commands on Route Shared Hosting

Artisan::call(your-command);

To be able to run artisan commands with media routes, we just need to create a route and add a command like the example above. Here I give an example of how to run the artisan command (which is the most frequently used) on a Shared Hosting route:

  1. Running the config:clear Command on Shared Hosting
    Route::get('/config-clear', function() {
        Artisan::call('config:clear'); 
        return 'Configuration cache cleared!';
    });
  2. Running the config:cache Command on Shared Hosting
    Route::get('/config-cache', function() {
        Artisan::call('config:cache');
        return 'Configuration cache cleared! <br> Configuration cached successfully!';
    });
  3. Running the cache:clear Command on Shared Hosting
    Route::get('/cache-clear', function() {
        Artisan::call('cache:clear');
        return 'Application cache cleared!';
    });
  4. Running the view:cache Command  on Shared Hosting
    Route::get('/view-cache', function() {
        Artisan::call('view:cache');
        return 'Compiled views cleared! <br> Blade templates cached successfully!';
    });
  5. Running the view:clear Command on Shared Hosting
    Route::get('/view-clear', function() {
        Artisan::call('view:clear');
        return 'Compiled views cleared!';
    });
  6. Running the route:cache Command on Shared Hosting
    Route::get('/route-cache', function() {
        Artisan::call('route:cache');
        return 'Route cache cleared! <br> Routes cached successfully!';
    });
  7. Running the route:clear Command on Shared Hosting
    Route::get('/route-clear', function() {
        Artisan::call('route:clear');
        return 'Route cache cleared!';
    });
  8. Running the storage:link Command on Shared Hosting
    Route::get('/storage-link', function() {
        Artisan::call('storage:link');
        return 'The links have been created.';
    });

    If you deploy or upload a laravel project to shared hosting by not separating the public folder with other laravel folders or files, you can create or run the php artisan storage:link command on shared hosting by creating a route as above.

    ln -s /home/u111xxxx/yourapp/storage/app/public /home/u111xxxx/public_html/storage
    Another way that can be used to create or run the php artisan storage:link command on shared hosting is by using a cronjob. If the way we deploy or upload a laravel project to shared hosting is by separating the public folder (upload in public_html) with folders or files from other laravel, the command that can be used to create storage:link via cronjob is like the example above. In the example above, replace the value of u111xxxx with the username or cPanel account name and the yourapp value can be replaced with the project name.
    ln -s /home/u111xxxx/public_html/storage/app/public /home/u111xxxx/public_html/public/storage
    And if the method of uploading a laravel project to shared hosting does not separate the public folder from other laravel folders or files, the command that can be used to create or run php artisan storage on Shared Hosting can be like the example above. In the example above, please replace u111xxxx with your username or cPanel account name.
  9. Running php artisan down Command on Shared Hosting
    Route::get('/down', function() {
        Artisan::call('down');
        return 'Application is now in maintenance mode.';
    });
    The php artisan down command is used to change the application or website to maintenance mode, so that after running this command the website or application cannot be accessed.
  10. Running php artisan up Command on Shared Hosting
    Route::get('/up', function() {
        Artisan::call('up');
        return 'Application is now live.';
    });
    So that the website or application can be accessed again, we need to run the php artisan up command to change it to live mode again.

Menjalankan Artisan dengan Artisan UI Package

running php artisan command with artisan ui package

If we use at least PHP version 8, we can use the artisan ui package from Lorisleiva to run artisan commands. We can use this package to run artisan commands both locally and on the hosting server.

With this artisan ui package, it will certainly help us to run artisan commands on shared hosting, even though some hosting packages provided by hosting service providers do not include terminal features for certain hosting packages. So, before we deploy or upload our Laravel project on shared hosting, we can install this package first in our local development.

composer require lorisleiva/artisan-ui
php artisan artisan-ui:install

The installation is quite easy. Please open a terminal, then run the commands above sequentially. After the installation process is complete, now if you try to access example.com/artisan in a browser it will display a display like in the image above. Please select which of the artisan commands you want to run.

running php artisan command on shared hosting with artisan ui package

Here I will give an example of running the storage:link command from the artisan ui package. As in the picture above, after selecting the artisan command, you can directly click "Execute" to run the php artisan storage:link command. When I try to check in the public folder, there is already a storage folder. The result is exactly the same as running php artisan storage:link in the terminal.

Sources & Documentation: github.com/lorisleiva/artisan-ui

So this article is about how to run laravel artisan commands with the artisan::call helper on routes on Shared Hosting and how to easily run artisan commands with the artisan ui package. Please decide for yourself which method you want to use to run the artisan command on shared hosting, whether you want to use the route method or want to use the artisan ui package.

Hopefully it is useful and can help, if there are criticisms, suggestions or whatever you want to discuss, please write a comment in the comment form provided below. Good luck and see you in the next articles that are no less interesting.

Tinggalkan Komentar
Loading Comments