Last active
June 2, 2023 20:38
-
-
Save Belval/4af0e0fcacfc75585f566c44c6bad5d3 to your computer and use it in GitHub Desktop.
A script to setup goaccess on Ubuntu for nginx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install goaccess | |
echo "deb https://deb.goaccess.io/ $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/goaccess.list | |
wget -O - https://deb.goaccess.io/gnugpg.key | sudo apt-key add - | |
sudo apt-get update | |
sudo apt-get install goaccess | |
# Update logrotate so it won't create tiny files | |
echo " | |
/var/log/nginx/*.log { | |
yearly | |
maxsize 1000000000 | |
missingok | |
rotate 4 | |
compress | |
delaycompress | |
notifempty | |
create 0640 www-data adm | |
sharedscripts | |
prerotate | |
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ | |
run-parts /etc/logrotate.d/httpd-prerotate; \ | |
fi \ | |
endscript | |
postrotate | |
invoke-rc.d nginx rotate >/dev/null 2>&1 | |
endscript | |
} | |
" | sudo tee /etc/logrotate.d/nginx | |
# Create script to update goaccess.html files. I put mine in /var/www/html/ | |
echo " | |
#!/bin/bash | |
for f in /var/log/nginx/*.access.log; do | |
goaccess $f --log-format=COMBINED -a -o /var/www/html/$(echo $f | cut -d'/' -f5 | cut -d'.' -f1).html | |
done | |
" | sudo tee /usr/bin/update_goaccess.sh | |
sudo chmod +x /usr/bin/update_goaccess.sh | |
# Now you can simply call it and serve the html files! | |
sudo /usr/bin/update_goaccess.sh | |
# I automate this with cron | |
# sudo crontab -e | |
# Set it to run every 5 minutes | |
# */5 * * * * /usr/bin/update_goaccess.sh | |
# Finally, goaccess does not ship with a login so we will have to use nginx basic auth | |
sudo apt-get install apache2-utils | |
sudo htpasswd -c /etc/nginx/.htpasswd YOUR_USERNAME | |
# Update your nginx server file accordingly by adding this: | |
#auth_basic "Restricted Content"; | |
#auth_basic_user_file /etc/nginx/.htpasswd; | |
# See https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-nginx-on-ubuntu-14-04 for more detail | |
sudo systemctl restart nginx | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment