skip to Main Content

After deploying Laravel 6 to Google App Engine, got this error when run the 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

enter image description here

I have followed the instrucation given on https://cloud.google.com/community/tutorials/run-laravel-on-appengine-flexible2

Here is my app.yaml file

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"
       ]
    }

I have Tested on local machine and its working successfully.

2

Answers


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

    As mentioned in this post, the reason behind this error could be because of directory permissions:

    Directories within the storage and the bootstrap/cache directories should be writable by your web server or Laravel will not run.

    You will find more information about it following this two posts:

    I hope it helps.

    Login or Signup to reply.
  2. In the App Engine Flex environment you can’t write files because the file system is read only. There is neat solution though since you can integrate Stackdriver Logging into Laravel.

    You need to execute the following command:

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

    Additionally you need to change the report function in the app/Exceptions/Handler.php file:

    /**
     * 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);
        }
    }
    

    At the top of the file you need to add use GoogleCloudErrorReportingBootstrap;

    In your app.yaml you need to add the following to the env_variables:

    LOG_CHANNEL: stackdriver
    

    In the logging.php you need to add the following to the 'channels' array:

    'stackdriver' => [
        'driver' => 'custom',
        'via' => AppLoggingCreateStackdriverLogger::class,
        'level' => 'debug',
    ],
    

    The last thing you need to do is create the CreateStackdriverLogger class in the app/Logging directory:

    <?php
    
    namespace AppLogging;
    
    use GoogleCloudLoggingLoggingClient;
    use MonologHandlerPsrHandler;
    use MonologLogger;
    
    class CreateStackdriverLogger
    {
        /**
         * Create a custom Monolog instance.
         *
         * @param array $config
         * @return MonologLogger
         */
        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;
        }
    }
    

    Now all your logs go to the Stackdriver system.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search