Installing AWStats (Advanced Web Statistics) on Ubuntu 18.04 with Apache
In this tutorial, we are going to learn how to install AWStats on Ubuntu 18.04 server with Apache web server. AWStats (Advanced Web Statistics) is a free, open-source Web analytics software written in the Perl programming language. Web analytics software are used by webmasters to know how many visitors are on a site in a day/week/month, what web browser they are using, etc. It is a crucial piece of software to help grow their websites.
AWStats Features
- It can generate user statistics from server log files. Web, streaming, ftp or mail server are supported.
- It can show you number of visits, and unique visitors, visit duration.
- display information about visitors (OS, browser, IP address, screen size, search engine, keyphrase and keywords used to find your website)
- It works from command line and as CGI.
To see a full list of features, please check the official AWStats website.
Installing AWStats on Ubuntu 18.04 with Apache
First, you need to install Apache web server. Run the following command to install Apache from the default Ubuntu repository.
sudo apt install apache2
Then install AWStats. The two Perl package is used to get GeoIP information.
sudo apt install awstats libgeo-ip-perl libgeo-ipfree-perl
AWStats can collect statistics from Apache log files. It’s recommended to create separate log files for each Apache virtual host.
Configuring Apache Virtual Host
Edit your Apache virtual host file.
sudo nano /etc/apache2/sites-available/example.com.conf
First, you need to enable logging in your virtual host by adding the following lines in the <VirtualHost>
tag.
ErrorLog ${APACHE_LOG_DIR}/exmaple.com_error.log CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
Then add the following lines to the end of the file before the closing </VirtualHost>
tag.
Alias /awstatsclasses "/usr/share/awstats/lib/" Alias /awstats-icon/ "/usr/share/awstats/icon/" Alias /awstatscss "/usr/share/doc/awstats/examples/css" ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ ScriptAlias /awstats/ /usr/lib/cgi-bin/ Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Save and close the file. Run the following command to enable CGI module and restart Apache for the changes to take effect.
sudo a2enmod cgi sudo systemctl restart apache2
The AWStats web interface will be accessible at example.com/cgi-bin/awstats.pl
, but we need to make a little more configuration to make it work.
Configuring AWStats
The main configuration file is /etc/awstats/awstats.conf
. To keep your modifications when the awstats package gets updated, we copy it to a new file.
sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.exmaple.com.conf
Then edit the new file.
sudo nano /etc/awstats/awstats.example.com.conf
First, you need to change the value of LogFile
to the file name used by your Apache virtual host.
LogFile="/var/log/apache2/example.com_access.log"
Next, find the following line.
LogFormat=4
Because we used the combined
log format in Apache virtual host, so we need to change the value to 1.
LogFormat=1
Then, add your domain name in the SiteDomain
parameter. If your website uses a sub-domain, like blog.example.com, then add blog.example.com to SiteDomain
parameter.
SiteDomain="blog.example.com"
Also, add your domain name to the HostAliases
parameter.
HostAliases="blog.example.com localhost 127.0.0.1"
Save and close the file. Next, we need to allow www-data
user to read the Apache logs by using the following command.
sudo setfacl -R -m "u:www-data:rx" /var/log/apache2/
The AWStats web interface is now accessible (example.com/cgi-bin/awstats.pl
).
The AWStats cron job (/etc/cron.d/awstats
) by default updates statistics every 10 minutes. AWStats stores its data under /var/lib/awstats/
directory.
Restricting Access to AWStats Web Interface
By default, the AWStats web interface is publicly accessible. To restrict access, we can enable basic password authentication with Apache web server. Run the following command to set a password for user admin
. /etc/apache2/htpasswd
file is used to store usernames and password
sudo htpasswd -c /etc/apache2/htpasswd admin
Then edit the Apache virtual host file.
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following lines.
<Directory "/usr/lib/cgi-bin/"> AuthUserFile /etc/apache2/htpasswd AuthName "Please Enter Your Password" AuthType Basic Require valid-user </Directory>
Save and close the file. Then restart Apache for the changes to take effect.
sudo systemctl restart apache2
If you access the AWStats web interface again, you will be asked to enter username and password.
Conclusion
I hope this tutorial helped you install AWStats on Ubuntu 18.04 with Apache web server. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂
Works perfectly for me.
Thanks alot brotherman, works like a charm
Works as a charm even on raspberry.
Thanks!
Hello Xiao,
I’ve followed your Apache tutorial so my vhost conf file contains 2 different sections (http and https) I assume this the SSL log file I need to set into /etc/awstats/awstats.example.com.conf right ?
Thank you
Yes. Use the SSL log file.
Hi,
Thank you for the tutorial.
How would we configure the awstats.conf file or the vhost.conf file when we have multiple sites running as virtual hosts?
For those installing under Ubuntu 20.04 you must install the ACL package “sudo install acl” for the setfacl function to work.
Ooops “sudo apt install acl” of course.
I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your website to come back in the future.
All the best
Howdy this is kind of of off topic but I was wondering if blogs
use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding expertise so I wanted to get advice from
someone with experience. Any help would be greatly appreciated!
Howdy are using WordPress for your blog platform?
I’m new to the blog world but I’m trying to get
started and set up my own. Do you require any coding knowledge to make your own blog?
Any help would be greatly appreciated!
Very good write-up. I certainly appreciate this site.
Continue the good work!
Awesome article.
Great blog here! Also your website loads up fast! What
host are you using? Can I get your affiliate link to your host?
I wish my website loaded up as fast as yours lol
You’ve written an excellent post, and you’ve shared it
with us. Your article provided me with some unique and useful
knowledge. I appreciate you sharing this text with us.
Superkons