banner
 Sayyiku

Sayyiku

Chaos is a ladder
telegram
twitter

Debian 11 uses Docker to install Plausible Analytics for self-hosted website statistics.

This article will guide you on how to install Plausible Analytics, a self-hosted website analytics tool, on Debian 11 using Docker.

PS: This article is also applicable to any Linux distribution that supports Docker.

Why Self-Hosted Website Analytics?#

The reason is simple: you want to keep your website data under your control. Do you want your website data to be sold to "so-called" big data analytics companies by third parties?

Plausible Analytics is a privacy-focused website analytics software that can meet all your needs and can replace commercial products like Google Analytics after a few months of trial.

Installing Plausible Analytics#

It is recommended to install Plausible Analytics in the /opt/plausible directory:

mkdir -p /opt/plausible
cd /opt/plausible

First, we need to create a docker-compose.yaml file. Modify the parameters according to your requirements:

version: "3.8"
services:
  mail:
    image: bytemark/smtp
    restart: always

  plausible_db:
    image: postgres:12
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres
    restart: always

  plausible_events_db:
    image: yandex/clickhouse-server:21.3.2.5
    volumes:
      - event-data:/var/lib/clickhouse
      - ./clickhouse/clickhouse-config.xml:/etc/clickhouse-server/config.d/logging.xml:ro
      - ./clickhouse/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/logging.xml:ro
    ulimits:
      nofile:
        soft: 262144
        hard: 262144
    restart: always

  plausible:
    image: plausible/analytics:latest
    command: sh -c "sleep 10 && /entrypoint.sh db createdb && /entrypoint.sh db migrate && /entrypoint.sh db init-admin && /entrypoint.sh run"
    depends_on:
      - plausible_db
      - plausible_events_db
      - mail
      - geoip
    volumes:
      - ./geoip:/geoip:ro
    ports:
      - 127.0.0.1:8000:8000
    env_file:
      - plausible-conf.env
    restart: always

  geoip:
    image: maxmindinc/geoipupdate
    env_file:
      - geoip.env
    volumes:
      - ./geoip:/usr/share/GeoIP

volumes:
  db-data:
    driver: local
  event-data:
    driver: local
  geoip:
    driver: local

Then, create a geoip folder and a plausible-conf.env file in the same directory:

mkdir -p geoip
touch plausible-conf.env
touch geoip.env

Modify the plausible-conf.env file according to the configuration guide on the official website. Assuming your website URL is https://stat.example.com/, an example configuration is as follows:

ADMIN_USER_EMAIL=admin@example.com
ADMIN_USER_NAME=admin
ADMIN_USER_PWD=adminpassword
BASE_URL=https://stat.example.com/
SECRET_KEY_BASE=randomcharacters
MAILER_EMAIL=notification@example.com
SMTP_HOST_ADDR=SMTPhostname
SMTP_HOST_PORT=SMTPport
SMTP_USER_NAME=SMTPusername
SMTP_USER_PWD=SMTPpassword
DISABLE_REGISTRATION=true
GEOLITE2_COUNTRY_DB=/geoip/GeoLite2-Country.mmdb

The SECRET_KEY_BASE should be a 64-character random string. You can generate it using openssl rand -base64 64 or pwgen 64.

Set DISABLE_REGISTRATION to true to disable user registration.

You can use any email sending service available on the market for SMTP, or you can use free services like Gmail. You can also set up your own Mailcow server. The tutorial is available here.

Next, register an account on Maxmind. After successful registration, go to the left menu, click on Account > Manage License Keys, and click Generate new license key to get a License key. Note down your Account ID and the License key.

Modify the geoip.env file and enter the following information:

GEOIPUPDATE_EDITION_IDS=GeoLite2-Country
GEOIPUPDATE_FREQUENCY=168 
GEOIPUPDATE_ACCOUNT_ID=yourAccountID
GEOIPUPDATE_LICENSE_KEY=yourLicenseKey

Fetch the images and start the containers:

docker-compose pull
docker-compose up -d

After the containers are started, you can access Plausible at http://127.0.0.1:8000/. If you want to serve it externally, you need to configure Nginx as a reverse proxy.

After restarting Nginx, you can access it at https://stat.example.com/.

Updating Plausible Analytics#

The universal Docker update method:

docker-compose pull
docker-compose up -d
docker system prune

Backing up Plausible Analytics#

The main backup involves backing up the database. Use the following command:

docker exec -t plausible_plausible_db_1 pg_dumpall -c -U postgres | gzip > dump_$(date +"%Y-%m-%d_%H_%M_%S").gz

This command will dump the PostgreSQL database with the current timestamp and compress it using gzip.

Uninstalling Plausible Analytics#

docker-compose down
rm -rf /opt/plausible
docker image rm postgres:12
docker image rm maxmindinc/geoipupdate:latest
docker image rm plausible/analytics:latest
docker image rm yandex/clickhouse-server:21.3.2.5
docker image rm bytemark/smtp:latest
docker volume rm plausible_db-data
docker volume rm plausible_event-data

Adding Plausible Analytics to WordPress#

To add Plausible Analytics to WordPress, modify the header.php file of your theme and add the tracking code after <?php wp_head(); ?>.

If you don't want to modify the theme, you can directly install the official plugin.

Adding Plausible Analytics to VuePress#

If you are using VuePress v1.x, modify the .vuepress/config.js file and add the following code inside module.exports:

['script', {}, `
const script = document.createElement('script');
script.async = true;
script.defer = true;
script['data-domain'] = 'yourdomain';
script.src = 'https://stat.example.com/js/plausible.js';
document.head.appendChild(script);`
],

If you are using VuePress v2.x, modify the .vuepress/config.ts file and add the following code inside export default:

['script', {}, `
const script = document.createElement('script');
script.async = true;
script.defer = true;
script['data-domain'] = 'yourdomain';
script.src = 'https://stat.example.com/js/plausible.js';
document.head.appendChild(script);`
],

Adding Plausible Analytics to Next.js#

Install the next-plausible package and use similar code:

import PlausibleProvider from 'next-plausible'

export default function MyApp({ Component, pageProps }) {
  return (
    <PlausibleProvider domain="yourdomain" customDomain="https://stat.example.com" selfHosted>
      <Component {...pageProps} />
    </PlausibleProvider>
  )
}

For more integration methods, refer to the official documentation.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.