温馨提示×

centos lamp如何查看日志

小樊
47
2026-10-06 04:47:31
栏目: 智能运维

在 CentOS 的 LAMP 环境(Linux + Apache + MySQL/MariaDB + PHP)中,查看日志主要集中在 Apache、MySQL/MariaDB、PHP 以及 系统日志。下面给你一份常用、实用的查看方法。


一、Apache 日志(最重要)

1. 日志文件位置(常见)

/var/log/httpd/access_log     # 访问日志
/var/log/httpd/error_log      # 错误日志

如果是编译安装,路径可能不同,可在 Apache 配置中查看:

grep -i "log" /etc/httpd/conf/httpd.conf

2. 实时查看错误日志

tail -f /var/log/httpd/error_log

3. 查看最近 100 行访问日志

tail -n 100 /var/log/httpd/access_log

4. 搜索关键字(如 500 错误)

grep "500" /var/log/httpd/error_log

二、MySQL / MariaDB 日志

1. 查看错误日志位置

mysql -u root -p -e "SHOW VARIABLES LIKE 'log_error';"

常见路径:

/var/log/mysqld.log
/var/log/mariadb/mariadb.log

2. 查看错误日志

tail -f /var/log/mysqld.log

3. 开启并查看慢查询日志(可选)

SET GLOBAL slow_query_log = 'ON';
SHOW VARIABLES LIKE 'slow_query_log_file';

三、PHP 日志

1. 查看 PHP 错误日志路径

php -i | grep error_log

或查看 php.ini:

grep error_log /etc/php.ini

2. 常见路径

/var/log/php-fpm/error.log
/var/log/php_errors.log

3. 实时查看 PHP 错误

tail -f /var/log/php-fpm/error.log

四、系统日志(通用)

1. 使用 journalctl(CentOS 7+)

journalctl -xe

查看 Apache:

journalctl -u httpd

查看 MySQL:

journalctl -u mysqld

2. 传统日志

/var/log/messages
/var/log/secure

五、常用组合命令(排查网站问题)

# 实时看 Apache 错误
tail -f /var/log/httpd/error_log

# 实时看 PHP 错误
tail -f /var/log/php-fpm/error.log

# 看系统级别报错
journalctl -xe

六、快速判断问题在哪

问题现象 优先看
网站 500 Apache error_log + PHP 日志
网站打不开 httpd 是否运行 + error_log
数据库连不上 mysqld.log
权限问题 error_log + /var/log/messages

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

  • CentOS 版本(6 / 7 / 8 / Stream)
  • Apache / PHP / MySQL 版本
  • 具体报错现象(如 500、白屏、连接失败)

我可以给你更精确的日志查看和排查命令。

0 踩