在 CentOS 上排查 PHP 内存溢出(Memory Exhausted / Allowed memory size exhausted),一般按下面几步来:
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
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
php -i | grep memory_limit
php -d memory_limit=512M script.php
或在 php.ini:
memory_limit = 512M
tail -f /var/log/php-fpm/www-error.log
tail -f /var/log/nginx/error.log
tail -f /var/log/httpd/error_log
php.ini:
display_errors = On
error_reporting = E_ALL
echo memory_get_usage(true);
yum install php-pecl-xhprof
分析函数级内存占用。
file_get_contents)# 查日志
tail -f /var/log/php-fpm/error.log
# 查内存限制
php -i | grep memory_limit
# 临时加大内存运行
php -d memory_limit=512M index.php
如果你愿意,可以告诉我:
我可以直接帮你定位是哪一行代码或配置问题。