要清理 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/
目錄中。您可以編輯相應的配置文件,以便根據需要自動壓縮、刪除和輪換日志文件。