Run Grafana on a Public Domain using Nginx Proxy

In this article, we are going to learn to run Grafana on a public domain say something like https://<your_web_domain/>. Here is the high level steps which we will follow:

  1. Launch an EC2 instance on AWS
  2. Install Docker on EC2
  3. Run Grafana on Docker on AWS EC2
  4. Install Nginx
  5. Configure Nginx to Serve Grafana over a public

For the first two steps, please follow this article: Launching an EC2 Instance on AWS and Installing and Running Docker on EC2

Run Grafana on Docker on AWS EC2

We can run the following command to pull grafana image and run it.

docker run -d --name my-grafana-container -p 3000:3000 grafana/grafana

Explanation:

  • docker run: This is the command used to run a Docker container.
  • -d: This flag stands for “detached mode.” It runs the container in the background and prints the container ID.
  • --name my-grafana-container: This option assigns the name “my-grafana-container” to the container. The container can be referenced by this name instead of its ID.
  • -p 3000:3000: This option maps port 3000 on the host to port 3000 on the container. Grafana typically listens on port 3000 for incoming requests, so this allows you to access Grafana’s web interface through port 3000 on your host machine.
  • grafana/grafana: This specifies the Docker image to use for creating the container. In this case, it’s pulling the official Grafana image from the Docker Hub repository named “grafana.”

Now technically, we can access grafana by providing the public IP address, followed by the port number 3000. E.g if your public IP address is this: 3.88.71.204 then you can access grafana at http://3.88.71.204:3000

Using External Storage for Grafana’s Docker Container:

Since Grafana uses SQLITE database to keep it’s application related data. This database is currently created inside the docker container’s mount point. Once you stop your docker container and remove it, all the data related to your Grafana instance will be removed as well. So to avoid this scenario, we need to mount local storage to the Grafana’s docker container. Here are the steps to follow for that:

docker volume create grafana-storage

If you want to know where this storage is created, you can run docker volume inspect command. Here is how you can run it.

docker volume inspect grafana-storage

# Sample Output

[
    {
        "CreatedAt": "2024-05-08T06:31:20Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/grafana-storage/_data",
        "Name": "grafana-storage",
        "Options": null,
        "Scope": "local"
    }
]

Remove Grafana’s container on Docker

Now let’s remove the previous running Grafana container. We will have to stop the container first before removing it. Use the following commands to do that.

docker stop my-grafana-container
docker rm my-grafana-container

Running Grafana on Docker Container with Local Volume Mapping

docker run -d --name my-grafana-container -p 3000:3000 -v grafana-storage:/var/lib/grafana grafana/grafana grafana/grafana

You should see grafana container started and running now. Here is a sample screenshot of how it’s shown:

Now if you go the the directory of local storage which is mounted to Grafana Container, you should see grafana related directories being created there.

Installing and Setting up Nginx

Let’s install and configure Nginx now to forward traffic from our domain name to Grafana backend.

Installing Nginx on EC2

Here are the set of commands we need to run to install, start and check the status of nginx.

sudo yum install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

Configuring Nginx to send traffic to Grafana

Let’s move to Nginx configuration directory (typically located at /etc/nginx/conf.d/ ) and create a configuration file called nginx-grafana.conf

server {
    listen 443 ssl;
    server_name <your_domain_name>;

    ssl_certificate /etc/nginx/conf.d/<path-to-the-domain-certificate>;
    ssl_certificate_key /etc/nginx/conf.d/<path-to-the-domain-certificate-key>;

    access_log /var/log/nginx/nginx_grafana_access.log;
    error_log /var/log/nginx/nginx_grafana_error.log;

    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;
    }

}

Ensure to replace these with the actual values in your environment:

<your_domain_name> : Replace it with the domain name which you want to use to point to Grafana.
<path-to-the-domain-certificate> : Replace it with the path to your certificate.
<path-to-the-domain-certificate-key> : Replace it with the path to your certificate’s key.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top