在本教程中,我们将学习如何在Ubuntu 20.04上安装 Grafana 8 。
Grafana 不存在于 Ubuntu 的默认存储库中,我们将添加 Grafana 的官方存储库进行安装。这可确保您拥有最新版本。
运行以下命令以添加 Grafana 存储库:
$ sudo wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
安装其他必要的包
$ sudo apt install -y apt-transport-https software-properties-common wget
更新存储库的缓存
$ sudo apt update
现在你可以使用 APT 命令安装 Grafana
$ sudo apt install grafana
您可以检查安装的版本以获取更多信息
$ grafana-server -v
Version 8.2.3 (commit: fb85ed6912, branch: HEAD)
输出显示 Grafanaversion 8.2已安装。
现在在启动时启用该服务,以便如果服务器重新启动,它也会自动启动
$ sudo systemctl enable grafana-server
Synchronizing state of grafana-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /lib/systemd/system/grafana-server.service.
您需要启动服务才能让 Grafana 正常工作
$ sudo systemctl start grafana-server
由于我们希望通过域名(或子域)访问它,因此我们将使用反向代理将通信重定向到服务器上的 Grafana。
我们将安装 Nginx 并添加证书来处理 Grafana 的所有外部请求。
Grafana 通常运行在 3000 端口,这意味着你需要在防火墙上打开该端口,并通过 IP 地址和端口访问它。
在我们的配置中,我们将使用Nginx作为反向代理来监听 80/443 端口上的请求
$ sudo apt install nginx
由于我们需要确保通信安全,我们将复制证书
$ sudo cp grafana.domain.com.crt /etc/nginx/certs/grafana.domain.com.crt
然后复制证书的key
$ sudo cp grafana.domain.com.key /etc/nginx/certs/grafana.domain.com.key
由于这是我们的第一个配置,我们需要停用默认配置以避免任何可能的冲突
$ sudo rm /etc/nginx/sites-enabled/default
是时候为 Grafana 设置配置文件了。您需要正确指明您的证书和密钥文件所在的位置,此外,默认情况下,Nginx 会将端口 80 上的所有流量重定向到端口 443 上的安全通道
$ sudo vim /etc/nginx/sites-available/jenkins.conf
Server {
server_name grafana.websitefortesting.com;
listen 80 ;
access_log /var/log/nginx/grafana.log;
return 301 https://$host$request_uri;
}
server {
server_name grafana.websitefortesting.com;
listen 443 ssl http2 ;
access_log /var/log/nginx/grafana.log;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
ssl_session_timeout 5m;
ssl_certificate /etc/nginx/certs/grafana.websitefortesting.com.crt;
ssl_certificate_key /etc/nginx/certs/grafana.websitefortesting.com.key;
add_header Strict-Transport-Security "max-age=31536000";
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
现在我们需要通过在文件夹中创建配置文件的软链接来激活配置/etc/nginx/site-enabled。
$ sudo ln -s /etc/nginx/sites-available/grafana.conf /etc/nginx/sites-enabled/grafana.conf
可以检查Nginx的配置是否良好
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
然后我们需要重启Nginx服务来考虑所有的修改
$ sudo systemctl restart nginx
现在Grafana的安装和配置都完成了,我们就可以访问了。为此,您需要打开浏览器并输入 Grafana 服务器的 URLhttp://grafana.domain.com
默认用户名和密码是admin。之后,系统将提示您更改默认密码。
这将让您直接访问您的仪表板
现在您可以开始使用您的 Grafana 并设置所有内容。如果您需要一些配置指南,可以查看官方文档。
在本教程中,我们学习了如何在 Ubuntu 20.04 上安装 Grafana。