Monday, November 4, 2019

Awstats setup on Apache2 Ubuntu 18.04

Helpful websites: LinuxbabeLeave No Bit Unturned.
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-perl
Edit domain config file
$ sudo nano /etc/apache2/sites-available/
Append log file names to separate stats for each website you'll track.  Edit thefollowing within the <VirtualHost> tag
ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined
Add 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 +SymLinksIfOwnerMatch
Save file.  Enable the CGI module
$ sudo a2enmod cgi
Configure Awstats files for default website.  Back up the file
$ sudo cp /etc/awstats/awstats.conf /etc/awstats/awstat.conf.bak
Then edit
$ sudo nano /etc/awstats/awstats.conf
Edit the following lines as shown
HostAliases=" localhost"
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/
$ sudo chown www-data:www-data /var/lib/awstats/
Restart Apache2
$ sudo systemctl restart apache2
The stats for your main (or only) domain should be accessible now at  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 admin
Configure the Apache virtual host file
$ sudo nano /etc/apache2/sites-available/
Then add the following
<Directory "/usr/lib/cgi-bin/">
    AuthUserFile /etc/apache2/htpasswd
    AuthName "Please Enter Your Password" 
    AuthType Basic
    Require valid-user
Save the file. Restart Apache2
$ sudo systemctl restart apache2
Now 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/
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 lines
Include "/etc/awstats/awstats.conf"
HostAliases=" localhost"
Create directory structure and set permissions.
$ sudo mkdir -p /var/lib/awstats/
$ sudo chown www-data:www-data /var/lib/awstats/
Restart Apache2
$ sudo systemctl restart apache2
Cron 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/ www-data

Log File Permissions and Rotation

Edit the Apache logrotate file to give apache read permissions to everyone.
$ sudo nano /etc/logrotate.d/apache2
Edit the following line as shown
create 644 root adm
Save the file.