Let’s say we want to setup awstats to display at the address https://server.example.com/subdirectory/awstats. Here’s a way to do that.

Install Awstats

This will assume that your awstats web directory (the css, js and such) are in /usr/share/awstats/wwwroot, but your distro may be different. An easy way to determine this is by installing awstats, and then checking in Apache’s conf.d directory for the awstats.conf file. It should have a section:

...
Alias /awstatsclasses "/usr/share/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/share/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/share/awstats/wwwroot/icon/"
...

Which you can then use to match the three location ^~ /awstats____ entries below.

NGINX config

Here is the nginx config for https://server.example.com/subdirectory/awstats:

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name  server.example.com;
    
    # keys, log, and settings here
    

    location ^~ /awstatsicons {
        auth_basic            "Restricted Area";
        auth_basic_user_file  htpasswd;
        alias /usr/share/awstats/wwwroot/icon/;
    }
    location ^~ /awstatscss {
        auth_basic            "Restricted Area";
        auth_basic_user_file  htpasswd;
        alias /usr/share/awstats/wwwroot/css/;
    }
    location ^~ /awstatsclasses {
        auth_basic            "Restricted Area";
        auth_basic_user_file  htpasswd;
        alias /usr/share/awstats/wwwroot/classes/;
    }

    location  /subdirectory/awstats {
      auth_basic            "Restricted Area";
      auth_basic_user_file  htpasswd;
      # use this to redirect https://server.example.com/subdirectory/awstats to awstats.
      return 301 $scheme://server.example.com/subdirectory/awstats/awstats.pl;
    }

    # this controls the cgi script
    location ~ ^/subdirectory/awstats/.*\\.(cgi|pl|py|rb) {

        auth_basic            "Restricted Area";
        auth_basic_user_file  htpasswd;
       
        # gzip off;
        include uwsgi_params;
        uwsgi_modifier1 9;
        uwsgi_pass unix:/run/uwsgi/awstats.sock;

    }
    
}

uWSGI config

Now install uwsgi, if you haven’t already.

sudo apt-get --no-install-recommends install uwsgi
dnf install uwsgi uwsgi-router-basicauth
pacman -Sy uwsgi

And then use this for your /etc/uwsgi.d/awstats.ini

[uwsgi]
plugins      = router_basicauth, cgi
socket       = /run/uwsgi/awstats.sock
# set your password here, same as what's in the above htpasswd
# format:      ^/ basicauth:[name],[user]:[password]
route        = ^/ basicauth:Restricted Area,secretname:secretpassword
uid          = nobody
gid          = nobody
processes    = 2
threads      = 2
# location of your awstats.pl script
cgi          = /usr/share/awstats/wwwroot/cgi-bin/awstats.pl
chmod-socket = 666
harakiri     = 30

For completeness, this was my main /etc/uwsgi.ini

[uwsgi]
uid = uwsgi
gid = uwsgi
# your pid file may be different
pidfile = /run/uwsgi/uwsgi.pid
emperor = /etc/uwsgi.d
stats = /run/uwsgi/stats.sock
chmod-socket = 660
emperor-tyrant = true
cap = setgid,setuid

Be sure to check the logs once you start it, to

  1. make sure it’s working
  2. enjoy the funny logs:
... announcing my loyalty to the Emperor...
... [emperor] vassal awstats.ini is now loyal
... *** RAGNAROK ALREADY EVOKED (mercyless in 30 seconds)***
... [emperor] curse the uwsgi instance awstats.ini <-(that means it messed up)
... waiting for Emperor death

Ya, uwsgi is a new favorite.

Test it

Now once those are working, go to https://server.example.com/subdirectory/awstats/awstats.pl and you should be able to see it in action. If it doesn’t, check that your paths are correct. You may want to make sure you can pull up images, which will help you verify things.

Conclusion

It’s a pretty straightforward setup. I tired doing it with php, wrapping the perl in it. But I found that uwsgi needed fewer files and was faster in the end.

Resources