As a Hong Kong VPS runs more services — Nginx, PostgreSQL, your application, Redis, Sidekiq — log files spread across /var/log/ in different formats and locations. Searching across them with grep scales poorly. Grafana Loki is a lightweight log aggregation system — inspired by Prometheus but for logs — that indexes only metadata (labels) rather than full-text, making it dramatically cheaper to run than Elasticsearch while still enabling powerful log querying and alerting.
Stack Components
- Loki — log storage and query engine (receives and indexes log streams)
- Promtail — log collector (tails log files and ships to Loki)
- Grafana — visualisation and alerting UI (query Loki with LogQL)
Step 1: Deploy Loki with Docker
<code">mkdir -p /opt/loki/{config,data}
cat > /opt/loki/config/loki-config.yml << 'EOF'
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
common:
instance_addr: 127.0.0.1
path_prefix: /loki
storage:
filesystem:
chunks_directory: /loki/chunks
rules_directory: /loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
schema_config:
configs:
- from: 2024-01-01
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
limits_config:
retention_period: 30d # Keep logs for 30 days
ingestion_rate_mb: 16
ingestion_burst_size_mb: 32
ruler:
alertmanager_url: http://localhost:9093
EOF
docker run -d \
--name loki \
--restart unless-stopped \
-p 127.0.0.1:3100:3100 \
-v /opt/loki/config:/etc/loki \
-v /opt/loki/data:/loki \
grafana/loki:latest \
-config.file=/etc/loki/loki-config.yml
docker logs loki | tail -10
Step 2: Install Promtail Log Collector
<code"># Download Promtail binary
LOKI_VERSION="3.0.0"
wget https://github.com/grafana/loki/releases/download/v${LOKI_VERSION}/promtail-linux-amd64.zip
unzip promtail-linux-amd64.zip
mv promtail-linux-amd64 /usr/local/bin/promtail
chmod +x /usr/local/bin/promtail
cat > /etc/promtail-config.yml << 'EOF'
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml # Tracks file read positions
clients:
- url: http://localhost:3100/loki/api/v1/push
scrape_configs:
# Nginx access logs
- job_name: nginx
static_configs:
- targets: [localhost]
labels:
job: nginx
host: hk-vps-prod
__path__: /var/log/nginx/access.log
pipeline_stages:
- regex:
expression: '^(?P\S+) \S+ \S+ \[(?P[^\]]+)\] "(?P\S+) (?P\S+) \S+" (?P\d+) (?P\d+)'
- labels:
method:
status:
- metrics:
http_requests_total:
type: Counter
description: "Total HTTP requests"
source: status
# Nginx error logs
- job_name: nginx-errors
static_configs:
- targets: [localhost]
labels:
job: nginx-error
host: hk-vps-prod
__path__: /var/log/nginx/error.log
# Application logs (adjust path for your app)
- job_name: application
static_configs:
- targets: [localhost]
labels:
job: myapp
host: hk-vps-prod
__path__: /var/log/myapp/*.log
# System auth log
- job_name: auth
static_configs:
- targets: [localhost]
labels:
job: auth
host: hk-vps-prod
__path__: /var/log/auth.log
# PostgreSQL logs
- job_name: postgresql
static_configs:
- targets: [localhost]
labels:
job: postgresql
host: hk-vps-prod
__path__: /var/log/postgresql/postgresql-*.log
EOF
# Systemd service
cat > /etc/systemd/system/promtail.service << 'EOF'
[Unit]
Description=Promtail Log Collector
After=network.target
[Service]
ExecStart=/usr/local/bin/promtail -config.file=/etc/promtail-config.yml
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl enable --now promtail
Step 3: Configure Grafana to Query Loki
If you already have Grafana running (from the Prometheus/Grafana monitoring guide), add Loki as a data source:
- Grafana → Configuration → Data Sources → Add data source
- Select Loki
- URL:
http://localhost:3100 - Save & Test
If starting fresh:
<code">docker run -d \ --name grafana \ --restart unless-stopped \ -p 127.0.0.1:3000:3000 \ -v /opt/grafana:/var/lib/grafana \ grafana/grafana:latest
Step 4: LogQL Queries
LogQL is Loki’s query language — similar to PromQL but for logs.
<code"># In Grafana → Explore → Select Loki data source
# All Nginx 5xx errors in last hour
{job="nginx"} | status =~ "5.."
# Count 404 errors per minute (rate query)
count_over_time({job="nginx"} | status = "404" [1m])
# Find all failed login attempts in auth.log
{job="auth"} |= "Failed password"
# Filter application errors with stack traces
{job="myapp"} |= "ERROR" | json | line_format "{{.level}}: {{.message}}"
# Search across ALL jobs simultaneously
{host="hk-vps-prod"} |= "database connection"
# Top error patterns in last 24h
topk(10, count_over_time({job="myapp"} |= "ERROR" [24h]))
Step 5: Log Alerting
<code"># Create alert rule in Grafana:
# Alerting → Alert Rules → New Alert Rule
# Example: Alert when error rate exceeds 10/minute
# Query: count_over_time({job="nginx"} | status =~ "5.." [1m]) > 10
# Condition: IS ABOVE 10
# Evaluate every: 1m
# Contact point: Email / Slack / WeChat Work webhook
# Example: Alert on failed SSH logins (possible brute force)
# Query: count_over_time({job="auth"} |= "Failed password" [5m]) > 20
# Fires when: 20+ failed logins in 5 minutes
Step 6: Dashboard for Nginx Request Analysis
<code"># Grafana dashboard JSON panel for HTTP status distribution
# In Grafana → Dashboards → New → Add panel
# Visualization: Pie chart
# Query (Loki): sum by (status) (count_over_time({job="nginx"}[5m]))
# Transforms: Extract fields → label: status
# Key panels to build:
# 1. Request rate over time (line graph)
# 2. HTTP status distribution (pie chart)
# 3. Top 10 error paths (table)
# 4. Log volume by job (bar chart)
# 5. Recent error log lines (logs panel)
Resource Requirements
| Component | RAM | CPU | Disk |
|---|---|---|---|
| Loki | 200–500 MB | Low | Depends on retention × log volume |
| Promtail | 50–100 MB | Very low | Minimal |
| Grafana | 200–400 MB | Low | ~100 MB |
| Total stack | ~500 MB–1 GB | 1–5% | Grows with log retention |
For a typical VPS with 5–10 services, expect 1–5 GB of log data per month at 30-day retention. Scale the Loki data volume accordingly when provisioning disk.
Conclusion
Grafana Loki on a Hong Kong VPS centralises logs from Nginx, PostgreSQL, your applications, and system auth into a single searchable interface — without the operational complexity and RAM requirements of Elasticsearch. LogQL queries find patterns across all services simultaneously, and Grafana alerts fire on error rate spikes or security events before they become incidents. The Loki + Promtail + Grafana stack runs in under 1 GB of RAM, making it practical alongside your application workloads on any 4 GB+ VPS plan.
Centralise your logs: Browse Server.HK Hong Kong VPS plans — a 4 GB plan comfortably runs the full Loki stack alongside your application and database.