LNMP使用Awstats分析Nginx日志[轉(zhuǎn)載]
Awstats的運(yùn)行需要PERL的支持,因?yàn)镹ginx對PERL支持很弱,只能通過生成Html方式來輸出統(tǒng)計(jì)。
首先,需要每日切割日志文件。提供個(gè)腳本給大家
mv /usr/local/nginx/www.5dft.com.log /usr/local/nginx/logs/access5dft_`date +%Y%m%d`.log
killall –s USR1 nginx #使用USR1參數(shù)通知Nginx進(jìn)程切換日志文件
很簡單,兩行實(shí)現(xiàn)切割。別問我格式,我網(wǎng)上抄來的。太困了,爭取最快時(shí)間寫完。
然后到某開源網(wǎng)wget回來,解壓即可。我放到/usr/local/里面了,別問為什么,我習(xí)慣。
接下來的內(nèi)容非常非常重要,大家睜大眼睛看清楚。修改日志配置為以下格式(具體位置在/usr/local/nginx/conf/你的網(wǎng) 址.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"';
行了格式就是上面那樣,要問為什么?因?yàn)槟J(rèn)格式Awstats不認(rèn)。o(╯□╰)o
在剛剛解壓回來的文件里面的tools找到awstats_configure.pl并執(zhí)行開始配置。
-----> 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??#因?yàn)槲覀冞@里用的是 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 創(chuàng)建一個(gè)新的統(tǒng)計(jì)配置
回 車
-----> 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? ? ? ? ? ? ? ? #統(tǒng)計(jì)網(wǎng)站的域名
回車
-----> 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):
#>
使用默認(rèn)直接回車,接下來便會出現(xiàn)以下的提示
----> 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 按指定時(shí)間執(zhí)行
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...? ? ? ? ? ? ? ? 回車?yán)^續(xù)
【上面從別的地方摘來的,別問從哪摘,N個(gè)網(wǎng)站都這篇文章我哪知道出處?反正我是上面少了一個(gè)步驟,少了個(gè)回車,其它正常。繼續(xù)】
接下來就生成了個(gè)文件了。我的是在/etc/awstats/awstats.www.5dft.com.conf
在那個(gè)文件找到統(tǒng)計(jì)的日志文件的路徑
LogFile="/var/log/httpd/mylog.log"
改為
LogFile="/opt/nginx/logs/access5dft_%YYYY-0%MM-0%DD-0.log
對應(yīng)上邊 Nginx 日志切割程序的所生成的目錄存放結(jié)構(gòu),要注意 Awstats 的年月日格式的跟 Nginx 的寫法有所不同。我們現(xiàn)在執(zhí)行統(tǒng)計(jì)的順序是:
Nginx 產(chǎn)生日志 –> 日志切割 –> Nginx 繼續(xù)產(chǎn)生日志 –> 另存切割日志 –> 交由Awstats統(tǒng)計(jì) –> 生成結(jié)果
在本文中 Awstats 所統(tǒng)計(jì)的日志,是已切下來的那部分。也能調(diào)轉(zhuǎn)順序,先統(tǒng)計(jì)完了再切。不過這比較容易造成統(tǒng)計(jì)的遺漏。配置修改完成后,保存退出。然后我們可以開始試一下手 動執(zhí)行。
先執(zhí)行日志切割腳本 logcron.sh 把 Nginx 的日志切下來。
然后執(zhí)行 Awstats 日志更新程序開始統(tǒng)計(jì)分析。
./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.
就是成功了。如果沒有的話是缺少一個(gè)/var/lib什么什么的,mkdir一下就可以。我發(fā)現(xiàn)網(wǎng)上教程都少了這一步,耽誤小白啊。o(╯□╰)o 還好我是大白,很快解決。繼續(xù)。
那么我們就必須建個(gè)文件夾來放文件咯。放在網(wǎng)站里面方便看。
省略建文件夾步驟,繼續(xù)。
/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是生成靜態(tài)頁面的。后面指定更新,后面語言CN,后面指 定生成靜態(tài)的目錄,后面awstats.pl是Awstats 日志更新程序路徑。
請注意上面的命令是一行的,切勿分割或者別的什么的。
把那個(gè)和分割日志的放進(jìn)crontab即可完成任務(wù)。
好了,到此任務(wù)基本完成。剩下的生成的文件要怎么加密就是你的問題了。我是懶蟲,試了幾次500之后我就直接改個(gè)復(fù)雜的文件夾名就完事了。但是別塞 進(jìn)robot,否則...嘿嘿,別人一看就知道了。明白我意思吧?
文件夾加密也不是很難,估計(jì)我抄錯(cuò)資料了。太晚了不折騰,睡覺~~~
轉(zhuǎn)載自:http://www.xinblog.info/post-72.html
>>轉(zhuǎn)載請注明出處:VPS偵探 本文鏈接地址:http://www.0794baidu.com/manage/lnmp-awstats-nginx-logs.html










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