CentOS 下 PHP 报错 的原因非常多,不能一概而论。下面我按最常见原因 → 排查方法 → 解决思路给你一个系统性的说明,你可以对照排查。
一、PHP 日志常见报错位置
1️⃣ PHP 错误日志
/var/log/php-fpm/error.log
/var/log/php/error.log
或(取决于版本):
/var/log/php-fpm/www-error.log
2️⃣ Nginx / Apache 日志
/var/log/nginx/error.log
/var/log/httpd/error_log
3️⃣ PHP 应用自身日志
如 Laravel、ThinkPHP:
storage/logs/
runtime/log/
二、CentOS 中最常见的 PHP 报错原因
✅ 1. PHP 版本不兼容
现象
PHP Fatal error: Uncaught Error: Call to undefined function
或
This version of PHP is not supported
原因
- CentOS 自带 PHP 版本较老(5.4 / 7.2)
- 新程序需要 PHP 7.4 / 8.x
排查
php -v
解决
yum install epel-release -y
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install yum-utils
yum-config-manager --enable remi-php81
yum install php php-fpm
✅ 2. PHP-FPM 没启动 / 端口错误
现象
502 Bad Gateway
connect() failed (111: Connection refused)
排查
systemctl status php-fpm
解决
systemctl start php-fpm
systemctl enable php-fpm
检查监听方式:
listen = /run/php-fpm/www.sock
或
listen = 9000
✅ 3. 权限问题(最常见)
现象
Permission denied
mkdir(): Permission denied
原因
- PHP-FPM 运行用户(apache / nginx)无权写目录
排查
ps aux | grep php-fpm
解决
chown -R nginx:nginx /var/www/html
chmod -R 755 /var/www/html
⚠️ 不要直接 777
✅ 4. SELinux 阻止(CentOS 特有)
现象
- 日志里没明显错误
- 但访问失败 / 写文件失败
排查
getenforce
临时关闭测试
setenforce 0
永久解决(推荐)
setsebool -P httpd_read_user_content 1
setsebool -P httpd_can_network_connect 1
✅ 5. php.ini 配置问题
常见错误
upload_max_filesize
post_max_size
memory_limit
排查
php -i | grep php.ini
修改
vim /etc/php.ini
upload_max_filesize = 50M
post_max_size = 50M
memory_limit = 256M
重启:
systemctl restart php-fpm
✅ 6. 缺少 PHP 扩展
现象
Class 'PDO' not found
undefined function mb_strlen()
解决
yum install php-pdo php-mysql php-mbstring php-json
systemctl restart php-fpm
✅ 7. 代码错误
现象
PHP Parse error
PHP Fatal error
解决
- 打开 PHP 错误显示(开发环境)
display_errors = On
error_reporting = E_ALL
三、快速定位问题(推荐流程)
# 1. 看 PHP 错误
tail -f /var/log/php-fpm/error.log
# 2. 看 Web 服务错误
tail -f /var/log/nginx/error.log
# 3. 看 PHP 版本
php -v
# 4. 看 PHP-FPM 状态
systemctl status php-fpm
四、如果你愿意,我可以直接帮你定位
你可以直接把 具体报错内容 发出来,例如:
PHP Fatal error: Allowed memory size exhausted
或
502 Bad Gateway
我可以直接告诉你 原因 + 精确解决方案 ✅
