简体   繁体   中英

Laravel - failed to open stream: Permission denied

I'm trying to install a laravel app on digital ocean. When I run the command php artisan migrate --seed I'm getting the following error when it reaches one of the seeders:

 The stream or file "/var/www/test.mysite.com/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied

I followed the DO tutorial ( https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-laravel-with-nginx-on-ubuntu-20-04 ) and added the following permissions:

sudo chown -R www-data.www-data /var/www/test.mysite.com/storage
sudo chown -R www-data.www-data /var/www/test.mysite.com/bootstrap/cache

So why is it still throwing the permission denied error?

Run this command for storage permission

sudo chmod -R 755 storage/

Also you can set nginx permissive mode

semanage permissive -a httpd_t

The nginx permissive mode working fine for me.

Managed to fix. I needed to give my self (the logged in ssh user) ownership of the directory and the webser as follows:

sudo chown -R $USER:$USER /var/www/test.mysite.com
sudo chgrp -R www-data /var/www/test.mysite.com/storage /var/www/test.mysite.com/bootstrap/cache
sudo chmod -R ug+rwx /var/www/test.mysite.com/storage /var/www/test.mysite.com/bootstrap/cache

Go your project root directory and run this command

chmod -R 777 storage/

If there for bootstrap cache try this

chmod -R 777 bootstrap/cache

You should set security settings as follows

setsebool -P httpd_unified 1

If default port(80/tcp) is changed run the following command in addition.

setsebool -P nis_enabled 1

this works for me.

In my side it is about WWWUSER and WWWGROUP env variables. In local env these values are 1000 . I copied all.env files to server and then I can't able to write a file in docker (or sail). Because server's uuid and guid are different. You must set these correctly in.env file, otherwise docker can not able to write a file in project (log, session etc...).

Solution steps:

1- Find correct uuid and guid:

$ cat /etc/passwd | grep $(whoami)
exampleuser:x:1007:1008::/home/exampleuser:/bin/bash

2- The column order is like that: [username]:[password]:[UUID]:[GUID] So 1007 is uuid, 1008 is guid. Then we must set these values in.env file.

WWWUSER=1007
WWWGROUP=1008

3- Rebuild and restart containers. Actually I don't know is this step necessary but I strongly suggest make it.

sail stop
sail build --no-cache
sail down
sail up -d

After that you can run whatever you want with sail command. For example:

sail composer install
sail artisan key:generate
sail artisan cache:clear
sail artisan [any command which you want to execute]

Edit: Making writable all folders is not a good idea. Folders permissions must be 755 or 750. If you set them 777 then other shared hostings can access to your files. This is a vulnerable. If you did this then don't forget to set folder permissions to 750 or 755. chmod -R 750 storage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM