lnmp環境下網站日誌進行分割配置

來源:互聯網
上載者:User

這個教程適用於Nginx使用者及lnmp使用者參考的參考,其他環境下,大叔不雞道哈!

首先我們得用root 登陸執行

#!/bin/bash
#function:cut nginx log files for lnmp v0.5 and v0.6
#set the path to nginx log files
log_files_path="/home/wwwlogs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
#set nginx log files you want to cut
log_files_name=(access vpser licess)
#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save
save_days=30

############################################
#Please do not modify the following script #
############################################
mkdir -p $log_files_dir

log_files_num=${#log_files_name[@]}

#cut nginx log files
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log
done

#delete 30 days ago nginx log files
find $log_files_path -mtime +$save_days -exec rm -rf {} \;

$nginx_sbin -s reload


指令碼主要參數:


#設定nginx記錄檔目錄路徑
log_files_path="/home/wwwlogs/"

#記錄檔將會存放到/home/wwwlogs/年/月/記錄檔名_年月日.log
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")

#設定要切割的日誌的名字,如果日誌目錄下面的記錄檔名為vpser.net.log,則填寫vpser.net,每個日誌名用空格分隔
log_files_name=(access vpser.net licess)

#設定nginx檔案的位置
nginx_sbin="/usr/local/nginx/sbin/nginx"

#設定日誌儲存的時間,天
save_days=30

如果是非lnmp一鍵安裝包使用者可以通過修改上述參數適應你的nginx環境。


將nginx日誌切割指令碼下載到/root/下面後

執行 crontab -e

進入vi編輯模式

在編輯模式下鍵入 i 進行編輯

輸入:

00 00 * * * /bin/bash /root/cut_nginx_logs.sh

按Esc

再按 “:”   備忘,這是小寫冒號

輸入wq 斷行符號 即可儲存退出

日誌自動切割指令碼建立完成 每天會在0點自動執行

查詢自動執行任務輸入

crontab -l

 

附:輸入 crontab -e  提示:     crontab: command not found,只需要運行下面的命令安裝 vixie-cron crontabs 即可:

yum -y install  vixie-cron crontabs

其中,vixie-cron 是cron的主程式;crontabs 是用來安裝、卸裝、或列舉用來驅動 cron 守護進程的表格的程式


補充:開啟LNMP網站訪問日誌 分析網站記錄檔


首先、找到你需要開啟網站所在的conf檔案,位置在

/usr/local/nginx/conf/vhost/laozuo.org.conf
其次、用文字編輯器開啟上述的檔案,找到最下面的
access_log off;
將其替換為:
access_log  /home/wwwlogs/laozuo.org.log  laozuo.org;
然後將下面代碼加到該conf頂部:
log_format  laozuo.org  '$remote_addr - $remote_user [$time_local] "$request" '             '$status $body_bytes_sent "$http_referer" '             '"$http_user_agent" $http_x_forwarded_for';
最後,重啟LNMP
/root/lnmp restart
重啟LNMP才可以生效,記錄檔儲存在home/wwwlogs檔案夾下面,記得要定期刪除,否則你的機器佔用檔案越來越大。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.