在 Ubuntu 上“关闭 PHP 日志”通常分几种情况,下面按最常见场景说明,你可以对号入座。
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 是分开的,要改对应的
编辑对应文件:
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 就不会写错误日志了
编辑站点配置:
sudo nano /etc/nginx/sites-enabled/default
注释或删除:
access_log off;
error_log /dev/null;
重启:
sudo systemctl restart nginx
sudo nano /etc/apache2/apache2.conf
ErrorLog off
CustomLog /dev/null common
或虚拟主机中:
ErrorLog off
重启:
sudo systemctl restart apache2
如果你只是不想看到 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() |
如果你能告诉我:
我可以直接给你一份精准配置。