Back to articles
DevOps & Systems

Detailed Guide to Building a VPS-Based Global Ad Blocker with DNS over HTTPS to Block Ads and Trackers on All Devices

This guide walks users through setting up a VPS to create a global privacy filter that blocks ads and trackers using DNS over HTTPS (DoH). All DNS traffic is routed through the VPS,...

DPTCloud Engineering4 min read
Detailed Guide to Building a VPS-Based Global Ad Blocker with DNS over HTTPS to Block Ads and Trackers on All Devices

Image credit: TheDigitalArtist · Pixabay Content License

This guide walks users through setting up a VPS to create a global privacy filter that blocks ads and trackers using DNS over HTTPS (DoH). All DNS traffic is routed through the VPS, blocking ads, trackers, and malware without installing software on individual devices. The guide covers DNS configuration, setting up a DNS server, integrating with AdGuard or Pi-hole, and configuring devices to use the VPS as the default DNS. This solution is ideal for users seeking full network control, enhanced privacy, and faster page loads.

Prerequisites and Initial Setup

To begin, you need a VPS with at least 1GB RAM and 20GB SSD. The server should run Linux (Ubuntu 22.04 or Debian 11). Access the VPS via SSH using the command:

ssh root@your_vps_ip

Update the system:

apt update && apt upgrade -y

Create a non-root user to improve security:

adduser username
usermod -aG sudo username

Set a password and configure SSH to allow only key-based authentication.

Install and Configure DNS Server

We will use Unbound, a powerful open-source DNS server, combined with DNS over HTTPS for enhanced security. Install Unbound:

apt install unbound -y

Edit the Unbound configuration file at /etc/unbound/unbound.conf:

server:
    interface: 127.0.0.1@53
    access-control: 127.0.0.0/8 allow
    access-control: 192.168.0.0/16 allow
    do-ip4: yes
    do-ip6: no
    do-udp: yes
    do-tcp: yes
    hide-identity: yes
    hide-version: yes
    harden-glue: yes
    harden-dnssec-stripped: yes
    use-caps-for-id: yes
    prefetch: yes
    val-clean-additional: yes

    # DNS over HTTPS
    tls-service-key: /etc/unbound/unbound_server.key
    tls-service-pem: /etc/unbound/unbound_server.pem
    tls-port: 853
    tls-cert-bundle: /etc/ssl/certs/ca-certificates.crt

    # Ad blocking
    include: /etc/unbound/adblock.conf

Create the /etc/unbound/adblock.conf file and add common ad-blocking lists:

local-zone: "ads.example" redirect
local-zone: "tracker.example" redirect
local-data: "ads.example. IN A 127.0.0.1"
local-data: "tracker.example. IN A 127.0.0.1"

To enable DNS over HTTPS, generate an SSL certificate using Let's Encrypt:

certbot certonly --standalone -d your-domain.com

Then configure Unbound to use this certificate.

Integrate with Pi-hole (Optional)

To enhance ad-blocking capabilities, you can install Pi-hole on the same server. Install Pi-hole:

curl -sSL https://install.pi-hole.net | bash

Choose the "Basic" configuration and set the default DNS to the VPS IP address. Pi-hole will automatically download ad-blocking lists from sources like https://pgl.yoyo.org/adservers/.

Configure Client Devices

On client devices (computers, phones, routers), set the default DNS to the VPS IP address. For example, on Android:

  • Go to Settings → Network & internet → Wi-Fi
  • Select the connected Wi-Fi network → Choose Edit
  • Choose Custom DNS → Enter the VPS IP address

On a router, find the Network Settings or DNS Settings section and enter the VPS IP address as the DNS server.

Verification and Troubleshooting

To verify DNS is working, use the command:

dig your-domain.com @your_vps_ip

If it returns an IP address, DNS is working. Test ad blocking by visiting ad-heavy sites like https://www.adblocktest.com/.

If DNS fails, check:

  • Firewall rules allow ports 53 (UDP/TCP) and 853 (TLS)
  • Unbound is running: systemctl status unbound
  • Unbound configuration file has no syntax errors

References

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478