You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Symfony 3.4.10应用遇StreamHandler.php内存耗尽错误求助

解决Symfony 3.4生产环境Nginx+PHP-FPM内存耗尽问题

Hey there, let's break down this confusing memory exhaustion issue you're hitting. Even though you've set a 1GB memory limit, the error says only 20KB was requested—this usually points to a recursive loop or misconfiguration rather than a single large memory allocation. Here are the most likely fixes:

1. Verify PHP-FPM is actually using your 1GB memory limit

First, let's make sure your php.ini change is taking effect for PHP-FPM (it's common to edit the wrong ini file!).

  • Create a temporary phpinfo.php file in your web root:
    <?php phpinfo(); ?>
    
  • Access it via your browser and search for memory_limit. If it's not showing 1024M, you probably modified the CLI php.ini instead of the FPM one.
    • For PHP 7.0 FPM, the correct file is usually /etc/php/7.0/fpm/php.ini
    • Update memory_limit = 1024M there, then restart PHP-FPM:
      sudo systemctl restart php7.0-fpm
      
  • Don't forget to delete the phpinfo.php file after checking!

2. Fix potential Monolog recursive logging loop

The error points to StreamHandler.php, which suggests a problem with your logging setup that's causing an infinite loop:

  • Check log directory permissions: Symfony's var/log directory (and any other log paths you're using) needs write access for the PHP-FPM user (usually www-data):
    sudo chown -R www-data:www-data /var/www/my-project/application/var
    sudo chmod -R 755 /var/www/my-project/application/var
    
  • If logs are being written to system directories (like /var/log/nginx), ensure www-data has write access there too, or adjust your Monolog config to use a path the user can write to.
  • A recursive loop happens when writing to the log fails, and Monolog tries to log that failure—creating an endless cycle that eats up memory until the limit is hit. Fixing permissions usually stops this.

3. Ensure you're running Symfony in production mode

Your Nginx config doesn't set the APP_ENV parameter, which means Symfony might be defaulting to dev mode—even in production! Dev mode loads heavy debugging tools (like the Web Profiler) that drastically increase memory usage.

  • Update your Nginx config's location ~ ^/app\.php(/|$) block to add these fastcgi params:
    fastcgi_param APP_ENV prod;
    fastcgi_param APP_DEBUG 0;
    
  • Restart Nginx after making the change:
    sudo systemctl restart nginx
    
  • Also, make sure you've cleared and warmed up the production cache:
    cd /var/www/my-project/application
    php bin/console cache:clear --env=prod --no-debug
    php bin/console cache:warmup --env=prod --no-debug
    

4. Remove dev dependencies from production

If you ran composer install without the --no-dev flag in production, you're loading unnecessary dev packages (like PHPUnit, DebugBundle) that add memory overhead:

  • Run this in your project root to clean up dev dependencies:
    composer install --no-dev --optimize-autoloader
    

5. Troubleshoot with memory tracking (if needed)

If the above steps don't work, you can enable more detailed memory logging to spot where the leak is:

  • Add this to your app.php (front controller) to log memory usage at key points:
    error_log("Memory usage start: " . memory_get_usage(true));
    // ... after kernel boot
    error_log("Memory usage after kernel boot: " . memory_get_usage(true));
    
  • Check your PHP error log for these entries to see where memory is spiking.

内容的提问来源于stack exchange,提问作者darckcrystale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:37:49