【问题标题】:Deploy Laravel on Google App Engine - Flexible在 Google App Engine 上部署 Laravel - 灵活
【发布时间】:2020-04-19 18:41:17
【问题描述】:

将 Laravel 6 部署到 Google App Engine 后,运行 url https://PROJECT_ID.appspot.com/ 时出现此错误。

The stream or file "/app/storage/logs/laravel-2019-12-31.log" could not be opened: failed to open stream: Permission denied

我已按照https://cloud.google.com/community/tutorials/run-laravel-on-appengine-flexible2 上的说明进行操作

这是我的 app.yaml 文件

runtime: php  # language of the app
env: flex     # let app engine know we use flexible environment
runtime_config:
 document_root: .   #folder where index.php is
# Ensure we skip ".env", which is only for local development
skip_files:
 - .env #we want to skip this to make sure we don’t mess stuff up on the server
env_variables:
 # Put production environment variables here.
 APP_ENV: production   # or production
 APP_DEBUG : true # or false 
 APP_KEY: YOUR_API_KEY
#go to generate app key paragraf in this tutorial
 CACHE_DRIVER: database
# instead of putting the cache in the database I recommend using redis
 SESSION_DRIVER: database #or file since both work
 APP_LOG: errorlog
 APP_TIMEZONE: UTC #your timezone of choice
# follow the part of the tutorial on setting up your SQL database
 DB_CONNECTION: mysql
 DB_HOST: localhost
 DB_DATABASE: XXX
 DB_USERNAME: XXX
 DB_PASSWORD: XXX
 DB_SOCKET: /cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME
 QUEUE_DRIVER: database #in case you execute queued jobs
 MAIL_DRIVER: mail
 MAIL_HOST: smtp.sparkpostmail.com
 MAIL_PORT: 587
 MAIL_USERNAME: null
 MAIL_PASSWORD: null
 LOG_DELETE:  true # this is a parameter added by us in the project .env file. You can add here any setting you would add to your .env file
 GOOGLE_VISION_PROJECT_ID : PROJECT_ID
#we need this for the flex environment
beta_settings:
   # for Cloud SQL, set this value to the Cloud SQL connection name,
   # e.g. "project:region:cloudsql-instance"
   cloud_sql_instances: "YOUR_CLOUDSQL_CONNECTION_NAME"  

composer.json

"scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ],
        "post-install-cmd": [
           "Illuminate\\Foundation\\ComposerScripts::postInstall",
           "@php artisan optimize",
           "chmod -R 755 storage bootstrap\/cache"
       ],
       "post-update-cmd": [
           "Illuminate\\Foundation\\ComposerScripts::postUpdate",
           "@php artisan optimize"
       ]
    }

我已经在本地机器上进行了测试,并且可以成功运行。

【问题讨论】:

    标签: laravel google-app-engine google-cloud-platform laravel-6


    【解决方案1】:

    无法打开流或文件“/app/storage/logs/laravel-2019-12-31.log”:无法打开流:权限被拒绝

    正如post 中提到的,这个错误背后的原因可能是因为directory permissions

    存储中的目录和 bootstrap/cache 目录应该可以被您的 Web 服务器写入,否则 Laravel 将无法运行。

    您将在这两个帖子之后找到有关它的更多信息:

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      在 App Engine Flex 环境中,您无法写入文件,因为文件系统是只读的。不过有一个很好的解决方案,因为您可以将 Stackdriver Logging 集成到 Laravel 中。

      您需要执行以下命令:

      composer require google/cloud-logging google/cloud-error-reporting
      

      另外你需要更改app/Exceptions/Handler.php文件中的report函数:

      /**
       * Report or log an exception.
       *
       * @param  \Exception  $exception
       * @return void
       */
      public function report(Exception $exception)
      {
          if (isset($_SERVER['GAE_SERVICE'])) {
              Bootstrap::init();
              Bootstrap::exceptionHandler($exception);
          } else {
              parent::report($exception);
          }
      }
      

      需要在文件顶部添加use Google\Cloud\ErrorReporting\Bootstrap;

      在您的app.yaml 中,您需要将以下内容添加到env_variables

      LOG_CHANNEL: stackdriver
      

      logging.php 中,您需要将以下内容添加到'channels' 数组中:

      'stackdriver' => [
          'driver' => 'custom',
          'via' => App\Logging\CreateStackdriverLogger::class,
          'level' => 'debug',
      ],
      

      您需要做的最后一件事是在app/Logging 目录中创建CreateStackdriverLogger 类:

      <?php
      
      namespace App\Logging;
      
      use Google\Cloud\Logging\LoggingClient;
      use Monolog\Handler\PsrHandler;
      use Monolog\Logger;
      
      class CreateStackdriverLogger
      {
          /**
           * Create a custom Monolog instance.
           *
           * @param array $config
           * @return \Monolog\Logger
           */
          public function __invoke(array $config)
          {
              $logName = isset($config['logName']) ? $config['logName'] : 'app';
              $client = new LoggingClient([
                  'projectId' => 'YOUR-PROJECT-ID'
              ]);
      
              $psrLogger = $client->psrLogger($logName);
      
              $handler = new PsrHandler($psrLogger);
              $logger = new Logger($logName, [$handler]);
      
              return $logger;
          }
      }
      

      现在您的所有日志都将转到 Stackdriver 系统。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-23
        • 1970-01-01
        • 2018-07-15
        • 2019-01-04
        • 2023-03-02
        • 1970-01-01
        • 2018-08-31
        • 1970-01-01
        相关资源
        最近更新 更多