FIle could not be opened in append mode: failed to open stream: Permission denied laradock
Asked Answered
A

7

40

I'm setting up laradock (Setup for Multiple Projects) following the official documentation from the Laradock in my local machine.

After installation I installed the laravel through workspace container bash. I did configured the config file for the app in nginx/sites/ directory and in /etc/hosts file.

While visiting the development url I'm getting the following error message:

The stream or file "/var/www/laravel-api/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied

Alleviation answered 1/8, 2020 at 8:12 Comment(3)
What are the perms for the storage directory? It may be that the permissions for the directory are causing the issue here. You can find this with the ls -l /var/www/laravel-api/storage/ command in your terminal.Cacophonous
Checked from workspace container bash. storage/logs/ directory has drwxr-xr-x 2 root root 4096 Aug 1 07:37 logs Alleviation
Please see this as well: vsupalov.com/docker-shared-permissions. The files that are created/overwritten by your docker containers in your shared folders can have undesirable permissions if you do not configure them correctly.Catalepsy
T
68

This worked for me:

chown -R www-data:www-data "project foldername"
Transistor answered 6/2, 2021 at 21:37 Comment(3)
when i paste this in my terminal in cpanel server it says "chown: invalid user: ‘www-data:www-data’"Berm
@ehsanasarzadeh it depens under which user your webserver is running. Sometimes it is apache, www-data, www or even root. find out which on is correct for you system an adapt the statement accordingly.Georgiannageorgianne
To add yourself in www-data group, use command sudo usermod -a -G www-data userName. So that you can have write access to folder as well.Sharla
L
18

If you are still facing the issue even after changing the permission and owner of file, just check for the OS of you Linux server. Login to your server

$ ssh user@server-ip

check your OS running on linux server

$ cat /etc/os-release

//output

NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"

If it is CentOS, you need to disable Security-Enhanced Linux (SELinux) if enabled. Check for SELinux status

$ sestatus

If it is enabled, then

$ setenforce Permissive

Or

$ sudo setenforce 0

Have a good day!

Longoria answered 26/8, 2021 at 10:14 Comment(1)
i disabled seLinux and everything worked. Logging and me trying to connect to ldap. But I enabled seLinux again and found another way for ldap. setsebool httpd_can_connect_kdap on. And for logging I use setfaclMammoth
C
11

If you aren't running your application as root on your web server, then it wont have write access based on the permissions you've provided.

Checked from workspace container bash. storage/logs/ directory has drwxr-xr-x 2 root root 4096 Aug 1 07:37 logs

The error is complaining about permission denial for opening in append mode - it doesn't have permission to write to the file, only root does.

What you need to do is make your web server group the owner of the storage directory:

chown -R www-data:www-data /var/www/laravel-api/storage/

The www-data can be switched out for whatever group your web server is associated with.

To avoid completely repeating an amazing and complete answer, I recommend you give this answer a read:

How to set up file permissions for Laravel?

Cacophonous answered 1/8, 2020 at 10:34 Comment(0)
A
9

you have to enter the workspace first by typing "docker-compose exec workspace bash"(without quotes)

To give a write permission to a single file,

chmod -R 777 /var/www/laravel-api/storage/logs/laravel.log

or

chmod -R 777 /var/www/laravel-api/storage/logs

or

sudo chmod -R 777 /var/www/laravel-api/storage/logs/laravel.log

when the same error appears but different folders or files, do the same thing only change the folder name

example :

chmod -R 777 /var/www/laravel-api/storage/logs

chmod -R 777 /var/www/laravel-api/storage/framework/views

chmod -R 777 /var/www/laravel-api/storage/framework/sessions
Adalie answered 12/4, 2021 at 10:23 Comment(4)
Its risky to use 777 permission on a live server as it can be a point of exploitation by a hacker. At all cost avoid it. Try using 775Sidereal
@stanleymbote - Granting access to a single .log file?Adalie
Ofcourse you first need access to the server to even attempt accessing that log file. But once you have it, you could enter malicious code into it and hope that the app that reads the log file doesn't escape the output or whatever. Security benefits from having many layers, and filesystem rights is one of themBevvy
777 is the worst practice and it should be always avoidedLarentia
W
5

Give group write access to /storage/logs/

sudo chmod g+w storage/logs
Warty answered 13/5, 2021 at 11:6 Comment(0)
A
4

You need to run the following command. It works for me:

step 1:

sudo chmod -R +rwX .

step 2:

sudo chown -R $(whoami) .

Accelerate answered 13/12, 2020 at 5:30 Comment(3)
explain.. this didn't do anything to me..Griffy
@Griffy Thank you for your query. Basically, I was working on a laravel project in visual studio code and I got that storage issue. And then I open the visual studio code *terminal and executed those commands and it worked for me.Accelerate
Thank you! I tried all the others answers but this one helped me!Venita
D
0

I got this bug on 1 of my projects after updating macos in 2024.

Changing permissions did nothing. I had to update homestead and vagrant which I haven't done in ages.

This is what worked for me

cd Homestead

vagrant --update

vagrant destroy

git pull origin main

git add .

git commit -m "updating homestead 2024"

composer clear-cache

composer install

git add .

git commit -m "updating homestead 2024"

// update vagrant 

brew tap hashicorp/tap 

brew install hashicorp/tap/hashicorp-vagrant

vagrant box update

vagrant up


Doloroso answered 26/5 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.