A Step-by-Step Guide to Self-Hosting Umami for Website Analytics

Understanding and analyzing website traffic is essential for businesses and website owners to make informed decisions and optimize user experiences. One popular tool for this purpose is Umami, an open-source web analytics platform that offers simplicity, privacy, and insightful data tracking.

While many analytics platforms rely on third-party services and cloud hosting, Umami provides the option of self-hosting, allowing users to have greater control over their data while ensuring compliance with data protection laws.

Self-hosting Umami enables full ownership and control over analytics data. By hosting Umami on your own servers, you can ensure that sensitive information remains within your infrastructure, reducing the risk of unauthorized access.

In this guide, I will walk you through the process of self-hosting Umami for website analytics.

Prerequisites

To self-host Umami, you will need the following:

  • PostgreSQL or MySQL database;
  • Linux Server to host the application;

Setting Up the Database

We will start by creating the required database in either PostgreSQL or MySQL.

1. Create a Database

CREATE DATABASE umamianalyticsdb;

2. Create a User

PostgreSQL:

CREATE USER umamianalytics WITH ENCRYPTED PASSWORD 'YOUR PASSWORD';

MySQL:

CREATE USER umamianalytics IDENTIFIED BY 'YOUR PASSWORD';

3. Grant Privileges

PostgreSQL:

GRANT ALL PRIVILEGES ON DATABASE umamianalyticsdb TO umamianalytics;

MySQL:

GRANT ALL ON umamianalyticsdb.* TO umamianalytics;
FLUSH PRIVILEGES;

Installing Umami

We will install Umami from the source. You can either download the source code directly from GitHub or clone the repository. In this guide, we will clone the repository into the /applications folder.

1. Clone the Repository

cd /applications
git clone https://github.com/umami-software/umami.git
cd umami

2. Configure the Database

Create an .env file with the following content:

PostgreSQL:

DATABASE_URL=postgresql://umamianalytics:<POSTGRESQL PASSWORD>@localhost:5432/umamianalyticsdb

MySQL:

DATABASE_URL=mysql://umamianalytics:<MYSQL PASSWORD>@localhost:3306/umamianalyticsdb

3. Install Node.js and Yarn

Install Node.js 18:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

Install yarn

npm install -g yarn

4. Install Dependencies and Build Umami

yarn install
yarn build

Starting Umami

After building the application, start it with:

yarn start

By default, Umami runs on http://localhost:3000 with the default login credentials:

  • Password: umami
  • Username: admin

Keeping Umami Running with Systemd

To keep the process running in the background, create a systemd service file:

nano /etc/systemd/system/umami.service

Add the following content:

[Unit]
Description=Umami Server

[Service]
ExecStart=yarn start

WorkingDirectory=/applications/umami
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=umami

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable umami
sudo systemctl start umami

Setting Up a Reverse Proxy with NGINX

To serve Umami through a domain, create an NGINX server block.

1. Create the NGINX Configuration

nano /etc/nginx/sites-available/umami

Add the following configuration:

