Installing and configuring your web analytics system with Liwan

1. Why Liwan

Initially, I tried using Google Analytics for my website, but then decided to switch to a lighter and more visitor-privacy-respecting solution.

What's wrong with GA?

  • It collects user data and transfers it to the USA. And American surveillance laws allow US intelligence agencies to access it.
  • Even with additional privacy measures, the data is still transmitted in a readable form and can be re-identified — either by Google itself or by US authorities.
  • A number of European countries have recognized the use of GA as illegal under GDPR.

I started looking for other self-hosted solutions, but kept running into heavyweight options that install a million npm packages to work, or something similar. And I quickly found Liwan through an article by Henry Gressmann — the author of this wonderful program.

It's written entirely in Rust and distributed as a single executable file. The configuration is simple, and resource consumption is minimal — just what I need for my small blog on my small VPS :)

2. Installation

The official website has instructions, I'll just describe the full process as I did it.

The entire process can and should be done under a regular user, I'll call it user. I'll use the standard port 9042, and the domain example.org.

First, you can set up an A record for the subdomain where Liwan will be located, so it points to our server. It can be the same IP address. In this example, liwan.example.org will be used.

After that, you can proceed with the installation, and by the end the DNS records should already be updated.

curl -JLO 'https://github.com/explodingcamera/liwan/releases/latest/download/liwan-x86_64-unknown-linux-musl.tar.gz'
mkdir -p ~/.local/bin
tar -xzf liwan-x86_64-unknown-linux-musl.tar.gz -C ~/.local/bin liwan
chmod +x ~/.local/bin/liwan
export PATH=$HOME/.local/bin:$PATH
echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc

MaxMind databases are used to get the visitor's country. Liwan can automatically download and update the database if you provide it with an API key. Or you can download it yourself. I had problems with registration, so I went the second way.

curl -LO --create-dirs --output-dir ~/.local/share/liwan https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb

Next, you can create a configuration file and edit it.

liwan generate-config
mv liwan.config.toml ~/.config/liwan/
vim ~/.config/liwan/liwan.config.toml

I'll show my version:

base_url="https://liwan.example.org"
port=9042

[geoip]
maxmind_db_path="/home/user/.local/share/liwan/GeoLite2-City.mmdb"

I have a static site and I use Nginx to serve it, so it was easiest for me to use it as a reverse proxy for Liwan.

I created a separate file /etc/nginx/sites-available/liwan:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name liwan.example.org;
    
    location / {
        proxy_pass http://127.0.0.1:9042;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Now let's enable the config and install an SSL certificate.

sudo ln -s /etc/nginx/sites-available/liwan /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d liwan.example.org

Next, it's best to run liwan manually from the console once. It will perform the initial setup and provide a secret URL where you can create your first project and entity for the site. As I understand it, a project is like a folder for separate sites, and an entity is the site itself.

Now you can create a systemd service for convenient management. I placed it in the file /etc/systemd/system/liwan.service:

[Unit]
Description=Liwan Web Analytics
After=network.target

[Service]
ExecStart=/home/user/.local/bin/liwan
Restart=on-failure
User=user

[Install]
WantedBy=multi-user.target

All that's left is to start it and make sure everything works:

sudo systemctl daemon-reload
sudo systemctl enable liwan --now
sudo systemctl status liwan

In the control panel for the entity, HTML code will be available that needs to be inserted on each page to collect analytics.

To exclude your visits from being counted, open the website and execute the following in the developer console:

localStorage.setItem('disable-liwan', 'true');

A brief summary of visits will be available at liwan.example.org, and a more detailed report in the project at liwan.example.org/p/<project-id>. Reports can be made public. For example, mine is here: https://liwan.crowsflock.org/p/main.