34

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

Pawan Rai
  • 581
  • 1
  • 5
  • 18
  • 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. – ChewySalmon Aug 01 '20 at 08:37
  • Checked from workspace container bash. storage/logs/ directory has `drwxr-xr-x 2 root root 4096 Aug 1 07:37 logs ` – Pawan Rai Aug 01 '20 at 09:02
  • Please see this as well: https://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. – Kurt Friars Aug 01 '20 at 10:46

6 Answers6

62

This worked for me:

chown -R www-data:www-data "project foldername"
Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
MARIO MORENO
  • 721
  • 4
  • 3
  • when i paste this in my terminal in cpanel server it says "chown: invalid user: ‘www-data:www-data’" – ehsan asarzadeh Jul 21 '21 at 11:36
  • 1
    @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. – Calamity Jane Jul 23 '21 at 09:14
  • 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. – Farhat Aziz Jan 20 '23 at 06:00
14

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!

Vivek Choudhary
  • 634
  • 8
  • 14
  • 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 setfacl – Annemieke Buijs Nov 30 '22 at 07:47
10

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
I B R A
  • 3
  • 1
Anjana Silva
  • 8,353
  • 4
  • 51
  • 54
  • 12
    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 775 – stanley mbote Jun 23 '21 at 15:20
  • @stanleymbote - Granting access to a single `.log` file? – Anjana Silva Jun 28 '21 at 07:49
  • 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 them – Flame Aug 03 '22 at 21:20
9

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?

ChewySalmon
  • 576
  • 5
  • 22
4

Give group write access to /storage/logs/

sudo chmod g+w storage/logs
zEELz
  • 171
  • 6
3

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

step 1:

sudo chmod -R +rwX .

step 2:

sudo chown -R $(whoami) .

Najmul Hoq
  • 143
  • 1
  • 11
  • explain.. this didn't do anything to me.. – francisco Jan 15 '22 at 14:37
  • @francisco 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. – Najmul Hoq Jan 16 '22 at 14:33
  • Thank you! I tried all the others answers but this one helped me! – Bruno Aug 02 '22 at 18:46