Awstats is a useful free program that provides website statistics. The installation in this guide was performed on a Ubuntu 18.04 server running Apache2.
Install and Configure Awstats
$ sudo apt install awstats libgeo-ip-perl libgeo-ipfree-perlEdit domain config file
$ sudo nano /etc/apache2/sites-available/your-domain.com
Append log file names to separate stats for each website you'll track. Edit thefollowing within the <VirtualHost> tagErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combinedAdd the following 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 +SymLinksIfOwnerMatchSave file. Enable the CGI module
$ sudo a2enmod cgiConfigure Awstats files for default website. Back up the file
$ sudo cp /etc/awstats/awstats.conf /etc/awstats/awstat.conf.bakThen edit
$ sudo nano /etc/awstats/awstats.confEdit the following lines as shown
LogFile="/var/log/apache2/your-domain.com_access.log" LogFormat=1 SiteDomain="your-domain.com" HostAliases="your-domain.com localhost 127.0.0.1" DirData="/var/lib/awstats/your-domain.com"Save file. Give user www-data log access permissions (the following command requires "acl")
$ sudo setfacl -R -m "u:www-data:rx" /var/log/apache2/Create directory structure to hold data and then grant permissions
$ sudo mkdir -p /var/lib/awstats/your-domain.com $ sudo chown www-data:www-data /var/lib/awstats/your-domain.comRestart Apache2
$ sudo systemctl restart apache2The stats for your main (or only) domain should be accessible now at your-domain.com/cgi-bin/awstats.pl. Cron should be set to update awstats every ten minutes by default.
Secure access to Awstats page
Now that Astats is working you'll want to restrict access. Run the following to set a password for admin. The password will be stored at /etc/apache2/htpasswd.
$ sudo htpasswd -c /etc/apache2/htpasswd adminConfigure the Apache virtual host file
$ sudo nano /etc/apache2/sites-available/your-domain.com.conf
Then add the following<Directory "/usr/lib/cgi-bin/"> AuthUserFile /etc/apache2/htpasswd AuthName "Please Enter Your Password" AuthType Basic Require valid-user </Directory>Save the file. Restart Apache2
$ sudo systemctl restart apache2Now when you access the awstats page you should be prompted to enter a user name and password.
Multiple Websites
Create an awstats config file for your other websites.
$ sudo nano /etc/awstats/awstats.your-other-domain.com.conf
An include statement at the top will bring over the settings from the default awstats file. We just need to change some of the directives from the default by adding the following linesInclude "/etc/awstats/awstats.conf" SiteDomain="your-other-domain.com" HostAliases="your-other-domain.com localhost 127.0.0.1" DirData="/var/lib/awstats/your-other-domain.com" LogFile="/var/log/apache2/your-other-domain.com_access_log"Create directory structure and set permissions.
$ sudo mkdir -p /var/lib/awstats/your-other-domain.com $ sudo chown www-data:www-data /var/lib/awstats/your-other-domain.comRestart Apache2
$ sudo systemctl restart apache2Cron will update the stats in ten minutes. If you want to check your configuration now run the following:
$ sudo -l -c /usr/share/awstats/tools/update.sh www-data
Log File Permissions and Rotation
Edit the Apache logrotate file to give apache read permissions to everyone.
$ sudo nano /etc/logrotate.d/apache2Edit the following line as shown
create 644 root adm
Save the file.