AWStats and Multiple Domains
Seasons are changing, there is no doubt. There is no other place that this becomes more evident than in the WestHost Technical Support Department. With the decrease in the temperature outside we have seen an increase in the volume of contacts we see on a weekly basis. I suppose that it is inevitable, when people migrate indoors some will probably land behind a monitor and a keyboard. We are also approaching the holiday shopping season and reliability, preparedness, and tracking have become hot topics.
Recently we have seen an increase in questions regarding site statistics and how to best utilize some of our applications to analyze your site traffic. WestHost offers a great statistical application called AWStats. AWStats gives you excellent statistical information on who is visiting your site. As a Web master, you might use that data for the purpose of marketing or maintenance. For instance, AWStats tells you which search engine an end user used to find your site, and the keywords they typed in the search box. AWStats tells you how many visitors you’ve had over a given period of time, what pages they viewed, the browser they used, and a whole lot more. Many of you have asked if you can use AWStats to track multiple domains you have hosted within your single Virtual Private Server. The answer is yes you can. It is fairly easy. I will point out the key steps and common traps. I know that most of you don’t want to see anything too confusing, but would rather have a few things pointed out in plain English, so that is exactly what I intend to do. I’ll just go ahead and dive right in.
First of all, we need to have an idea of what AWStats really does on a fundamental basis. The application is going to parse the access logs that Apache keeps of all of the visitors to your site looking for visit information. From the chaos therein, it will create its own data files. Those data files will then be utilized to populate the pages that you see when you view your statistics. The problem is that by default, Apache will place all of the visit information into a single access log. Now that we understand what Apache and AWStats are going to do we can extrapolate what we need to do to accomplish our task. First we need to configure Apache to separate the access logs by domain and then AWStats to parse the separate logs accordingly. Simple enough right? Yep!
The first step is to configure Apache to write its separate access logs. This can be accomplished by editing the Apache configuration file, /etc/httpd/conf/httpd.conf. If you open and review this file, it can look confusing. I certainly thought so the first time I opened it up for some “surgery.” Not to worry. If you handle directions like the average Joe, you won’t have any trouble. Now one thing that you have to consider is that Linux doesn’t really like to interact with configuration files that have been written or edited by a text editor that will write files in a DOS format. If you are a Windows user and are not editing this file right on the server, just use Notepad, upload the edited file and you should be just fine. You could also utilize the text editor built into File Manager to complete your editing. Now that you know which file we will be editing, and just how we are going to do it, you probably want a few examples. For simplicity sake I am going to utilize primarydomain.com and secondarydomain.com in my explanations. Once we have the config file open we will be looking for the VirtualHost entry for the secondary domain. It will be towards the bottom of the file and will look like this:
ServerName secondarydomain.com
ServerAlias www.secondarydomain.com
DocumentRoot /var/www/secondarydomain
/VirtualHost
We are going to add a little something to it. We need to tell Apache that we want a custom log for this domain. We will be adding the CustomLog directive for this virtual host just below the DocumentRoot directive. Generally we will want to have the new logs written to the same directory as they would be by default, we would just want to write them to a new file name, maybe something like secondarydomain_access_log. Here is an example using the information above:
ServerName secondarydomain.com
ServerAlias www.secondarydomain.com
DocumentRoot /var/www/secondarydomain
CustomLog logs/secondaryddomain_access_log combined
/VirtualHost
This entry will write the access log for secondarydomain.com to /var/log/httpd/ and it will be named secondarydomain_access_log. Once the file is modified and saved or uploaded, we will want to give Apache a nice gentle restart. This can be accomplished from within Site Manager or from the command line by issuing the “apachectl graceful” command. There is a catch to writing to separate access logs. By default they won’t be rotated. That may not sound like much but think about this, we have done such a good job to configuring Apache to write this log separately that it will happily do our bidding without question. Eventually our new log file will grow to an unmanageable size. We’ll need to talk about this later on. For now, let’s get back to our task at hand.
Now that we have got our logs separated, let’s tell AWStats what to do with them. We need new, separate config files for each of the newly separated domains. Relax, we have good templates to work with. The AWStats configuration files are located in /etc/awstats. Take a quick look at this directory. As long as you have installed AWStats you will see a file named awstats.primarydomain.com.conf. Let’s make this a simple as possible, just make a copy of that file, but name it awstats.secondarydomain.com and save it to /etc/awstats. Now on to the fun stuff, we need to edit our new file, open the file up and let’s get to work. The first directive we need to find is the log file directive. Look for a line like the example below:
As you can see by default this configuration will pull data from the standard log. Simply change this file to point to our new log file(s). Make it look similar to the example below.
Next we need to change the SiteDomain and HostAliases directives to reflect the secondary domain name rather than the primary domain name. They will look like this and will be on line 153 and 168 or there abouts:
HostAliases="primarydomain.com www.primarydomain.com 127.0.0.1 localhost"
Modify them to reflect the secondary domain.
HostAliases="secondarydomain.com www.secondarydomain.com 127.0.0.1 localhost"
Now just save any changes you made. There is no need to restart anything here but we do need to check our work. Let’s see just how good we did. First we need to give Apache something to log in our new access logs so open a browser and go visit your secondary domain. Then, navigate to your AWStats Web interface, http://primarydomain.com/awstats/awstats.pl?config=primarydomain.com. You will notice that we specified which set of stats we wanted to view by passing the config variable in the URL. Lets view the statistics for our secondary domain now, navigate to http://primarydomain.com/awstats/awstats.pl?config=secondarydomain.com. Upon initial access you may think that broke something. The statistics are not processed automatically. Not to worry, the developers of AWStats thought of this and provided us a link right at the top of the page to “update now.” Don’t hesitate, click that button, you know you want to. If all went well and you didn’t stress too much you should see a result that will contain stats that have been collected since we restarted Apache. Way to go! We are almost done.
Earlier I had made mention that the new access logs wouldn’t be rotated and eluded to the fact they would just continue to grow until they filled your disk space I also highly doubt that you want to click the “update now” button and wait for AWStats to finish updating each domain each time you want to view your statistics. Your account will already be scheduled for log rotation on a daily basis, that is unless you have edited your crontab. Ah yes, the crontab, what a wonderful tool, but we’ll leave that for another discussion. Back to logrotate, we simply need to ensure that our new logs and the AWStats updates for secondary domains are included. This is also pretty slick. We need to edit the logrotate.conf file located in the /etc/ directory. Crack it open, we’ll get this done really quickly. In that file, on about the ninth line you will see the entry for the primary access log. Here is an example:
referer_log
/var/log/httpd/ssl_request_log /var/log/httpd/ssl_engine_log {
Simply add your new access log files to the end of the line, but before the {
/var/log/httpd/referer_log /var/log/httpd/ssl_request_log /var/log/httpd/
ssl_engine_log /var/log/httpd/secondarydomain_access_log {
That will rotate our log files and manage their size. This will compress and archive our logs for a short period of time. Eventually logrotate will also delete the log files but only after adding our precious stats to the AWStats data files. Logrotate is a discussion for another time as well. Don’t close that file though, we aren’t done yet. There is one more entry we need to make to cause AWStats to update before the logs are rotated. Just below prerotate on about the 13th line of the file, you will see the following:
We need to insert a new line just below it and add our secondary domain name. Just make it look like this:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=secondarydomain.com
Save your changes and give yourself a pat on the back. We are done! AWStats will now show you stats separated by domain name. We have discussed a couple of simple changes, but those simple changes will provide you some significant information.