在 CentOS 上搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP)环境时,调试错误通常分为系统层、服务层、Web 层、代码层几个方向。下面给你一套实用调试流程,适合大多数情况。
systemctl status httpd
systemctl status mariadb # 或 mysqld
systemctl status php-fpm # 若使用 PHP-FPM
启动服务:
systemctl start httpd
systemctl enable httpd
/var/log/httpd/error_log
/var/log/httpd/access_log
实时查看错误:
tail -f /var/log/httpd/error_log
常见错误示例:
Permission denied → 文件/目录权限问题File does not exist → 路径错误PHP Parse error → PHP 代码问题编辑 php.ini:
vi /etc/php.ini
修改:
display_errors = On
error_reporting = E_ALL
log_errors = On
重启 Apache:
systemctl restart httpd
/var/log/php/php_error.log
(路径由 error_log 决定)
systemctl status mariadb
mysql -u root -p
/var/log/mariadb/mariadb.log
Access denied for user → 账号/权限问题Can't connect to MySQL server → 服务未启动或端口问题chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html
SELinux 相关问题:
getenforce
临时关闭测试:
setenforce 0
永久允许 httpd 访问:
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_unified 1
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
检查:
httpd -M | grep php
若没有 php 模块:
yum install php php-mysql
systemctl restart httpd
如果你愿意,可以把具体报错信息或场景(例如 500 错误、白屏、数据库连接失败)发出来,我可以直接帮你定位。