Home > Software design >  Laravel Permission denied error on Linux Server
Laravel Permission denied error on Linux Server

Time:05-26

I am facing a very strange error with my Laravel application on the production server (Linux). Whenever the users of my application login for the first time in morning, they get a permission denied error which read something like

file_put_contents(/var/www/html/PROJECT/storage/framework/cache/data/0c/e5/0ce52dca12715a327eb4c1b4bff36293ea67c719): Failed to open stream: No such file or directory

To overcome this, the first thing I have to do in the morning is to give permission to the entire project by

sudo chmod -R 777 PROJECT

And then it runs just fine. This is slowly getting very annoying as it is happening every morning. Why are the permissions getting revoked automatically and is there a permanent solution for this?

Please help me and thank you all in advance

CodePudding user response:

You can check if your webserver has enough permission to the directory instead of giving 777 to the project as this is your production environment which is not at all recommendable.

Also try php artisan cache:clear

CodePudding user response:

I think your application run some command also. That's why your storage permission is overrated by system user. (by which user cron execute).

The thing is, storage directory should have write access to both system user and webserver(apache/nginx)

BTW, Symfony framework has some nice solution for this kind of situation which also can be application in Laravel Application.

Please look at this: https://symfony.com/doc/current/setup/file_permissions.html

In your case, this command would be like:

HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1)

sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX /var/www/html/PROJECT/storage

sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX /var/www/html/PROJECT/storage

You can also achieve above solution if you know know what is your webserver user.

sudo chown -R "$local_user":"$webserver_user" "/var/www/html/PROJECT/storage"
sudo chmod -R 0777 "/var/www/html/PROJECT/storage"

If my opinion, setfacl is better solution if you have setfacl installed in your server.

  • Related