Contents

Goatcounter

This are the steps I took to install goatcounter on a EC2 (AWS). The counter is put behind a nginx server. Using letsencrypt for SSL.

Step 1: Install goatcounter on server

Downloaded latest release for linux(AMD64)

Link: https://github.com/zgoat/goatcounter/releases

Transfer the binary to your server with SCP.

scp ./goatcounter-v1.1.2-linux-amd64 ec2:~/

Step 2: Create server

SSH into your EC2 and run:

./goatcounter-v1.1.2-linux-amd64 create -email EMAIL -domain DOMAIN 

Where email is your admin-email and domain is the domain where the goat-server will be placed.

Step 3: Run server

./goatcounter-v1.1.2-linux-amd64 serve -listen 127.0.0.1:9991 -tls none

Step 4: Configure Nginx

Add nginx domain for goat.cnille.se

First generate an SSL certificate with letsencrypt. It is required that you have installed certbot on your EC2. Then run the commands:

sudo systemctl stop nginx
sudo certbot certonly --standalone -d $DOMAIN 
sudo systemctl start nginx

Where $DOMAIN is the domain you want to use for the goatcounter.

Then add this configuration to your nginx:

server {
  listen       443 ssl http2;
  server_name  $DOMAIN;
  ssl_certificate "/etc/letsencrypt/live/$DOMAIN/fullchain.pem";
  ssl_certificate_key "/etc/letsencrypt/live/$DOMAIN/privkey.pem";
  ssl_dhparam "/etc/pki/nginx/dhparams.pem";
  ssl_session_cache shared:SSL:1m;
  ssl_session_timeout  10m;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://127.0.0.1:9991;
    proxy_redirect off;
  }
}

Step 5: Add script to webpage

Visit your goatcounter webpage to find the script-code. It can be found on: http://GOAT_COUNTER_DOMAIN/code

The code will look something like this:

<script data-goatcounter="https://DOMAIN:/count"
        async src="//DOMAIN/count.js"></script>

Step 6: Prettifying

Create alias to goatcounter to keep versionfile seperated from running file:

ln -s goatcounter-v1.1.2-linux-amd64 goatcounter

Create a service to handle the goatcounter-service. With a service you can enable it to restart if the machine restarts.

Here is my service file located in /lib/systemd/system/goatcounter.service:

[Unit]
Description=Runninggoatcounterserver
After=network.target
After=nginx.service
Requires=network.target

[Service]
Type=simple
ExecStart=/home/ec2-user/goatcounter/goatcounter serve -listen 127.0.0.1:9991 -tls none -automigrate
Restart=always
RestartSec=10
WorkingDirectory=/home/ec2-user/goatcounter
User=ec2-user

[Install]
WantedBy=multi-user.target

Once you’ve added the service file you can run the server instead by running:

systemctl start goatcounter

You can also utilize the commands:

systemctl status goatcounter
systemctl restart goatcounter
systemctl stop goatcounter