Setup Grafana on an Ubuntu EC2 Instance

Amit Thakur
3 min readMar 23, 2022

--

In my new role as a Full Stack DevOps Engineer (yes that! 😵‍💫), i had the opportunity to setup Grafana on an EC2 Instance. I thought sharing this would help others overcome some issues they might face..

What is Grafana?

Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources. It supports several data sources such as MSSQL, MySQL, PostgreSQL, Influx, GCM etc.

Grafana has two versions Grafana Enterprise and Grafana Open Source Software.

Grafana Enterprise is a commercial version and require licensing. It comes with advance data source capabilities and additional features such as fine grain access control, exporting as PDF etc.

Here we will try to use the Grafana OSS version.

Install Grafana

Run the below commands, to install Grafana

$ sudo apt-get install -y gnupg2 curl$ curl https://packages.grafana.com/gpg.key | sudo apt-key add -$ sudo add-apt-repository “deb https://packages.grafana.com/oss/deb stable main”$ sudo apt-get update$ sudo apt-get -y install grafana

Start Grafana

Run the below commands, to start Grafana

sudo systemctl daemon-reload
sudo systemctl start grafana-server

Run the below commands, to ensure that Grafana runs automatically at boot

sudo systemctl enable grafana-server.service

Run the below commands, to check the version of Grafana installed

$ grafana-server –v

Check Grafana status

Run the below commands, to verify the status of Grafana service:

sudo systemctl status grafana-server

Watch out for any error logs in case the service failed to start. An additional helpful tip is to check the grafana.log file. To do this run the tail command

sudo tail /var/log/grafana/grafana.log

This will display the latest logs from grafana. Any errors can be easily identified here.

Run Grafana dashboard

By default, Grafana is available on port 3000. To check if Grafana is working or not, run the following command

curl -i http://localhost:3000

This should output an reference to the login page.

If you navigate to http://localhost:3000 on a local browser, it should take you to the Grafana login page.

Since the default Username and Password for Grafana is admin, you may want to change this using the ‘Forgot your password’ option

Customise Grafana

Grafana can be customised by updating the grafana.ini file stored at the below location

/etc/grafana/grafana.ini

This file allows us to specify and customise Grafana. Any line prefixed with a ; is commented.

To set a custom domain and change the root url of Grafana, we can modify the file to set the values as below:

protocol = http
http_port = 3000
domain = grafana.atdomain.com
enforce_domain = true
root_url = http://grafana.atdomain.com

To run Grafana on HTTPS, we can modify the file as below:

protocol = https
http_port = 443
domain = grafana.atdomain.com
enforce_domain = true
root_url = https://grafana.atdomain.com
cert_file = /etc/grafana/fullchain.pem
cert_key = /etc/grafana/privkey.pem

Running Grafana on HTTPS will also require us to generate a Certificate. This can be done easily using Let’s Encrypt. Once the cert and key is generated we can set the path in the file.

However, running Grafana on a port < 1024 requires an additional change.

https://grafana.com/docs/grafana/latest/installation/rpm/

This should allows us to run Grafana on https with custom domain name and url. If you have a reverse proxy tool on the machine, you will need to make some changes to the proxy config as explained here.

Now that we have Grafana running, i am looking forward to creating my own dashboard. Hope this will be helpful.

Thanks for reading.

--

--

Amit Thakur

iOS Programmer | Traveller | Reader | Cricket | LFC Fan | 🇮🇳