Awstas Setup for a Virtual Host on Centos 6.x

posted in: Technical | 0

The procedures for setup Awstats on a virtual host running Apache on a Centos 6.x server can be explained as follows;

The first thing to do, off course, is installing the Awstats RPM. Login as root and do the installation of Awstats using the command: yum install awstats

Next, it is needed to append Awstats directives into the httpd.conf file of the server that running virtual hosting.

The httpd.conf can be accessed for editing using the command: nano /etc/httpd/conf/httpd.conf

Add the following sections inside the corresponding <VirtualHost *:80> section of the httpd.conf

## AWstats ##
#Directives to allow use of AWStats as a CGI
Alias /awstasclasses "/usr/share/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/share/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/share/awstats/wwwroot/icon/"
ScriptAlias /stats/ "/usr/share/awstats/wwwroot/cgi-bin/"
#This is to permit URL access to scripts/files in AWStats directory.
<Directory "/usr/share/awstats">
Options None
AllowOverride None
Order allow,deny
Allow from all
#This is to allow access to the cgi-bin folder
<Directory "/usr/share/awstats/wwwroot/cgi-bin">
Order allow,deny
Allow from all
#This is to force authentication when trying to access /awstats
#Using flat-file authentication.
<Location /stats>
AuthType Basic
AuthName "Login Required"
AuthUserFile /var/www/
require valid-user
# End of Awstats directives section

Based on the above directives, then there are several things need to be done.

Go to /usr/share/awstats/wwwroot/cgi-bin/ using the command: cd /usr/share/awstats/wwwroot/cgi-bin/ and copy Awstas default configuration (conf) file to a new file corresponding to the host name that would be monitored. i.e., Next, the new copied Awstats conf file is needed to be modified to match the domain requirements. For example, it is needed to replace the directive Host Name from the default name to or any domain name used. It is also needed to define the location of Apache (httpd) log files. In this tutorial, the location of the Apache log file (access.log) is /var/www/ It is needed to modify the location directives hence Awstats can fine it.

Below are the main directive parameters that needed to be changed / modified:

    • LogFile="/var/www/"
    • HostAliases="REGEX[^.the.domain\.com$]"
    • DirData="/var/www/"

Once the con file has been modified then it is needed to run file to create statistics data.

To create the statistics data go to folder where the file is located (/usr/share/awstats/wwwroot/cgi-bin/)

cd /usr/share/awstats/wwwroot/cgi-bin/

Issue the command: ./ -update

In order to secure the awstats page access, then a htpasswd feature is used. This will force user to have correct credential to gain access.

To Create .htpasswd file, go to the intended folder for the .htpasswd file to be stored. In this example, a folder called awstats is used. Thefore the directory must be created first, before .httpasswd is created.

Create the directory: mkdir -p /var/www/

Go to the folder intended to be used for storing the file: cd /var/www/

Then issued the command to create the .htpasswd file (choose appropriate username and password)

Command to create the file: htpasswd -c .htpasswd username then type the password twice.
Before trying to access the awstats statistics page, restart the Apache web service, by issuing the command: service httpd restart
Open a browser and go to to access the awstats page. The system will ask for valid username and password.

Once the awstats setup is successful and can be accessed, then it is needed to add a cron job in the crontab for the to run periodically, i.e., every two hours. This can be done by appending the following syntax:
0 0,2,4,6,8,10,12,14,16,18,20,22 * * * root /usr/share/awstats/wwwroot/cgi-bin/
-config=/usr/share/awstats/wwwroot/cgi-bin/ -update > /dev/null 2>&1
in the /etc/crontab file using nano or vi editor. Note that the above two lines of codes should be written in a single continuous line in the /etc/crontab file.