温馨提示×

如何调试linux lamp

小樊
39
2026-09-12 14:15:42
栏目: 智能运维

调试 Linux + Apache + MySQL/MariaDB + PHP(LAMP) 通常按“分层排查”的思路进行:网络 → Apache → PHP → MySQL → 应用日志。下面给你一套实用调试流程。


一、基础环境检查

1. 确认服务是否运行

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

二、Apache 调试

1. 检查配置文件语法

apachectl configtest
# 或
httpd -t

2. 查看 Apache 错误日志

tail -f /var/log/httpd/error_log
# Ubuntu:
tail -f /var/log/apache2/error.log

3. 测试访问

curl -I http://localhost

若无法访问:

  • 防火墙:firewalld / ufw
  • SELinux(CentOS):
getenforce
setenforce 0   # 临时关闭排查

三、PHP 调试

1. 确认 PHP 是否生效

创建测试文件:

<?php
phpinfo();

访问是否能看到 PHP 信息。

2. 查看 PHP 错误

编辑 php.ini

display_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php_errors.log

重启 Apache 或 PHP-FPM。

3. PHP-FPM 调试(如适用)

tail -f /var/log/php-fpm/error.log

四、MySQL / MariaDB 调试

1. 是否能登录

mysql -u root -p

2. 查看错误日志

tail -f /var/log/mysqld.log
# 或
tail -f /var/log/mysql/error.log

3. PHP 连接数据库测试

<?php
$conn = new mysqli("localhost","user","pass","db");
if ($conn->connect_error) {
    die("DB Error: " . $conn->connect_error);
}
echo "OK";

常见问题:

  • 用户无权限
  • 主机限制(localhost vs 127.0.0.1)
  • 防火墙/端口未开(3306)

五、应用层调试建议

  • 打开框架调试模式(Laravel / ThinkPHP 等)
  • 使用 error_log()var_dump()
  • 使用 Xdebug(高级):
zend_extension=xdebug.so
xdebug.mode=debug

六、快速排查清单

✅ 服务是否启动
✅ 端口是否监听(netstat -tunlp
✅ 防火墙/SELinux
✅ Apache/PHP/MySQL 日志
✅ 文件权限(www-data / apache)
✅ 配置是否 reload


如果你愿意,可以告诉我:

  • 使用的 Linux 发行版
  • Apache 还是 Nginx
  • 具体报错信息

我可以给你更针对性的调试方案。

0