在 Debian 上调试 LAMP(Linux + Apache + MySQL/MariaDB + PHP)错误,通常可以按“分层排查”的思路进行。下面是一套比较系统的调试方法。
sudo systemctl status apache2
启动 / 重启 / 开机自启:
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl enable apache2
查看是否监听 80/443:
sudo ss -tlnp | grep apache
sudo systemctl status mariadb # 或 mysql
登录测试:
mysql -u root -p
php -v
确认 Apache 已加载 PHP 模块(以 PHP 8.2 为例):
ls /etc/apache2/mods-enabled/ | grep php
如未启用:
sudo a2enmod php8.2
sudo systemctl restart apache2
sudo tail -n 50 /var/log/apache2/error.log
访问日志:
sudo tail -n 50 /var/log/apache2/access.log
编辑 php.ini(路径可能不同):
php --ini
常见设置:
display_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php_errors.log
重启 Apache 后查看:
sudo tail -f /var/log/php_errors.log
sudo tail -n 50 /var/log/mysql/error.log
可能原因:
Require all granted检查:
ls -l /var/www/html
Apache 配置示例:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
/var/www/htmlDocumentRootsudo a2enmod rewrite
.phpPHP 中常见错误:
SQLSTATE[HY000] [2002] No such file or directory
说明使用了 localhost,但 MySQL 用 socket 连接。
解决:
127.0.0.1ini_set('display_errors', 1);
error_reporting(E_ALL);
sudo apache2ctl configtest
apache2ctl -M
<?php
phpinfo();
?>
如果你能贴出具体错误信息或日志内容(比如 500 错误、某个 PHP 报错),我可以直接帮你定位问题。