Description

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.

We’ll assume:

  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 and not a datacenter clone/image, cPanel or other Centos derivative
  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 or other stats.

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:

    C6: su -c "https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm"
    C7: su -c "https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.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 documentRoot 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/example.com/stats/
    (For this example we’re using: awstats-7.0-3.el6.noarch)

  9. Next let’s alter the Apache httpd vhosts.conf file and add an entry for our new subdomain:
  10. Add this information in /etc/httpd.conf.d/vhosts.conf
    create this file if it does not exist

    You should already have a domain configured that you’d like to track the stats of like this:

  11. Next we’ll edit /etc/httpd/conf.d/awstats.conf to include information about our new subdomain at /var/www/example.com/stats/
  12. vi /etc/httpd/conf.d/awstats.conf

  13. Now let’s edit the awstats.stats.example.com.conf we created above
  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/example.com/stats”
    5. LoadPlugin=”geoip GEOIP_STANDARD /var/lib/GeoIP/GeoIP.dat”
    6. LoadPlugin=”geoip GEOIP_STANDARD /var/www/GeoIP/GeoIP.dat”
    7. (using the MaxMind direct download method)

    8. LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /var/www/GeoIP/GeoLiteCity.dat”
    9. (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. C6: service httpd restart
    C7: systemctl restart httpd.service

  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. vi /etc/cron.hourly/awstats

    Add this line before exit 0, save and exit:

    C6: service crond restart
    C7: systemctl restart crond.service

  21. We can also use htpasswd to require usernames and passwords for basic access to the directory from prying eyes
  22. Do not confuse this with security of any kind, this is easily bypassed, use mod_authz instead and load the domain over ssl

    We’ll be doing this work as root in the directory: /var/www/example.com/stats/

    (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

    vi /etc/httpd/conf/httpd.conf

    Add this to the virtualhost entry for stats.example.com:

    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 & Testing

Explanation troubleshooting basics and expectations.

  1. Testing your configuration:
  2. You should now be able to go to: http://stats.example.com/ or 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. The first place to check is always the error logs specified in your VirtualHost config above.

  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. Incorrect SElinux context:

    Fix:

    Correct SElinux context:

    You should be able to see country/city GeoIP data now if you refresh the page.

Added Reading

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

Last Modified: 15 Dec, 2015 at 04:29:25