18

This is my folder structure:

+-- root-app-dir
|   +-- docker
|   |   +-- Dockerfile
|   |   +-- nginx.conf
|   +-- src // this is where the Laravel app lives
|   +-- docker-compose.yml

This is my docker-compose.yml

version: '2'

services:
  app:
    build:
      context: ./docker
      dockerfile: Dockerfile
    image: lykos/laravel
    volumes:
      - ./src/:/var/www/html/
    networks:
      - app-network

  nginx:
    image: nginx:latest
    volumes:
      - ./src/:/var/www/html/
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8000:80
    networks:
      - app-network

  mysql:
    image: mysql:5.7
    ports: 
      - "4306:3306"
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - mysqldata:/var/lib/mysql
    networks:
      - app-network

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    links:
      - mysql
    environment:
      PMA_HOST: mysql

volumes:
  mysqldata:
    driver: "local"

networks:
  app-network:
    driver: "bridge"

This is my nginx.conf

server {
  root /var/www/html/public;

  index index.html index.htm index.php;

  server_name _;
  charset utf-8;

  location = /favicon.ico { log_not_found off; access_log off; }
  location = /robots.txt { log_not_found off; access_log off; }

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location ~ \.php$ {
    # include snippets/fastcgi-php.conf;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_pass app:9000;
    fastcgi_index index.php;
  }

  error_page 404 /index.php;

  location ~ /\.ht {
    deny all;
  }
}

When I run docker-compose up -d and try to access the Laravel app through the htts://localhost:8000 I get this error on my screen

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

I have run sudo chown lykos:lykos -R ./src from my root-app-dir but the issue is still there. How can I fix this?

ltdev
  • 3,461
  • 17
  • 60
  • 117

6 Answers6

29

There are several options, what could be wrong:

  1. The storage link isn't properly set, you can read about it here, after launching app container, you should do php artisan storage:link to check whether proper link is there
  2. You don't have the proper right for the given folder, from default, logs folder should have www-data writable rights, you can check that by ssh to machine and the do ls -l on files in /var/www/html/[your laravel app], if not, add the proper rights by: chown -R www-data:www-data *
  3. Also related to file permissions: you can disable SELinux (but only in dev server, never do it on live) by cehcking the status: sestatus and then enforcing it to 0 by setenforce 0 (however on live server, it's better to leave SElinux running and try to disable the problem by chcon -R -t httpd_sys_rw_content_t [path to storage folder here]
Caleb
  • 122,179
  • 19
  • 178
  • 268
Tooschee
  • 531
  • 4
  • 8
  • I am having "chcon: can't apply partial context to unlabeled file" when I do chcon -R -t httpd_sys_rw_content_t – Utku Dalmaz Nov 02 '20 at 06:36
  • 1
    Note that the container would create `/var/www/html/storage/logs/laravel.log` by `root:root` hence you should run `chown -R www-data:www-data .` on root of the project. Next, the PHP would append logs to the mentioned file obviously by `www-data:www-data`. – M. Rostami Nov 08 '20 at 14:21
  • The third one saved me. thanks – M.Arjmandi May 14 '22 at 06:41
27

if the above does not work when its added to your .env file the following will:

sudo chmod o+w ./storage/ -R

this will give others permissions without altering user and group permissions.

Dharman
  • 26,923
  • 21
  • 73
  • 125
Developer Jay
  • 555
  • 1
  • 7
  • 14
  • This is just a disingenuous way of using `chmod 777`. It's very dangerous, and allows **anyone** to alter the files served to end users. – miken32 Jan 10 '22 at 16:51
15

if you are using laravel sail, add following to your .env file

WWWGROUP=1000
WWWUSER=1000
mischkez
  • 318
  • 4
  • 8
11

If you're using Sail on Docker Windows Desktop with WSL2 this issue arises when you start the docker container directly from the dashboard. Instead, start the container within your WSL2 terminal instance i.e

sail up -d
Rob Mascaro
  • 617
  • 5
  • 15
0
  1. With user root:root . You try with command : sudo chmod -R 777 storage/
  2. With user www-data:www-data . You try with command :
    • sudo chown -R www-data:www-data storage/
    • sudo chmod -R 755 storage/
  3. If you use Docker . You try with command :
    • sudo chown -R nginx:nginx storage/
    • sudo chmod -R 755 storage/
Nam Sama
  • 45
  • 3
0

I had this issue because I was using sail and the default directory for the logs thinks I am using Apache. I could not find any way of changing the log location to the correct storage folder, so I created a symbolic link instead:

ln -s /var/www/html/public/storage /home/<yourfoldername>/storage

When I ran my script again, it errored properly, and wrote to laravel.log file in the correct location

Muhammad Dyas Yaskur
  • 5,382
  • 10
  • 37
  • 61