LNMP使用Awstats分析Nginx日志[轉載]
Awstats的運行需要PERL的支持,因為Nginx對PERL支持很弱,只能通過生成Html方式來輸出統計。
首先,需要每日切割日志文件。提供個腳本給大家
mv /usr/local/nginx/www.5dft.com.log /usr/local/nginx/logs/access5dft_`date +%Y%m%d`.log
killall –s USR1 nginx #使用USR1參數通知Nginx進程切換日志文件
很簡單,兩行實現切割。別問我格式,我網上抄來的。太困了,爭取最快時間寫完。
然后到某開源網wget回來,解壓即可。我放到/usr/local/里面了,別問為什么,我習慣。
接下來的內容非常非常重要,大家睜大眼睛看清楚。修改日志配置為以下格式(具體位置在/usr/local/nginx/conf/你的網 址.conf):
log_format? www.5dft.com? '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
行了格式就是上面那樣,要問為什么?因為默認格式Awstats不認。o(╯□╰)o
在剛剛解壓回來的文件里面的tools找到awstats_configure.pl并執行開始配置。
-----> Check for web server install
Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup):
#> none??#因為我們這里用的是 Nginx,所以寫 none,跳過。
回車
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf'
File awstats.model.conf updated.
-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ?
#> y? ? ? ? #y 創建一個新的統計配置
回 車
-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
#> www.5dft.com? ? ? ? ? ? ? ? #統計網站的域名
回車
-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
#>
使用默認直接回車,接下來便會出現以下的提示
----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.5dft.com
#回頭把該命令填入crontab 按指定時間執行
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...? ? ? ? ? ? ? ? 回車繼續
【上面從別的地方摘來的,別問從哪摘,N個網站都這篇文章我哪知道出處?反正我是上面少了一個步驟,少了個回車,其它正常。繼續】
接下來就生成了個文件了。我的是在/etc/awstats/awstats.www.5dft.com.conf
在那個文件找到統計的日志文件的路徑
LogFile="/var/log/httpd/mylog.log"
改為
LogFile="/opt/nginx/logs/access5dft_%YYYY-0%MM-0%DD-0.log
對應上邊 Nginx 日志切割程序的所生成的目錄存放結構,要注意 Awstats 的年月日格式的跟 Nginx 的寫法有所不同。我們現在執行統計的順序是:
Nginx 產生日志 –> 日志切割 –> Nginx 繼續產生日志 –> 另存切割日志 –> 交由Awstats統計 –> 生成結果
在本文中 Awstats 所統計的日志,是已切下來的那部分。也能調轉順序,先統計完了再切。不過這比較容易造成統計的遺漏。配置修改完成后,保存退出。然后我們可以開始試一下手 動執行。
先執行日志切割腳本 logcron.sh 把 Nginx 的日志切下來。
然后執行 Awstats 日志更新程序開始統計分析。
./logcron.sh
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.5dft.com
看到幾行類似
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 120 new qualified records.
就是成功了。如果沒有的話是缺少一個/var/lib什么什么的,mkdir一下就可以。我發現網上教程都少了這一步,耽誤小白啊。o(╯□╰)o 還好我是大白,很快解決。繼續。
那么我們就必須建個文件夾來放文件咯。放在網站里面方便看。
省略建文件夾步驟,繼續。
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.5dft.com -lang=cn -dir=/home/wwwroot/5dft.com/xxx -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
上面的命令我不查資料還真不知道。awstats_buildstaticpages.pl是生成靜態頁面的。后面指定更新,后面語言CN,后面指 定生成靜態的目錄,后面awstats.pl是Awstats 日志更新程序路徑。
請注意上面的命令是一行的,切勿分割或者別的什么的。
把那個和分割日志的放進crontab即可完成任務。
好了,到此任務基本完成。剩下的生成的文件要怎么加密就是你的問題了。我是懶蟲,試了幾次500之后我就直接改個復雜的文件夾名就完事了。但是別塞 進robot,否則...嘿嘿,別人一看就知道了。明白我意思吧?
文件夾加密也不是很難,估計我抄錯資料了。太晚了不折騰,睡覺~~~
轉載自:http://www.xinblog.info/post-72.html
>>轉載請注明出處:VPS偵探 本文鏈接地址:http://www.0794baidu.com/manage/lnmp-awstats-nginx-logs.html










@sunny, 這個不是錯誤。
軍哥,分割日志后的error.log
2010/08/31 00:00:09 [notice] 1335#0: signal process started
請問這是什么錯誤,怎么解決? ?? ??
@xkx, 嗯
大概是這里吧
http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/index.html