Run PHP Artisan Commands on Shared Hosting
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:
- Running the config:clear Command on Shared Hosting
Route::get('/config-clear', function() { Artisan::call('config:clear'); return 'Configuration cache cleared!'; });
- 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!'; });
- Running the cache:clear Command on Shared Hosting
Route::get('/cache-clear', function() { Artisan::call('cache:clear'); return 'Application cache cleared!'; });
- 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!'; });
- Running the view:clear Command on Shared Hosting
Route::get('/view-clear', function() { Artisan::call('view:clear'); return 'Compiled views cleared!'; });
- 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!'; });
- Running the route:clear Command on Shared Hosting
Route::get('/route-clear', function() { Artisan::call('route:clear'); return 'Route cache cleared!'; });
- 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.
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/yourapp/storage/app/public /home/u111xxxx/public_html/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.ln -s /home/u111xxxx/public_html/storage/app/public /home/u111xxxx/public_html/public/storage
- Running php artisan down Command on Shared Hosting
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.Route::get('/down', function() { Artisan::call('down'); return 'Application is now in maintenance mode.'; });
- Running php artisan up Command on Shared Hosting
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.Route::get('/up', function() { Artisan::call('up'); return 'Application is now live.'; });
Menjalankan Artisan dengan 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.
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.
- Cara Mengatasi Error XAMPP: MySQL shutdown unexpectedly 23 Oktober 2021 66094 views
- Laravel 8: REST API Authentication dengan Sanctum 17 September 2021 31803 views
- Tutorial CRUD (Create, Read, Update & Delete) Codeigniter 4 dengan Bootstrap 14 Oktober 2021 30197 views
- Membuat REST API CRUD di Laravel 8 dengan Sanctum 18 September 2021 28293 views
- Contoh Cara Menggunakan Sweet Alert di Laravel 8 27 Agustus 2021 27463 views