要清理 CentOS 上的旧 PHP 日志,请遵循以下步骤:
首先,找到存储 PHP 日志的目录。通常情况下,这些日志文件位于 /var/log/php
或 /var/log/httpd
目录中。
使用 cd
命令进入相应的目录,例如:
cd /var/log/php
或者
cd /var/log/httpd
ls
命令查看目录中的日志文件:ls -l
find
命令查找特定日期之前的日志文件。例如,要删除 30 天前的日志文件,请执行以下命令:find . -type f -name "*.log" -mtime +30 -exec rm -f {} \;
这将删除当前目录及其子目录中所有 30 天前的 .log
文件。
access.log
或 error.log
,可以使用以下命令:find . -type f \( -name "access.log" -o -name "error.log" \) -mtime +30 -exec rm -f {} \;
对于 Apache,您可以在 httpd.conf
或 apache2.conf
文件中设置以下指令:
LogFormat "%h %l %u %t \"%r\" %>s %b" combined
CustomLog "/var/log/httpd/access_log" combined
ErrorLog "/var/log/httpd/error_log"
LogLevel warn
对于 Nginx,您可以在 nginx.conf
文件中设置以下指令:
http {
...
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
...
}
此外,您还可以使用 logrotate
工具自动管理日志文件。在 CentOS 上,logrotate
配置文件通常位于 /etc/logrotate.d/
目录中。您可以编辑相应的配置文件,以便根据需要自动压缩、删除和轮换日志文件。