温馨提示×

centos lamp怎么排错误

小樊
39
2026-09-18 09:30:34
栏目: 智能运维

在 CentOS 上搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 时,排错一般按「分层排查」来做。下面给你一套实用排错流程,从最常见到进阶。


一、先确认服务是否运行

1. Apache 是否启动

systemctl status httpd

没启动就:

systemctl start httpd
systemctl enable httpd

查看端口:

ss -lntp | grep :80

2. MySQL / MariaDB 是否启动

systemctl status mariadb
# 或
systemctl status mysqld

启动:

systemctl start mariadb
systemctl enable mariadb

3. PHP 是否安装并生效

php -v

Apache 下确认:

httpd -M | grep php

二、Apache 访问问题排查

1. 无法访问网页

  • 防火墙:
firewall-cmd --list-all
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
  • SELinux(常见坑):
getenforce

如果是 Enforcing

setsebool -P httpd_can_network_connect 1

网站目录权限:

chcon -R -t httpd_sys_content_t /var/www/html

2. 403 Forbidden

常见原因:

  • 目录无权限
  • SELinux
  • 没有 index 文件

解决:

chmod -R 755 /var/www/html

确认有:

ls /var/www/html/index.php

3. 500 Internal Server Error

看 Apache 错误日志:

tail -f /var/log/httpd/error_log

常见原因:

  • PHP 语法错误
  • .htaccess 配置错误
  • 文件权限不对

三、PHP 错误处理

1. 页面空白

编辑:

vi /etc/php.ini

确保:

display_errors = On
error_reporting = E_ALL

重启 Apache:

systemctl restart httpd

2. PHP 不解析(显示源码)

检查:

httpd -M | grep php

没有就安装:

yum install php php-mysql
systemctl restart httpd

四、数据库问题

1. 无法登录 MySQL

mysql -u root -p

忘记密码:

systemctl stop mariadb
mysqld_safe --skip-grant-tables &

2. PHP 连不上数据库

检查:

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

常见错误:

  • 用户无权限
  • 主机不是 localhost
  • 防火墙

五、日志是最快的排错方式

# 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

六、快速自检清单 ✅

  • [ ] httpd 运行
  • [ ] mariadb 运行
  • [ ] php 模块加载
  • [ ] 防火墙放行
  • [ ] SELinux 不拦
  • [ ] 日志无报错

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

  • CentOS 版本(6 / 7 / 8 / Stream)
  • 具体报错信息或截图
  • 访问网页错误 还是 安装阶段错误

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

0