server {
  server_name analytics.YOURDOMAIN.TLD;

  location / {
    proxy_pass http://<UMAMI_SERVER_IP>:3000;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

2. Validate and Restart NGINX

nginx -t
service nginx restart

3. Enable SSL with Certbot

certbot --nginx -d analytics.YOURDOMAIN.TLD

Upgrading Umami

To upgrade to the latest version:

1. Fetch the Latest Tagged Release

git fetch --all --tags

2. Checkout the Latest Version (Example: v2.16.1)

git checkout tags/v2.16.1 -b v2.16.1

3. Reinstall Dependencies and Build Umami

yarn install
yarn build

4. Restart the Umami Service

  service umami restart

By following this guide, you have successfully self-hosted Umami, giving you full control over your website analytics. This setup ensures data privacycompliance, and flexibility while providing valuable insights into your website traffic.

Monitoring and Managing Spring Boot Applications in Production

Spring Boot has become a popular choice for developing web applications and microservices due to its ease of use, robustness, and powerful features. However, deploying applications into production brings its own set of challenges, especially when it comes to monitoring and managing these applications to ensure they perform optimally and remain available to users. In this article, we’ll explore strategies and tools for monitoring and managing Spring Boot applications in a production environment.

Why Monitoring and Managing Spring Boot Applications is Crucial

Before diving into the specifics of monitoring and managing Spring Boot applications, let’s first understand why it’s crucial in a production environment:

  1. Performance Optimization: Monitoring allows you to identify performance bottlenecks and optimize your application’s resource utilization.
  2. Fault Detection and Recovery: Monitoring helps detect and diagnose faults or failures in real-time, allowing for quick recovery and minimizing downtime.
  3. Scalability and Resource Planning: Monitoring helps you understand your application’s resource consumption patterns, enabling better scalability planning and resource allocation.
  4. Security and Compliance: Monitoring helps detect and mitigate security threats and ensures compliance with regulatory requirements.

Monitoring Strategies for Spring Boot Applications

Monitoring Spring Boot applications involves tracking various metrics related to performance, resource utilization, errors, and more. Here are some key strategies and metrics to monitor:

  1. Health Check Endpoints: Spring Boot provides health check endpoints, by using spring-boot-actuator, that report the application’s health status. Monitor these endpoints to ensure your application is up and running. This article about How to Monitor Your Spring Boot Application Using Spring Boot Actuator is a good start.
  2. Metrics Monitoring: Use Spring Boot Actuator to expose metrics related to JVM, CPU, memory, and other aspects of your application’s performance. You can integrate Actuator with monitoring systems like Prometheus, Grafana, or Micrometer to visualize and analyze these metrics.
  3. Logging and Log Aggregation: Implement robust logging using frameworks like Logback or Log4j2. Centralize logs using tools like ELK Stack (Elasticsearch, Logstash, Kibana) or Splunk for easier analysis and troubleshooting.
  4. Error Monitoring: Monitor error rates, exceptions, and stack traces to identify and address application errors promptly. Tools like Sentry or Rollbar can help track and alert on errors in real-time.
  5. Database Monitoring: Monitor database queries, connection pools, and response times to ensure optimal database performance. Use database monitoring tools or built-in database metrics to track these parameters.

Managing Spring Boot Applications in Production

In addition to monitoring, effective management of Spring Boot applications in production involves:

  1. Continuous Deployment and Delivery (CI/CD): Implement CI/CD pipelines to automate deployment, testing, and delivery of your Spring Boot applications. Tools like Jenkins, GitLab CI/CD, or AWS CodePipeline can streamline this process.
  2. Configuration Management: Use externalized configuration (e.g., Spring Cloud Config) to manage application settings and properties across different environments. This allows for easier configuration changes without redeploying the application.
  3. Containerization and Orchestration: Containerize your Spring Boot applications using Docker and orchestrate them using Kubernetes or Docker Swarm. Containerization provides consistency and portability, while orchestration ensures scalability and high availability.
  4. Security Hardening: Implement security best practices such as role-based access control (RBAC), HTTPS, encryption, and regular security audits to protect your Spring Boot applications from security threats.
  5. Performance Tuning: Continuously monitor and optimize your application’s performance by analyzing metrics, identifying bottlenecks, and making necessary adjustments to configurations, code, or infrastructure.

Conclusion

Monitoring and managing Spring Boot applications in production is essential for ensuring their performance, availability, and reliability. By implementing robust monitoring strategies, leveraging appropriate tools, and following best practices for production management, you can maintain healthy and efficient Spring Boot applications that meet the needs of your users and stakeholders.

Remember, monitoring and management are ongoing processes that require continuous attention and improvement. Stay proactive, iterate on your processes, and adapt to changing requirements and environments to keep your Spring Boot applications running smoothly in production.

How to Monitor Your Spring Boot Application Using Spring Boot Actuator

Monitoring plays a vital role in the life cycle of any application, offering insights into its health, performance, and essential metrics. Spring Boot Actuator, a subproject of the Spring Boot framework, provides an effortless way to add production-ready features for monitoring and managing your Spring Boot application.

Add Dependency

You need to add the spring-boot-actuator dependency to our package manager to enable the Spring Boot Actuator.

In Maven:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

Enabling Spring Boot Actuator

To initiate Spring Boot Actuator in your project, a simple configuration is required. Open the application.properties file and insert the following:

management.endpoints.web.exposure.include=*

This setting exposes all Actuator endpoints over HTTP, which can be further customized based on specific needs.

Key Actuator Endpoints

Spring Boot Actuator introduces various built-in endpoints that provide valuable information about your application. Here are some commonly used endpoints:

Health Endpoint: /actuator/health

The /health endpoint provides insights into the application’s health by examining the status of various components such as the database, disk space, and custom health indicators.

Info Endpoint: /actuator/info

The /info endpoint permits the exposure of arbitrary application information. Customize this endpoint to display details like the application version, description, or any pertinent information.

Metrics Endpoint: /actuator/metrics

The /metrics endpoint supplies a comprehensive range of application metrics, encompassing memory usage, garbage collection statistics, and custom metrics. It proves invaluable for monitoring your application’s performance.

Environnement Endpoint: /actuator/env

The /env endpoint discloses information about the application’s environment properties, aiding in understanding the configuration of your application in a production setting.

Securing Actuator Endpoints

While Actuator endpoints are potent for monitoring, it is imperative to secure them, particularly in a production environment. Access to these endpoints can be controlled by configuring security settings. For instance, access can be restricted to specific endpoints or security features like HTTP Basic Authentication can be enabled.

You can configure Actuator endpoint security with the following properties:

spring.security.user.name=USERNAME
spring.security.user.password=SECURE_PASSWORD

Customizing Actuator Endpoints

Spring Boot Actuator is highly adaptable, allowing you to tailor monitoring features to suit your application’s specific needs. Create custom health indicators, metrics, and even custom endpoints to gain more flexibility and control.

For instance, to create a custom endpoint, implement the Endpoint interface and expose it using the @Endpoint annotation. Here’s a simple example:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

@Endpoint(id = "my_custom_endpoint")
public class CustomEndpoint {

    @ReadOperation
    public String customEndpoint() {
        return "Hello from the custom endpoint!";
    }
}
management.endpoints.web.exposure.include=health,info,my_custom_endpoint

Conclusion

Spring Boot Actuator proves to be a robust tool for monitoring and managing Spring Boot applications. Utilizing its built-in features and customization options, you can gain valuable insights into your application’s health and performance. Always remember to secure Actuator endpoints appropriately, especially in production, to safeguard the confidentiality of sensitive information.