Laravel 和 Docker:无法打开流或文件“/var/www/html/storage/logs/laravel.log”:无法打开流:权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50552970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:45:26  来源:igfitidea点击:

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

laraveldockerdocker-compose

提问by ltdev

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

这是我的 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

这是我的 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 -dand try to access the Laravel app through the htts://localhost:8000I get this error on my screen

当我运行docker-compose up -d并尝试通过htts://localhost:8000我的屏幕上出现此错误来访问 Laravel 应用程序时

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 ./srcfrom my root-app-dirbut the issue is still there. How can I fix this?

我已经逃离sudo chown lykos:lykos -R ./src了我的,root-app-dir但问题仍然存在。我怎样才能解决这个问题?

回答by Tooschee

There are several options, what could be wrong:

有几种选择,可能是什么问题:

  1. The storagelink isn't properly set, you can read about it here, after launching appcontainer, you should do php artisan storage:linkto check whether proper link is there
  2. You don't have the proper right for the given folder, from default, logsfolder should have www-datawritable rights, you can check that by ssh to machine and the do ls -lon 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: sestatusand then enforcing it to 0by 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]
  1. storage链接设置不正确,你可以看到它在这里,发射后app的容器,你应该做的php artisan storage:link检查适当的链接是否存在
  2. 您没有给定文件夹的正确权限,默认情况下,logs文件夹应该具有www-data可写权限,您可以通过 ssh 来检查机器和ls -l对文件的操作/var/www/html/[your laravel app],如果没有,请通过以下方式添加正确的权限:chown -R www-data:www-data *
  3. 也涉及到文件权限:您可以通过cehcking状态禁用SELinux(但只能在开发服务器,从来没有做到这一点活)sestatus,然后执行,以0通过setenforce 0(不论活的服务器上,这是更好地让SELinux的运行,并尝试禁用问题由chcon -R -t httpd_sys_rw_content_t [path to storage folder here]

回答by popoyvargas

from Tooschee's answer,
chcon -R -t httpd_sys_rw_content_t [path to storage folder here]
worked for me.

来自 Tooschee 的回答,
chcon -R -t httpd_sys_rw_content_t [path to storage folder here]
对我来说有效。