Installing Grafana with Nginx on Ubuntu 22.04

By Jennifer Mathew

Updated on May 18, 2024

In this tutorial, we'll discuss about installing Grafana with Nginx on Ubuntu 22.04 & securing with SSL. 

Grafana an open-source analytics and monitoring platform that allows you to visualize and analyze data from various sources. It supports a wide range of data sources including databases, cloud services, and custom applications.

With Grafana, you can create interactive dashboards to monitor, analyze, and understand your data in real-time.

Prerequisites:

Before you begin, ensure you have the following prerequisites:

  • Ubuntu 22.04 installed dedicated server.
  • A user account with sudo privileges
  • Basic knowledge of the Linux command line

Step 1: Update System Packages:

First, it's a good practice to update your system's package repository to ensure you have the latest versions of software packages:

sudo apt update && sudo apt upgrade

Step 2: Add the Grafana repository:

Now, you can install Grafana using the APT package manager. Run the following commands:

sudo apt install -y apt-transport-https software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"

Step 3: Install Grafana

Use following commands to install Grafana:

sudo apt update
sudo apt install grafana

After installing Grafana, start the Grafana service and enable it to start on boot:

sudo systemctl start grafana-server
sudo systemctl enable grafana-server

Access Grafana Web Interface. Grafana runs on port 3000 by default. Open your web browser and navigate to http://localhost:3000 or http://your_server_ip:3000 if you're accessing it remotely.

Step 4: Installing and Configuring NGINX

If you haven't already installed NGINX, you can do so using the following commands:

sudo apt install nginx

Now, let's configure Nginx. Create a new NGINX server block configuration file for Grafana:

sudo nano /etc/nginx/sites-available/grafana.example.com

Note: Replace grafana.example.com with your domain name.

Add the following configuration to the file. Replace grafana.example.com with your actual domain name or IP address:

server {
    listen 80;
  server_name grafana.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Save and close the file by pressing Ctrl + X, then Y, and Enter.

Next, enable the NGINX Configuration Create a symbolic link to enable the new configuration:

sudo ln -s /etc/nginx/sites-available/grafana.example.com /etc/nginx/sites-enabled/

Verify the NGINX configuration for any syntax errors:

sudo nginx -t

If there are no errors, reload NGINX to apply the changes:

sudo systemctl reload nginx

Step 5: Adjust Firewall Settings (if applicable)

If you're using a firewall, such as UFW, make sure to allow traffic on port 80:

sudo ufw allow 'Nginx Full'

Configure SELinux (Optional)

(If you have enabled SELinux, follow this step or else you can skip it.)

We need to configure SELinux to allow Nginx to connect to network services.

sudo setsebool -P httpd_can_network_connect 1

If you are still get an erro 500 or something like that, add tcp 3000 port in SELinux using following command:

sudo semanage port -a -t http_port_t -p tcp 3000

If you are still getting error or unable to access Grafana, disable SELinux and check.

Step 6: Configure HTTPS

For added security, consider configuring HTTPS for your Grafana instance. You'll need an SSL certificate for your domain. You can obtain a free SSL certificate from Let's Encrypt using Certbot.

sudo apt install certbot python3-certbot-nginx

Then run the following command to obtain and install the SSL certificate:

sudo certbot --nginx -d grafana.example.com

Follow the prompts to configure HTTPS with Certbot.

After configuring HTTPS, restart NGINX to apply the changes:

sudo systemctl restart nginx

Now your Grafana instance should be accessible via HTTPS.

You should now be able to access Grafana via your domain name or IP address. Open your web browser and navigate to https://grafana.example.com. NGINX will reverse proxy requests to Grafana running on port 3000.

Conclusion:

In this tutorial, we have discussed about installing Grafana with Nginx on Ubuntu 22.04 & securing with SSL. Grafana is a powerful tool for monitoring and analyzing data, and with its flexible and intuitive interface, you can create insightful dashboards to gain valuable insights from your data.