apache 运行Artisan命令时出现拒绝目录权限错误

pdsfdshx  于 2023-10-23  发布在  Apache
关注(0)|答案(2)|浏览(132)

我用laravel和apache以及mysql安装了docker,当我试图在vscode的终端中运行artisan命令时,我得到了:
在“/var/www/html/storage/logs”中没有现有目录,并且无法构建:权限被拒绝
Docker compose中的Apache设置:

  1. laravel-app:
  2. build:
  3. context: ./docker/app
  4. args:
  5. uid: ${UID}
  6. container_name: laravel-app
  7. environment:
  8. - APACHE_RUN_USER=#${UID}
  9. - APACHE_RUN_GROUP=#${UID}
  10. volumes:
  11. - .:/var/www/html
  12. ports:
  13. - ${HOST_PORT}:80
  14. networks:
  15. backend:
  16. aliases:
  17. - laravel-app

Apache的Dockerfile

  1. FROM php:7.2-apache
  2. RUN apt-get update
  3. # 1. development packages
  4. RUN apt-get install -y \
  5. git \
  6. zip \
  7. curl \
  8. sudo \
  9. unzip \
  10. libicu-dev \
  11. libbz2-dev \
  12. libpng-dev \
  13. libjpeg-dev \
  14. libmcrypt-dev \
  15. libreadline-dev \
  16. libfreetype6-dev \
  17. g++
  18. # 2. apache configs + document root
  19. ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
  20. RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
  21. RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
  22. # 3. mod_rewrite for URL rewrite and mod_headers for .htaccess extra headers like Access-Control-Allow-Origin-
  23. RUN a2enmod rewrite headers
  24. # 4. start with base php config, then add extensions
  25. RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
  26. RUN docker-php-ext-install \
  27. bz2 \
  28. intl \
  29. iconv \
  30. bcmath \
  31. opcache \
  32. calendar \
  33. mbstring \
  34. pdo_mysql \
  35. zip
  36. # 5. composer
  37. COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
  38. # 6. we need a user with the same UID/GID with host user
  39. # so when we execute CLI commands, all the host file's ownership remains intact
  40. # otherwise command from inside container will create root-owned files and directories
  41. ARG uid
  42. RUN useradd -G www-data,root -u $uid -d /home/devuser devuser
  43. RUN mkdir -p /home/devuser/.composer && \
  44. chown -R devuser:devuser /home/devuser

即使目录存在,命令也可以从容器中成功运行。我应该总是从容器中运行与laravel artisan相关的命令,还是有什么问题?

pw136qt2

pw136qt21#

转到项目文件夹并打开终端,然后运行以下命令

  1. sudo chmod -R 775 storage
nwwlzxa7

nwwlzxa72#

通过更改sail laravel的start-container解决了我的问题

  1. #!/usr/bin/env bash
  2. if [ ! -z "$WWWUSER" ]; then
  3. usermod -u $WWWUSER sail
  4. fi
  5. // Edit this to match the error "The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied"
  6. // We need three files inside storage. Note: no file named storage by default so we shall create
  7. if [ ! -d /. storage]; then
  8. mkdir /.storage
  9. cd storage/
  10. mkdir -p framework/{sessions,views,cache}
  11. cd ..
  12. fi
  13. chmod -R ugo+rw /.storage
  14. if [ ! -d /.composer ]; then
  15. mkdir /.composer
  16. fi
  17. chmod -R ugo+rw /.composer
  18. if [ $# -gt 0 ]; then
  19. exec gosu $WWWUSER "$@"
  20. else
  21. /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
  22. fi

这意味着我们创建存储文件夹并拥有缓存权限

展开查看全部

相关问题