Build Awstats Analysis Nginx log in Linux

Source: Internet
Author: User
Tags chmod mkdir

System: CentOS 5.x
Required Packages: awstats-7.3.tar.gz

1. Modify Nginx Log format

The code is as follows Copy Code

Log_format Main ' $remote _addr-$remote _user [$time _local] ' $request '
' $status $body _bytes_sent ' $http _referer '
' $http _user_agent ', ' $http _x_forwarded_for ', ' $request _time ';

Access_log/var/log/www/access.log main;

Ps:
Place the Log_format code in the HTTP definition section of your nginx, which you can reference in each of the following servers, without having to define the format in each server.

2. Cutting Nginx Log Script

The code is as follows Copy Code

VI cut_log.sh

#!/bin/bash
Export Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Logs_path= "/var/log/www/"
date_dir=${logs_path}$ (date-d "Yesterday" + "%Y")/$ (date-d "Yesterday" + "%m")/$ (date-d "Yesterday" + "%d")/
gzip_date_dir=${logs_path}$ (date-d "-2 Day" + "%Y")/$ (date-d "-2 Day" + "%m")/$ (date-d "-2 Day" + "%d")/

Mkdir-p $date _dir
MV ${logs_path}*access.log $date _dir
/usr/sbin/nginx-s Reopen
/usr/bin/gzip ${gzip_date_dir}*.log

and threw it into the crontab, and every night 11:57 to perform the cutting:

The code is as follows Copy Code
* * */bin/sh/root/webbak/cut_log.sh

3. Install Awstats

The code is as follows Copy Code

wget http://awstats.sourceforge.net/files/awstats-7.3.tar.gz
Tar zxf awstats-7.3.tar.gz && mv Awstats-7.3/usr/local/awstats
Chown-r Root:root/usr/local/awstats
Chmod-r =rwx/usr/local/awstats
chmod +x/usr/local/awstats/tools/*.pl
chmod +x/usr/local/awstats/wwwroot/cgi-bin/*.pl

Execute the awstats_configure.pl Configuration Wizard in the Tools directory to create a new statistic:
Cd/usr/local/awstats/tools
./awstats_configure.pl

There will be some hints as follows:

The code is as follows Copy Code

-----> Running OS detected:linux, BSD Orunix

----->check for Web server install
Enterfull config file path of your WEB server.
Example:/etc/httpd/httpd.conf
Example:/usr/local/apache2/conf/httpd.conf
Example:c:program filesapache groupapacheconfhttpd.conf Config file path (' None ' to skip Web server Setup):
>none #这里添none并回车 because we're not using Apache
Next option after carriage return:
Yourweb Server config file (s) could not is found.
Youwill need to the setup your Web server manually to declare AWStats
Script as a CGI, if you want Tobuild reports dynamically. 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 and want me to build anew AWStats config/profilefile (required if I install) [y/n]? Y #这里选Y, creating a new configuration file

-----> Define Config file name to create
What is the name of your Web site or profile?
Example:www.mysite.com
Example:demo
Yourweb site, virtual Server or profile name:
>www.111cn.net #这里输入你要分析的域名, or any one of your easy to remember configuration name and enter

----->define config file path in which directory does you have your config file (s)?
Default:/etc/awstats
DirectoryPath to store config file (s) (Enter Fordefault):
> #直接回车, define the path to your configuration file, use the default path/etc/awstats

----->create config file '/etc/awstats/awstats.nginx.conf '
configfile/etc/awstats/awstats.nginx.conf Created.
-----> Add updateprocess inside a scheduler Sorry, configure.pl does not support automatic cron addto.
You can does it manually by adding the following command to Yourcron:/usr/local/awstats/wwwroot/cgi-bin/awstats.pl-up Date-config=nginx
Or If you have several config files and prefer have only Onecommand:/usr/local/awstats/tools/aws tats_updateall.pl now
Press ENTER to continue ... #按回车继续
A simple config file has been created:/etc/awstats/awstats . nginx.conf you should have a lookinside to check and change manually main parameters.
Youcan then manually update your statistics for ' yuyuanchun.com ' Withcommand: > Perl awstats.pl-update-config=ngi NX
Youcan also build static on pages for ' Nginx ' with command:> Perl awstats.pl-output=pagetype-config=nginx Pressenter to finish ... #回车完成配置文件的创建

After the configuration file is created, we're going to have to revise it. Because I was cutting the log by day, after cutting finished to Awstats to analyze. is not to let the awstats to time-sharing is constantly increasing the log is being written, the advantage is not to omit data, and analysis has been cut completed log , not to worry about conflict. The disadvantage is that I cut a log one day, you have to wait until the next day to see some of yesterday's detailed data.

The code is as follows Copy Code
Vi/etc/awstats/awstats.www.111cn.net.conf
Put logfile= "/var/log/httpd/mylog.log"
Modified to:
Logfile= "/var/log/www/%yyyy-24/%mm-24/%dd-24/access.log"

The full path above is the Nginx log file that is saved after the cut. The%yyyy-24/%mm-24/%dd-24 indicates that the date of the year is minus 24 hours, which is the log directory of yesterday.

4. Create a directory where Awstats is used to record data

The code is as follows Copy Code
Mkdir-p/var/lib/awstats

5. Create a directory that holds awstats generated static files

The code is as follows Copy Code
Mkdir-p/var/www/vhosts/www.111cn.net/awstats/www.111cn.net

Of course, you can also use the script to achieve:
VI awstats.sh

  code is as follows copy code

#!/bin/ Bash
Export path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
mkdir-p/var/www/ Vhosts/www.111cn.net/awstats/www.111cn.net
/usr/local/awstats/tools/awstats_buildstaticpages.pl-update  
-config=www.111cn.net-lang=cn-dir=/var/www/vhosts/www.111cn.net/awstats/www.111cn.net 
- awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

PS:
/usr/local/awstats/tools/awstats_ buildstaticpages.pl    Awstats static page generation tool
-update-config=www.111cn.net     Update Configuration Entry
-lang=cn     language to Chinese
-dir=/var/www/vhosts/www.111cn.net/awstats/www.111cn.net     statistics Results Output directory
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl    Awstats Log Update path

and throw awstat.sh into Crontab. Execute:

The code is as follows Copy Code
* * */bin/sh/root/webbak/awstats.sh

The last manual execution of the awstats.sh

The code is as follows Copy Code
./awstats.sh

If you see it generate a bunch of web pages, that means success.

6. Modify Nginx Configuration

The code is as follows Copy Code

server {
Listen 80;
server_name www.111cn.net;
Access_log/var/log/www/access.log main;
Root/var/www/vhosts/www.111cn.net;
Index index.php index.html index.htm;

        Location ~ ^/awstats/{
                 root/var/www/vhosts/www.111cn.net/;
                AutoIndex on;
                index index.html;
                Access_log off;
                Auth_basic " Please enter your password: ";
                Auth_basic_user_ FILE/VAR/WWW/VHOSTS/HTPASSWD;
               }

Location ~ ^/icon/{
Root/usr/local/awstats/wwwroot;
Index index.html;
Access_log off;
}
}

Then restart Nginx, of course, I added identity authentication, only let oneself can see, to ensure security. Well, the Awstats installation configuration is here. The rest is to generate data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.