温馨提示×

linux lamp怎么排错

小樊
37
2026-10-12 12:39:30
栏目: 智能运维

在 Linux 上搭建或运行 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 环境时,排错通常遵循“分层定位”的思路。下面给你一套实用排错流程,从最常见到较深入。


一、先确认服务是否运行

1. Apache 是否启动

systemctl status httpd      # CentOS/RHEL
systemctl status apache2    # Ubuntu/Debian

没启动就:

systemctl start httpd
systemctl enable httpd

查看端口:

ss -lntp | grep 80

2. MySQL / MariaDB 是否运行

systemctl status mysqld
# 或
systemctl status mariadb

登录测试:

mysql -u root -p

3. PHP 是否安装正确

php -v

测试 PHP 解析:

echo "<?php phpinfo(); ?>" > /var/www/html/test.php

浏览器访问:

http://服务器IP/test.php

如果看到源码 → Apache 没解析 PHP


二、Apache 常见错误

1. 403 Forbidden

常见原因:

  • 目录权限不对
  • SELinux 限制(CentOS)

检查:

ls -ld /var/www/html

修复权限:

chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html

SELinux:

setenforce 0   # 临时关闭
getenforce

2. 404 Not Found

  • 文件不在 DocumentRoot
  • 文件名大小写错误(Linux 区分大小写)

查看 Apache 配置:

grep DocumentRoot /etc/httpd/conf/httpd.conf

3. 500 Internal Server Error

看错误日志:

tail -f /var/log/httpd/error_log

常见原因:

  • .htaccess 语法错误
  • PHP 报错
  • 文件权限问题

三、PHP 排错

1. 开启错误显示(开发环境)

编辑 php.ini:

display_errors = On
error_reporting = E_ALL

重启 Apache。


2. 查看 PHP 错误日志

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

四、MySQL / MariaDB 排错

1. 无法连接数据库

常见错误:

  • 用户名/密码错
  • 主机限制(localhost vs %)

登录后查看:

SELECT user, host FROM mysql.user;

授权示例:

GRANT ALL ON db.* TO 'user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

2. 服务启动失败

看日志:

journalctl -xe
cat /var/log/mysqld.log

常见原因:

  • 磁盘满
  • 配置文件错误
  • 权限问题(/var/lib/mysql)

五、防火墙 & 端口

开放 80 / 443

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

或 iptables:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

云服务器还要检查安全组。


六、通用排错命令(很重要)

# 看系统日志
journalctl -xe

# 看 Apache 日志
tail -f /var/log/httpd/error_log

# 看访问日志
tail -f /var/log/httpd/access_log

# 看资源
top / htop / df -h

七、排错思路总结(记住这个顺序)

  1. 服务起没起
  2. 端口通不通
  3. 权限对不对
  4. 日志说什么
  5. 配置改没改错

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

  • 用的系统(CentOS / Ubuntu)
  • 具体报错信息或截图
  • 是安装失败还是访问网页出错

我可以直接帮你定位到具体原因。

0 踩