adam78
adam78

Reputation: 10068

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?

Upvotes: 1

Views: 7215

Answers (5)

kodmanyagha
kodmanyagha

Reputation: 985

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

Upvotes: 1

ic.kim
ic.kim

Reputation: 1

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.

Upvotes: 0

Saif Uddin
Saif Uddin

Reputation: 187

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

Upvotes: 2

adam78
adam78

Reputation: 10068

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

Upvotes: 2

nayeemdev
nayeemdev

Reputation: 1241

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.

Upvotes: 4

Related Questions