Configuring AWStats – Multiple Domains Hosted On The Same Server

This howto will show you how to configure Apache httpd to display unique stats on http://stats.example.com from http://www.example.com for as many domains as you have configured.

    This howto assumes:

  1. You are the owner of your domain
  2. You are the owner of your system
  3. You are running an official copy of Centos 5.x or 6.x and not a datacenter clone/image, cPanel or other Centos derivitive
  4. You have SELinux enabled.

The purpose of this particular setup is to give a stats link to your customers or for your own sites that will show the actual stats as recorded by the Apache httpd log files so you or your customers can contrast and compare with Google Analytics stats.

Applicable to Centos Versions:

  • Centos 5.x
  • Centos 6.x

Requirements

Explanation of requirements.

  1. Administrative control over your domain and DNS.
  2. Root access to an official current Centos system.
  3. Apache httpd configured properly with the domain you want to track with AWStats.

Doing the Work

In this howto we will assume: you are root, you own example.com and have administrative access to create the subdomain stats.example.com.

  1. Installing the EPEL software repository, AWStats and optional components for Geo tracking:
  2. Click the EPEL link for more verbose instructions or give this command:
    su -c "http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm"
    
    Then issue this command:
    yum --enablerepo=epel install awstats GeoIP-data perl-Geo-IP
    
    

    Since there is no Centos package for the GeoLiteCity.dat you can get the single file from MaxMind as well as updated GeoIP.dat files here:

    http://www.maxmind.com/app/geolitecountry http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz http://www.maxmind.com/app/geolitecity http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

    If you choose to use only the MaxMind files directly, you can put them in /var/www/GeoIP as follows: sudo mkdir /var/www/GeoIP; cd /var/www/GeoIP sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz sudo gunzip GeoIP.dat.gz GeoLiteCity.dat.gz

  3. After successful install of the above software we will make a copy of the default AWStats config that we can use over and over:
  4. Copy default conf so we always have a clean starting point if something goes wrong.
    cp /etc/awstats/awstats.model.conf /etc/awstats/awstats.model.conf.orig
    
    Make another copy for our example site.
    cp /etc/awstats/awstats.model.conf.orig /etc/awstats/awstats.stats.example.com.conf
  5. Next we create the directories for the new subdomain stats.example.com. For security purposes the default Apache httpd docroot in Centos is /var/www/ :
  6. mkdir /var/www/stats.example.com && mkdir /var/www/stats.example.com/cgi-bin
  7. Next we copy the AWStats program files to our newly created stats.example.com/cgi-bin directory:
  8. cd /usr/share/awstats/wwwroot/ && cp -R * /var/www/stats.example.com/
    (For this example we're using: awstats-6.95-1.el5)
  9. Next let’s alter the Apache httpd conf file and add an entry for our new subdomain:
  10. vi /etc/httpd/conf/httpd.conf add this information at the bottom in the vhosts section or in /etc/httpd.conf.d/vhosts.conf either will work:
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    You should already have a domain configured that you'd like to track the stats of like this:
    
    <VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ScriptAlias /cgi-bin/ /var/www/example.com/cgi-bin/
    CustomLog logs/example.com_access_log combined
    ErrorLog logs/example.com_error_log
    </VirtualHost>
    
    We'll be adding this subdomain to track the above domains stats:
    
    <VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName stats.example.com
    DocumentRoot /var/www/stats.example.com
    ScriptAlias /cgi-bin/ /var/www/stats.example.com/cgi-bin/
    CustomLog logs/example.com.stats_access_log combined
    ErrorLog logs/example.com.stats_error_log
    ## AWstats ##
    Alias /classes "/var/www/stats.example.com/classes/"
    Alias /css "/var/www/stats.example.com/css/"
    Alias /icon "/var/www/stats.example.com/icon/"
    ScriptAlias /awstats/ "/var/www/stats.example.com/cgi-bin/"
    ## End AWstats ##
    </VirtualHost>
  11. Next we’ll edit /etc/httpd/conf.d/awstats.conf to include information about our new subdomain at /var/www/stats.example.com:
  12. vi /etc/httpd/conf.d/awstats.conf
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    <Directory "/var/www/stats.example.com">
    DirectoryIndex awstats.pl
    Options ExecCGI
    Options FollowSymLinks
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
  13. Now let’s edit the awstats.stats.example.com.conf we created in Step 2:
  14. vi /etc/awstats/awstats.stats.example.com.conf

    We will be editing the following lines: 1. LogFile="/var/log/httpd/example.com_access_log" 2. SiteDomain="stats.example.com" 3. HostAliases="stats.example.com" 4. DirData="/var/www/stats.example.com/" 5. LoadPlugin="geoip GEOIP_STANDARD /var/lib/GeoIP/GeoIP.dat" (using the yum install method) 6. LoadPlugin="geoip GEOIP_STANDARD /var/www/GeoIP/GeoIP.dat" (using the MaxMind direct download method) 7. LoadPlugin="geoip_city_maxmind GEOIP_STANDARD /var/www/GeoIP/GeoLiteCity.dat" (using the MaxMind direct download method) Note: The LogFile directive must be the same as the httpd CustomLog directive for the site we actually want the stats from (example.com).
  15. Next let’s restart Apache httpd so our changes take effect:
  16. service httpd restart
  17. Next we’ll call the AWStats perl script to automatically update our stats right now:
  18. perl /var/www/stats.example.com/cgi-bin/awstats.pl -config=stats.example.com -update
  19. Finally, to update our stats automatically every hour via cron we do the following:
  20. cd /etc/cron.hourly && vi 01awstats
    
    Add this line, save and exit:
    perl /var/www/stats.example.com/cgi-bin/awstats.pl -config=stats.example.com -update > /dev/null 2>&1
    
    chmod 755 01awstats
    
    service crond restart
  21. We can also use htpasswd to secure the directory from prying eyes:
  22. We'll be doing this work as root in the directory: /var/www/stats.example.com
    
    (In the vi editor use i for insert text, esc + :wq to write and quit vi)
    
    htpasswd -s .htpasswds joe
    New password: (This is the user Joe's password to access the stats.)
    Re-type new password:
    Adding password for user joe
    
    Let's Check to see the password was actually added as expected:
    vi .htpasswds
    
    joe:{SHA}9Q+DCACuf4OZs8b0E8eR5j1aIVU=
    
    vi /etc/httpd/conf/httpd.conf
    
    Add this to the virtualhost entry for stats.example.com:
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    
    <Directory "/var/www/stats.example.com">
    AuthName "Restricted Area"
    AuthType Basic
    AuthBasicProvider file
    AuthUserFile /var/www/stats.example.com/.htpasswds
    AuthGroupFile /dev/null
    require valid-user
    </Directory>

    Now, try hitting your site at: http://stats.example.com You should be prompted for a username and password for your user.

    If it does not work, try double checking the steps or join #centoshelp or #httpd on Freenode for further assistance.

Troubleshooting

How to test

Explanation troubleshooting basics and expectations.

  1. Testing your configuration:
  2. You should now be able to go to: http://stats.example.com/cgi-bin/awstats.pl and see your sites stats.
  3. If this does not work for you, please go through the howto more slowly and double check every setting as well as your Apache httpd error logs:
  4. Common mistakes are: Typos, file permissions, file ownership, improper or conflicting Apache httpd Options directives or other Apache httpd config errors.
  5. If you do not see the GeoIP data, but the rest of AWStats works, check the SELinux file contexts for the GeoIP files:
  6. ls -alsZ /var/lib/GeoIP/
    incorrect SElinux context --> -rw-r--r--  root root system_u:object_r:var_lib_t      GeoIP.dat
    incorrect SElinux context --> -rw-r--r--  root root system_u:object_r:var_lib_t      GeoLiteCity.dat
    
    sudo chcon -t httpd_sys_content_t /var/lib/GeoIP/GeoIP.dat
    sudo chcon -t httpd_sys_content_t /var/lib/GeoIP/GeoLiteCity.dat
    
    ls -alsZ /var/lib/GeoIP/
    correct SElinux context --> -rw-r--r--  root root system_u:object_r:httpd_sys_content_t GeoIP.dat
    correct SElinux context --> -rw-r--r--  root root system_u:object_r:httpd_sys_content_t GeoLiteCity.dat
    
    You should be able to see country/city GeoIP data now if you refresh the page.
    

Common problems and fixes

Describe common problems here, including links to known common problems if located on another site

More Information

Any additional information or notes.

Disclaimer

We test this stuff on our own machines, really we do. But you may run into problems, if you do, come to #centoshelp on irc.freenode.net

Added Reading


© 2012 CentosHelp.org