温馨提示×

centos php日志怎么查内存溢出

小樊
34
2026-09-24 08:50:16
栏目: 编程语言

CentOS 上排查 PHP 内存溢出(Memory Exhausted / Allowed memory size exhausted),一般按下面几步来:


一、先看 PHP 错误日志(最常用)

1. 找到 php 错误日志位置

php -i | grep error_log

或看 php.ini:

vim /etc/php.ini

常见位置:

/var/log/php-fpm/error.log
/var/log/php_errors.log
/var/log/httpd/error_log
/var/log/nginx/error.log

2. 查看内存溢出错误

tail -f /var/log/php-fpm/error.log

grep -i "memory" /var/log/php-fpm/error.log

典型错误:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted

二、临时确认是不是内存溢出

1. 查看当前内存限制

php -i | grep memory_limit

2. 临时调大(测试用)

php -d memory_limit=512M script.php

或在 php.ini:

memory_limit = 512M

三、Web 环境(php-fpm / nginx / apache)

php-fpm

tail -f /var/log/php-fpm/www-error.log

nginx

tail -f /var/log/nginx/error.log

apache

tail -f /var/log/httpd/error_log

四、代码中定位内存溢出位置

1. 打开错误显示(开发环境)

php.ini:

display_errors = On
error_reporting = E_ALL

2. 在代码中打点

echo memory_get_usage(true);

3. 用 xhprof / tideways(推荐)

yum install php-pecl-xhprof

分析函数级内存占用。


五、常见导致内存溢出的原因

  • 大数组 / 大循环
  • 一次读取大文件(如 file_get_contents
  • 无限递归
  • ORM 一次查全表
  • 闭包未释放

六、快速排查命令汇总

# 查日志
tail -f /var/log/php-fpm/error.log

# 查内存限制
php -i | grep memory_limit

# 临时加大内存运行
php -d memory_limit=512M index.php

如果你愿意,可以告诉我:

  • PHP 版本
  • 是 CLI 还是 Web(nginx/apache)
  • 报错原文

我可以直接帮你定位是哪一行代码或配置问题。

0