2

Context

I am trying to deploy a fresh Symfony 3.4.10 application. It's the first time I deploy a Symfony application. It runs well in dev environment using the built-in php server.

I use nginx as webserver and php-fpm.

Problem

When I try to reach my application, an error is thrown in my logs saying:

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"

The page I try to load is just a connection form.

I allocated 1024MB in my php.ini, thinking it was because the app needed more RAM on the first call.

What seems really strange is that the log is telling the memory size is exhausted but the amount the app was trying to allocate is really low: 20480 bytes allocated for a maximum amount of 1073741824 bytes, I don't understand why it fails and how to solve this.

My website nginx configuration:

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;
}
darckcrystale
  • 1,582
  • 2
  • 18
  • 40
  • Is it at all possible you modified the wrong `php.ini`? Oftentimes there are different configs for different sapis (cli, apache module, php-fpm). Also, reload php-fpm after updating! – Cameron Hurd May 25 '18 at 00:57
  • At the first time I did edit the wrong one (so the `Allowed memory size of xxx bytes` was not changing, then I managed to find the good one, to edit it and the message changed to `Allowed memory size of 1073741824 bytes` (which is 1024MB). But thx for the question, it's often the problem indeed! – darckcrystale May 25 '18 at 01:10
  • Nice! Good sleuthing. :) – Cameron Hurd May 25 '18 at 13:37

2 Answers2

5

Ok, so I've figured how to make my application works! It was a problem of rights on my files, the user of php-fpm could not access the files and this resulted in Symfony trying to send an error.

I don't know why the exception thrown by Symfony LogicException: Missing stream url, the stream can not be opened. This may be caused by a premature call to close(). overloaded the memory limit so if someone has a clue on why, it would be super helpful to explain it!

darckcrystale
  • 1,582
  • 2
  • 18
  • 40
  • 1
    You exhausted your memory because your rights to write the error log were insufficient and raised an error that was going to be wrote on your error log. Guess what, you hadn't the rights to write in it and... recursive loop and memory exhaustion. – Luca Ricci Jul 21 '22 at 12:07
0

This makes sense:

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

That extra 20480b put it right at the limit.

You could always up the setting in php.ini but the real question is why does it consume that much memory? Is it common for Symfony to consume that much? Also if there is some type of leak or unbounded memory consuming process it will likely crash on an increased amount of memory at some point.

d g
  • 1,594
  • 13
  • 13
  • I think there is a memory leak indeed but the error thrown says it comes from `/var/www/my-project/application/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php`, that's not one of my files but a Symfony one, I've never edited it. – darckcrystale May 25 '18 at 06:28
  • So there would be something right before the call to this file which would consume almost the 1024MB and that would be the call to this file which would exceed the memory limit? – darckcrystale May 25 '18 at 06:29