Introduction

I have tried to stay away from Google Analytics since 2013, I have explored some Google Analytics Alternatives, because I wanted to avoid setting cookies on my visitor’s browsers.

I have installed Pikik, now known as Matomo, also in 2013, and run my self-hosted instance for some years, but then I run out of time and left my blog running with no attention from my part, being a static generated blog, I just set it on an Apache server, and removed everything else.

Sin April this year, I have decided to pay attention to my blog again, and wanted to see some statistics, fortunately and thanks to Marko Saric I have came to Plausible, I have tested it and wrote a Plausible Analytics review post, where it can be seen that I really like the solution.

I like it, because it respects privacy, just like Matomo, but I prefer its simple and clean interface over Matomo’s one.

Self-hosting

I really enjoy self-hosting everything, even self-hosting email, so, as soon as I knew Plausible was ready to self-host, I wanted to give it a try, some days ago, I read this post and finally decided to try to self-host Plausible, I followed the instructions in that page, and also read the official instructions and adapted it a little.

I decided to write this post, so anyone can follow this step by step guide and end-up with a Plausible Analytics server running on Docker. So, let’s do it.

Preparing the server

You will need a host server, I will use Ubuntu 20.04, with Docker and Docker Compose installed on it.

sudo apt update && sudo apt upgrade

Then

sudo apt install docker.io docker-compose

Docker Compose

Following both guides previously mentioned, I have come with this docker-compose.yml file:

version: "2.3"

services:
  plausible:
    image: plausible/analytics:latest
    restart: unless-stopped
    command: sh -c "/entrypoint.sh db migrate && /entrypoint.sh run"
    depends_on:
      - db
      - clickhouse
      - geoip
    ports:
      - 8589:8000 # Use any host's port you want, I am using 8589, because I have it free to use with Caddy.
    environment:
      - SECRET_KEY_BASE=4da5858b289e07537d3cebb29e62be4bc2de04b76531384d13173057a492e89e63e3bf27e4414ab877746dfc52a13a37e843ca44d9e5a09509986ec4003d899b # Change this key
      - SIGNING_SALT=0e4e49e91d2d4e49ede4881d1ace6d89 # Change this key too
      - DATABASE_URL=postgres://plausible:plausible@db:5432/plausible
      - DISABLE_REGISTRATION=true
      - DISABLE_SUBSCRIPTION=true
      - CLICKHOUSE_DATABASE_HOST=clickhouse
      - CLICKHOUSE_DATABASE_NAME=plausible
      - CLICKHOUSE_DATABASE_USER=default
      - CLICKHOUSE_DATABASE_PASSWORD=
      - DATABASE_POOL_SIZE=20
      - CLICKHOUSE_DATABASE_POOLSIZE=20
      - HOST=plausible.yourdomain.tld # Change to the domain you wan to have Plausible Available
      - SCHEME=https
      - MAILER_ADAPTER=Bamboo.SMTPAdapter
      - MAILER_EMAIL=no-reply@yourdomain # Change this to reflect the address where emails are going to come from
      - SMTP_HOST_ADDR=mail.yourdomain # Change this to your server, you can use mailgun, or google smtp
      - SMTP_HOST_PORT=465 # Change to fit your server
      - SMTP_USER_NAME=username # This can be, user@gmail.com if you are using Google SMTP
      - SMTP_USER_PWD=you-pass # Change with your SMTP password
      - SMTP_HOST_SSL_ENABLED=true
      - GEOLITE2_COUNTRY_DB=/geoip/GeoLite2-Country.mmdb
    volumes:
      - geoip:/geoip:ro

  clickhouse:
    image: yandex/clickhouse-server:latest
    restart: unless-stopped
    volumes:
      - ./clickhouse:/var/lib/clickhouse
    ulimits:
      nofile:
        soft: 262144
        hard: 262144

  db:
    image: postgres:12-alpine
    restart: unless-stopped
    volumes:
      - ./postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=plausible
      - POSTGRES_USER=plausible

  db-backups:
    image: prodrigestivill/postgres-backup-local:12-alpine
    restart: unless-stopped
    volumes:
      - ./scratch/db-backups:/backups
    depends_on:
      - db
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_PASSWORD=plausible
      - POSTGRES_USER=plausible
      - POSTGRES_DB=plausible
      - BACKUP_KEEP_MONTHS=1
      - BACKUP_KEEP_WEEKS=1


  geoip:
    image: maxmindinc/geoipupdate
    environment:
      - GEOIPUPDATE_ACCOUNT_ID=123456 # Change with your own key, you can sign-up at: https://www.maxmind.com/en/geoip2-services-and-databases
      - GEOIPUPDATE_LICENSE_KEY=asdfasfdasdfadsf # Change it with your own key
      - GEOIPUPDATE_EDITION_IDS=GeoLite2-Country
      # update every 7 days
      - GEOIPUPDATE_FREQUENCY=168
    volumes:
      - geoip:/usr/share/GeoIP

  caddy:  ## Use this section, only if you are not already using a reverse proxy, like Caddy, or Nginx in front your you applications
    container_name: caddy
    restart: always
    ports:
       - '80:80'
       - '443:443'
    volumes:
       - '/var/www:/usr/share/caddy'
       - 'caddy_data:/data'
       - 'caddy_config:/config'
       - '$PWD/caddy.conf:/etc/caddy/Caddyfile'
    image: 'caddy:2.1.1-alpine'

volumes:
  geoip:
    driver: local
  caddy_data: # Remove this if you are not going to use Caddy
  caddy_config: # Remove this if you are not going to use Caddy
    

Initial Setup

Once you have your docker-compose.yml file created, it is time to setup Plausible, actually it is time to create and setup the databases, I have followed The Orange One instructions, but added one line in order to create the database, otherwise you end up with an error.

docker-compose up -d db clickhouse

Now, attach the plausible container in order to create and migrate the database

docker-compose run plausible sh

Once inside, create the database:

/entrypoint.sh db createdb

And run the migrations script:

/entrypoint.sh db migrate

Now, we will create the admin account, to do so, run these commands:

export ADMIN_USER_NAME=admin
export ADMIN_USER_EMAIL=your_email
export ADMIN_USER_PWD=your_password
/entrypoint.sh db init-admin
exit # Exit the container

Final steps

You need to add in your DNS provider a record to point your domain, to the IP where you are installing Plausible Analytics. And if you are using the Caddy section I am using in the above docker-compose.yml file, you need to create a simple caddy.conf file in the same folder, it should have at least this:

plausible.yourdomain.tld { # Should be the same as the HOST variable in the Plausible section of the docker-compose.yml file
        reverse_proxy 45.79.195.104:8589 # Use your IP and the port you are using on ports in plausible section of docker-compose.yml file
}

We are now ready to restart the container

docker-compose down

And run it for the first time:

docker-compose up -d

Conclusion

Privacy is important, and one should always try to respect it, the visitors to your site, do not want to be tracked, plausible and other solutions like Matomo, or GoAccess (Spanish) or AwStats do respect it.

You can get an account at Plausible Analytics or host it yourself using these instructions.