调试 Linux 下的 LAMP(Linux + Apache + MySQL/MariaDB + PHP)环境,通常按“分层排查”的思路来:网络 → Apache → PHP → 数据库 → 应用日志。下面给你一套实用调试流程。
systemctl status httpd # CentOS/RHEL
systemctl status apache2 # Ubuntu/Debian
systemctl status mysqld # 或 mariadb
systemctl status php-fpm # 如果使用 PHP-FPM
没启动就启动:
systemctl start httpd
systemctl enable httpd
# 防火墙
firewall-cmd --list-all
# 或
ufw status
# 放行 80/443
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
SELinux(CentOS):
getenforce
setenforce 0 # 临时关闭测试
apachectl configtest
或
httpd -t
tail -f /var/log/httpd/error_log
# Ubuntu
tail -f /var/log/apache2/error.log
curl http://localhost
创建测试文件:
<?php
phpinfo();
访问后看是否有 PHP 信息。
编辑 php.ini:
display_errors = On
error_reporting = E_ALL
重启服务:
systemctl restart httpd
tail -f /var/log/php-fpm/error.log
# 或
tail -f /var/log/php_errors.log
mysql -u root -p
tail -f /var/log/mysqld.log
# 或
tail -f /var/log/mysql/error.log
SELECT user, host FROM mysql.user;
SHOW GRANTS FOR 'user'@'localhost';
(如 WordPress / Laravel / ThinkPHP)
strace -f -p $(pgrep httpd | head -1)
| 问题 | 原因 |
|---|---|
| 403 Forbidden | 目录权限 / SELinux |
| 500 Error | PHP 语法 / 权限 |
| 连接数据库失败 | 用户权限 / socket |
| 页面空白 | PHP 错误未显示 |
| 慢 | 未开 OPcache |
tail -f /var/log/httpd/error_log
tail -f /var/log/php-fpm/error.log
curl -I http://localhost
如果你愿意,可以告诉我:
我可以直接帮你定位问题。