I wanted to look at my sensor dashboards from my phone when I am not at home. Grafana listens on port 3000 with plain HTTP, so I put nginx in front of it with a Let’s Encrypt certificate. This is on Ubuntu 18.04, with a public DNS name, grafana.example.com, pointing to the server, and ports 80 and 443 open.
If the server is not reachable from the internet, you need the DNS challenge instead of the HTTP challenge. Since March 2018 Let’s Encrypt also issues wildcard certificates, but only with the DNS challenge. I did not need either.
Grafana on localhost only
In /etc/grafana/grafana.ini:
[server]
http_addr = 127.0.0.1
http_port = 3000
domain = grafana.example.com
root_url = https://grafana.example.com/
root_url is used for redirects and for links in alert emails. My first try did not set it, and the login redirect sent me to localhost:3000.
sudo systemctl restart grafana-server
While you are here, change the default admin/admin password before you open anything, and keep [auth.anonymous] enabled = false unless you really want public dashboards.
nginx site
# /etc/nginx/sites-available/grafana
server {
listen 80;
server_name grafana.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
sudo ln -s /etc/nginx/sites-available/grafana /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
certbot
The certbot package in the Ubuntu 18.04 archive works, but the certbot PPA has newer versions:
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install python-certbot-nginx
sudo certbot --nginx -d grafana.example.com
certbot gets the certificate, adds the listen 443 ssl lines and certificate paths to the nginx site, and asks if HTTP should redirect to HTTPS. I said yes.
Certificates are valid for 90 days. The package installs a systemd timer (or a cron job) that runs certbot renew twice a day. Check it:
sudo certbot renew --dry-run
systemctl list-timers | grep certbot
Port 80 must stay open for the renewals too. If you close it after the first certificate, renewal fails 60 days later and you find out when the browser complains. Keep it open and let it redirect to HTTPS. While experimenting, use --dry-run or --staging, because Let’s Encrypt limits how many certificates you can request per domain per week.
Sub path instead of subdomain
If you want example.com/grafana/, set root_url = https://example.com/grafana/ and use proxy_pass http://127.0.0.1:3000/; with a trailing slash inside a location /grafana/ block. I tried this first and went back to the subdomain. It is simpler.