How to Install and Configure Prometheus + Grafana on a Linux Cloud VPS
How to Install and Configure Prometheus + Grafana on a Linux Cloud VPS
Problem statement: Modern applications need real‑time visibility into CPU, memory, disk I/O, and custom application metrics. Without a proper monitoring stack, you’re forced to react to incidents after they happen, leading to downtime and lost revenue. This tutorial walks you through provisioning a minimal Cloud VPS, installing Prometheus for metrics collection, and visualizing those metrics with Grafana.
Prerequisites
- A fresh Linux server (Ubuntu 22.04 LTS recommended) with root or sudo access.
- Basic knowledge of systemd services and firewall configuration.
- SSH client installed on your workstation.
- Port 22 (SSH) open, and ports 9090 (Prometheus) and 3000 (Grafana) allowed in the firewall.
Step 1 – Provision a Cloud VPS
Start with a lightweight instance that gives you enough CPU and RAM for both Prometheus and Grafana. You can rely on DevNix Cloud VPS to spin up a 1 vCPU / 1024 MB RAM server in minutes. After the order completes, you’ll receive an IP address and SSH credentials.
Step 2 – Secure the Server and Install Essentials
2.1 Update the OS
sudo apt update && sudo apt upgrade -y
2.2 Install required packages
sudo apt install -y curl wget gnupg2 software-properties-common
2.3 Harden SSH (optional but recommended)
sudo sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
Step 3 – Install Prometheus
3.1 Create a dedicated user
sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus
3.2 Download the latest binary
Check the official download page for the current version. The example below uses v2.53.0.
wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
tar xzf prometheus-2.53.0.linux-amd64.tar.gz
sudo mv prometheus-2.53.0.linux-amd64/prometheus /usr/local/bin/
sudo mv prometheus-2.53.0.linux-amd64/promtool /usr/local/bin/
sudo mkdir -p /etc/prometheus /var/lib/prometheus
sudo cp -r prometheus-2.53.0.linux-amd64/consoles /etc/prometheus/
sudo cp -r prometheus-2.53.0.linux-amd64/console_libraries /etc/prometheus/
3.3 Set ownership
sudo chown -R prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool /etc/prometheus /var/lib/prometheus
3.4 Create the main configuration file
sudo tee /etc/prometheus/prometheus.yml > /dev/null <<'EOF'
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
EOF
3.5 Define a systemd service
sudo tee /etc/systemd/system/prometheus.service > /dev/null <<'EOF'
[Unit]
Description=Prometheus Monitoring
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
EOF
3.6 Start and enable Prometheus
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus
sudo systemctl status prometheus
Step 4 – Install Node Exporter (system metrics)
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.0/node_exporter-1.8.0.linux-amd64.tar.gz
tar xzf node_exporter-1.8.0.linux-amd64.tar.gz
sudo mv node_exporter-1.8.0.linux-amd64/node_exporter /usr/local/bin/
sudo useradd --no-create-home --shell /usr/sbin/nologin nodeusr
sudo chown nodeusr:nodeusr /usr/local/bin/node_exporter
sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<'EOF'
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=nodeusr
Group=nodeusr
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=default.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
Step 5 – Install Grafana
5.1 Add Grafana APT repository
sudo apt-get install -y apt-transport-https software-properties-common wget
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 /etc/apt/sources.list.d/grafana.list
sudo apt update
5.2 Install the package
sudo apt install -y grafana
5.3 Enable and start Grafana
sudo systemctl enable --now grafana-server
sudo systemctl status grafana-server
Step 6 – Configure Firewall (UFW example)
sudo ufw allow OpenSSH
sudo ufw allow 9090/tcp # Prometheus UI
sudo ufw allow 3000/tcp # Grafana UI
sudo ufw enable
Step 7 – Connect Grafana to Prometheus
- Open a browser and navigate to
http://<VPS_IP>:3000. Default credentials areadmin / admin(you’ll be prompted to change the password). - In Grafana, click **Configuration → Data Sources → Add data source**.
- Select **Prometheus**.
- Set the URL to
http://<VPS_IP>:9090and click **Save & Test**. You should see a green confirmation. - Import a ready‑made dashboard (e.g., “Node Exporter Full”) from the Grafana dashboard repository to instantly visualize CPU, memory, disk, and network metrics.
Step 8 – Optional: Enable Persistent Storage for Prometheus
Prometheus stores time‑series data under /var/lib/prometheus. For production use, mount a separate SSD volume or use a network block device to avoid data loss during OS upgrades.
# Example: attach a 20 GB volume (assuming /dev/vdb)
sudo mkfs.ext4 /dev/vdb
sudo mkdir -p /mnt/prometheus
sudo mount /dev/vdb /mnt/prometheus
sudo rsync -a /var/lib/prometheus/ /mnt/prometheus/
sudo umount /var/lib/prometheus
sudo mount /dev/vdb /var/lib/prometheus
sudo chown -R prometheus:prometheus /var/lib/prometheus
Conclusion
With a modest Cloud VPS you now have a fully functional monitoring stack: Prometheus scrapes system metrics via Node Exporter, stores them efficiently, and Grafana renders them in real‑time dashboards. The setup is lightweight enough for small‑to‑medium services yet scales by adding more scrape targets or remote storage back‑ends as your infrastructure grows. Regularly review alert rules and expand your dashboards to cover application‑specific metrics, and you’ll stay ahead of performance bottlenecks before they impact users.