温馨提示×

ubuntu php日志怎么关闭

小樊
35
2026-10-06 10:39:37
栏目: 编程语言

在 Ubuntu 上“关闭 PHP 日志”通常分几种情况,下面按最常见场景说明,你可以对号入座。


一、关闭 PHP 自身的错误日志(php.ini)

1. 找到 php.ini

php --ini

常见路径:

  • /etc/php/8.1/cli/php.ini
  • /etc/php/8.1/fpm/php.ini
  • /etc/php/8.1/apache2/php.ini

⚠️ CLI、FPM、Apache 是分开的,要改对应的


2. 修改 php.ini

编辑对应文件:

sudo nano /etc/php/8.1/fpm/php.ini

关闭错误显示和日志

display_errors = Off
log_errors = Off
error_log = /dev/null

保存后重启:

sudo systemctl restart php8.1-fpm
# 或 apache
sudo systemctl restart apache2

✅ 这样 PHP 就不会写错误日志了


二、关闭 Nginx / Apache 的 PHP 访问/错误日志(可选)

Nginx

编辑站点配置:

sudo nano /etc/nginx/sites-enabled/default

注释或删除:

access_log off;
error_log /dev/null;

重启:

sudo systemctl restart nginx

Apache

sudo nano /etc/apache2/apache2.conf
ErrorLog off
CustomLog /dev/null common

或虚拟主机中:

ErrorLog off

重启:

sudo systemctl restart apache2

三、只“不记录 PHP 错误”,但保留系统日志(推荐)

如果你只是不想看到 PHP 报错,但系统还要正常:

display_errors = Off
log_errors = On
error_log = /var/log/php-error.log

然后:

sudo truncate -s 0 /var/log/php-error.log

四、临时关闭(不重启)

在 PHP 代码里:

ini_set('log_errors', 0);
ini_set('display_errors', 0);
error_reporting(0);

总结

目的 做法
完全不写 PHP 日志 log_errors = Off
不显示错误 display_errors = Off
不写 Web 日志 Nginx/Apache access_log off
临时关闭 ini_set()

如果你能告诉我:

  • PHP 版本
  • Nginx 还是 Apache
  • 想关的是错误日志还是访问日志

我可以直接给你一份精准配置。

0 踩