温馨提示×

怎么调试linux lamp

小樊
45
2026-09-30 19:44:50
栏目: 智能运维

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


一、基础检查

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

2. 防火墙 & SELinux

# 防火墙
firewall-cmd --list-all
# 或
ufw status

# 放行 80/443
firewall-cmd --add-service=http --permanent
firewall-cmd --reload

SELinux(CentOS):

getenforce
setenforce 0   # 临时关闭测试

二、Apache 调试

1. 配置文件语法

apachectl configtest

或

httpd -t

2. 查看错误日志

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

3. 测试访问

curl http://localhost

三、PHP 调试

1. 确认 PHP 已加载

创建测试文件:

<?php
phpinfo();

访问后看是否有 PHP 信息。

2. PHP 错误显示(开发环境)

编辑 php.ini:

display_errors = On
error_reporting = E_ALL

重启服务:

systemctl restart httpd

3. PHP 错误日志

tail -f /var/log/php-fpm/error.log
# 或
tail -f /var/log/php_errors.log

四、MySQL / MariaDB 调试

1. 能否连接

mysql -u root -p

2. 查看错误日志

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

3. 用户权限问题

SELECT user, host FROM mysql.user;
SHOW GRANTS FOR 'user'@'localhost';

五、应用层调试技巧

1. 打开网站 debug 模式

(如 WordPress / Laravel / ThinkPHP)

  • 开启 debug
  • 查看应用日志目录

2. 使用浏览器开发者工具

  • Network(看 500 / 404)
  • Console(JS 错误)

3. strace(高级)

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

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

  • 你用的是 CentOS / Ubuntu / Alma / Debian
  • Apache 还是 Nginx
  • 具体报错信息(截图或日志)

我可以直接帮你定位问题。

0 踩