Deploying Symfony 3.4.10 application error: Allowed memory size exhausted by StreamHandler.php

Deploying Symfony 3.4.10 application error: Allowed memory size exhausted by StreamHandler.php

上下文

我正在尝试部署新的 Symfony 3.4.10 应用程序。这是我第一次部署 Symfony 应用程序。它在使用内置 php 服务器的开发环境中运行良好。

我使用 nginx 作为网络服务器和 php-fpm。

问题

当我尝试访问我的应用程序时,我的日志中出现错误:

2018/05/25 02:32:03 [error] 15819#15819: *1 FastCGI sent in stderr: 
    "PHP message: PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted 
    (tried to allocate 20480 bytes) 
    in /var/www/my-project/application/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php 
    on line 107
PHP message: PHP Fatal error: 
    Allowed memory size of 1073741824 bytes exhausted 
    (tried to allocate 20480 bytes) 
    in /var/www/my-project/application/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php 
    on line 107"
    while reading response header from upstream, 
    client: xx.xx.xx.xx, server: preprod.my-website.fr, 
    request: "GET / HTTP/2.0", 
    upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", 
    host: "preprod.mywebsite.fr"

我尝试加载的页面只是一个连接表单。

我在 php.ini 中分配了 1024MB,认为这是因为应用程序在第一次调用时需要更多 RAM。

真正奇怪的是,日志显示内存大小已耗尽,但应用程序尝试分配的内存量非常低:20480 bytes 已分配最大内存量 1073741824 bytes,我不明白为什么它会失败以及如何解决这个问题。

我的网站nginx配置:

server {
    listen 443;
    listen [::]:443;

    server_name preprod.my-website.fr;
    root /var/www/my-project/application/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }

    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/my-project_error.log;
    access_log /var/log/nginx/my-project_access.log;

    # SSL configuration
    ssl on;
    ssl_certificate /etc/letsencrypt/live/preprod.my-website.fr/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/preprod.my-website.fr/privkey.pem;
}

server {
    listen 0.0.0.0:80;
    server_name preprod.my-website.fr;
    rewrite ^ https://$host$request_uri? permanent;
}

这是有道理的:

(1073741824b + 20480b)/1024/1024 = 1024mb

额外的 20480b 使其达到了极限。

您总是可以在 php.ini 中进行设置,但真正的问题是为什么它会消耗那么多内存? Symfony 消耗那么多是不是很常见?此外,如果存在某种类型的泄漏或无限内存消耗过程,它可能会在某个时候因内存量增加而崩溃。

好的,我已经想出如何使我的应用程序正常工作了!这是我文件的权限问题,php-fpm 的用户无法访问这些文件,这导致 Symfony 尝试发送错误。

我不知道为什么 Symfony 抛出的异常 LogicException: Missing stream url, the stream can not be opened. This may be caused by a premature call to close(). 超载了内存限制,所以如果有人知道为什么,解释它会非常有帮助!