在 CentOS 上搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 时,排错一般按「分层排查」来做。下面给你一套实用排错流程,从最常见到进阶。
systemctl status httpd
没启动就:
systemctl start httpd
systemctl enable httpd
查看端口:
ss -lntp | grep :80
systemctl status mariadb
# 或
systemctl status mysqld
启动:
systemctl start mariadb
systemctl enable mariadb
php -v
Apache 下确认:
httpd -M | grep php
firewall-cmd --list-all
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
getenforce
如果是 Enforcing:
setsebool -P httpd_can_network_connect 1
网站目录权限:
chcon -R -t httpd_sys_content_t /var/www/html
常见原因:
解决:
chmod -R 755 /var/www/html
确认有:
ls /var/www/html/index.php
看 Apache 错误日志:
tail -f /var/log/httpd/error_log
常见原因:
编辑:
vi /etc/php.ini
确保:
display_errors = On
error_reporting = E_ALL
重启 Apache:
systemctl restart httpd
检查:
httpd -M | grep php
没有就安装:
yum install php php-mysql
systemctl restart httpd
mysql -u root -p
忘记密码:
systemctl stop mariadb
mysqld_safe --skip-grant-tables &
检查:
<?php
$conn = new mysqli("localhost","user","pass","db");
if ($conn->connect_error) {
die($conn->connect_error);
}
常见错误:
# Apache
tail -f /var/log/httpd/error_log
tail -f /var/log/httpd/access_log
# PHP
/var/log/php-fpm.log 或 php.ini 中配置
# MySQL
/var/log/mariadb/mariadb.log
如果你愿意,可以告诉我:
我可以直接帮你定位到具体原因。