在 Linux 上搭建或运行 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 环境时,排错通常遵循“分层定位”的思路。下面给你一套实用排错流程,从最常见到较深入。
systemctl status httpd # CentOS/RHEL
systemctl status apache2 # Ubuntu/Debian
没启动就:
systemctl start httpd
systemctl enable httpd
查看端口:
ss -lntp | grep 80
systemctl status mysqld
# 或
systemctl status mariadb
登录测试:
mysql -u root -p
php -v
测试 PHP 解析:
echo "<?php phpinfo(); ?>" > /var/www/html/test.php
浏览器访问:
http://服务器IP/test.php
如果看到源码 → Apache 没解析 PHP
常见原因:
检查:
ls -ld /var/www/html
修复权限:
chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html
SELinux:
setenforce 0 # 临时关闭
getenforce
查看 Apache 配置:
grep DocumentRoot /etc/httpd/conf/httpd.conf
看错误日志:
tail -f /var/log/httpd/error_log
常见原因:
编辑 php.ini:
display_errors = On
error_reporting = E_ALL
重启 Apache。
tail -f /var/log/php-fpm/error.log
# 或
tail -f /var/log/httpd/error_log
常见错误:
登录后查看:
SELECT user, host FROM mysql.user;
授权示例:
GRANT ALL ON db.* TO 'user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
看日志:
journalctl -xe
cat /var/log/mysqld.log
常见原因:
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
如果你愿意,可以告诉我:
我可以直接帮你定位到具体原因。