I want to run the shell code when the Laravel application runs. I created a provider and registered the first provider
'providers' => [
App\Providers\RunServiceProvider::class,
/*
* Laravel Framework Service Providers...
*/
....
And I put this code in boot
public function boot()
{
if (!is_writable(storage_path()) || !is_writable(base_path('bootstrap/cache'))) {
shell_exec('sudo chown -R $USER:www-data ./storage/');
shell_exec('sudo chown -R $USER:www-data ./bootstrap/cache/');
shell_exec("chmod -R 775 ./storage/");
shell_exec("chmod -R 775 ./bootstrap/cache/");
}
}
But it doesn't work, is the code correct? Is the storage & bootstrap path correct?
Is there a way to give permissions to folders?
CodePudding user response:
To execute sudo command without password, you should setup sudo before. Pls have a look here: https://linuxhandbook.com/sudo-without-password/
But in general, it's a bad practice to do things like that, especially on the app boot. Why do you need to check & update permissions on every request? Normally the permissions are not changing so often. Usually such things can be done once during the deploy and automated.