在阿里雲的linux 服務器下Apache的日志默認設置是七天更新一次, 並且所在的目錄無法通過FTP瀏覽器查看, 這樣讓小白操作起來非常麻煩
可以使用rotatelogs來設置服務器的網站訪問日志按天記錄,每天生成一個單獨的文件, 這樣非常方便查看
以我使用的CentOS為例, 首先要檢測是否帶有rotatelogs.
如果沒有安裝,則需要我們安裝rotatelogs, 運行命令:
yum install rotatelogs
修改apache的配置文件,按天記錄網站的訪問日志
打開apache的配置文件:vi /etc/httpd/conf/httpd.conf, 編輯:
#ErrorLog logs/error_log #注釋掉這行,加上下面這行
ErrorLog "|rotatelogs /home/lao8.org/log/error_log%Y_%m_%d.log 86400 480" #設置好日志保存目錄每天生成錯誤日志
#CustomLog logs/access_log common #注釋加上下面這樣
CustomLog "|rotatelogs /home/lao8.org/log//access_log%Y_%m_%d.log 86400 480" combined #每天生成訪問日志
保存並重啟apache (運行命令 service httpd restart)
備注:
1. Rotatelogs的用法:
rotatelogs [ -l ] logfile [ rotationtime [ offset ]] | [ filesizeM ]
選項:
-l 使用本地時間代替GMT時間作為時間基准。注意:在一個改變GMT偏移量(比如夏令時)的環境中使用-l會導致不可預料的結果。
logfile 它加上基准名就是日志文件名。如果logfile中包含"%",則它會被視為用於strftime()的格式字符串;否則它會被自動加上以秒為單位的".nnnnnnnnnn"后綴。這兩種格式都表示新的日志開始使用的時間。
rotationtime 日志文件滾動的以秒為單位的間隔時間。
offset 相對於UTC的時差的分鍾數。如果省略,則假定為"0"並使用UTC時間。比如,要指定UTC時差為"-5小時"的地區的當地時間,則此參數應為"-300"。
filesizeM 指定以filesizeM文件大小滾動,而不是按照時間或時差滾動。
2. Rotatelogs 失敗不生效的原因和解決辦法:
rotatelogs 截斷日志,進行配置。但是保存之后,服務器restart 始終無法重啟成功。
日志文件:
(2)No such file or directory: AH00089: Couldn't start ErrorLog process ' rotatelogs /var/www/html/logs/error/%Y/%m/%d_error.log 86400 480'.
AH00015: Unable to open logs
(2)No such file or directory: AH00089: Couldn't start ErrorLog process ' rotatelogs /var/www/html/logs/error/%Y/%m/%d_error.log 86400 480'.
AH00015: Unable to open logs
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly
Could not open log file '/home/google.com/logs/access/2014/access__20141019.log' (No such file or directory)
AH00106: piped log program '/usr/sbin/rotatelogs /home/google.com/logs/access/%Y/access__%Y%m%d.log 86400 480' failed unexpectedly
解決辦法:
1)rotatelogs 不能直接寫 rotatelogs,必須寫完整地址 /usr/sbin/rotatelogs
2)rotatelogs 無創建目錄的權限,所以最好不要給 格式化的地址加文件夾。
正確的寫法:
CustomLog "| /usr/sbin/rotatelogs /var/www/html/logs/access_%Y%m%d.log 86400 480 " combined