在 CentOS 系统中,PHP 日志和错误报告的设置主要涉及两个方面:PHP 本身的错误报告设置和 Web 服务器(如 Apache 或 Nginx)的错误日志设置。下面分别介绍这两个方面的设置方法。
编辑 php.ini 文件,通常位于 /etc/php.ini。你可以使用文本编辑器(如 vim、nano 等)打开它:
sudo vi /etc/php.ini
在 php.ini 文件中,找到以下设置项并进行调整:
display_errors:控制是否在浏览器中显示错误信息。将其设置为 Off,以避免将敏感信息泄露给用户。
display_errors = Off
log_errors:控制是否将错误信息记录到日志文件中。将其设置为 On。
log_errors = On
error_log:设置错误日志文件的路径。例如,将其设置为 /var/log/php_errors.log。
error_log = /var/log/php_errors.log
error_reporting:设置报告哪些类型的错误。通常设置为 E_ALL,以报告所有错误。
error_reporting = E_ALL
修改完成后,保存并关闭 php.ini 文件。然后重启 PHP-FPM 或 Apache 服务,使设置生效:
sudo systemctl restart php-fpm # 或者 sudo systemctl restart httpd
对于 Apache 服务器:
编辑 Apache 的配置文件(通常位于 /etc/httpd/conf/httpd.conf 或 /etc/apache2/apache2.conf),找到 ErrorLog 和 CustomLog 指令,设置错误日志文件的路径和格式。例如:
ErrorLog /var/log/httpd/error_log CustomLog /var/log/httpd/access_log combined
修改完成后,保存并关闭配置文件。然后重启 Apache 服务,使设置生效:
sudo systemctl restart httpd # 或者 sudo systemctl restart apache2
对于 Nginx 服务器:
编辑 Nginx 的配置文件(通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/your_domain.conf),在 server 块中找到 error_log 指令,设置错误日志文件的路径和格式。例如:
error_log /var/log/nginx/error.log debug;
修改完成后,保存并关闭配置文件。然后重启 Nginx 服务,使设置生效:
sudo systemctl restart nginx
现在,PHP 错误报告和 Web 服务器错误日志的设置已经完成。请确保定期检查错误日志文件,以便及时发现并解决问题。