Skip to content

Instantly share code, notes, and snippets.

@Numeri-Dev
Last active July 15, 2025 01:29
Show Gist options
  • Select an option

  • Save Numeri-Dev/51a245af5abb247b86a50d3d6a75cd86 to your computer and use it in GitHub Desktop.

Select an option

Save Numeri-Dev/51a245af5abb247b86a50d3d6a75cd86 to your computer and use it in GitHub Desktop.
Arch Linux Pihole Script
#!/bin/bash
# A modernized and robust installer for Pi-hole on Arch Linux.
# Exit immediately if a command exits with a non-zero status.
# Treat unset variables as an error.
# Print a trace of simple commands.
set -euo pipefail
# --- Constants and Variables ---
readonly PIHOLE_PKG="pi-hole-server"
readonly REQUIRED_PKGS=("lighttpd" "php-cgi" "php-sqlite")
readonly PHP_INI="/etc/php/php.ini"
readonly LIGHTTPD_CONF="/etc/lighttpd/lighttpd.conf"
readonly PIHOLE_EXAMPLE_CONF="/usr/share/pihole/configs/lighttpd.example.conf"
readonly SUDO_USER="${SUDO_USER:-$(whoami)}"
# --- Functions ---
# Function to print pretty messages
msg() {
echo "################################################################"
echo "##### $1"
echo "################################################################"
}
# Function to check for root privileges
check_root() {
if [[ "${EUID}" -ne 0 ]]; then
msg "This script must be run as root. Please use 'sudo'."
exit 1
fi
}
# Function to find an AUR helper
find_aur_helper() {
if command -v yay &>/dev/null; then
AUR_HELPER="yay"
elif command -v paru &>/dev/null; then
AUR_HELPER="paru"
else
msg "No AUR helper (yay/paru) found."
echo "Please install an AUR helper first."
echo "You can install yay with:"
echo " sudo pacman -S --needed git base-devel"
echo " git clone https://aur.archlinux.org/yay.git"
echo " cd yay"
echo " makepkg -si"
exit 1
fi
echo "--- Using '$AUR_HELPER' as the AUR helper."
}
# --- Main Script ---
check_root
find_aur_helper
# 1. Remove conflicting dnsmasq
if pacman -Qi dnsmasq &>/dev/null; then
msg "Removing conflicting dnsmasq package."
pacman -Rns --noconfirm dnsmasq
fi
# 2. Install Pi-hole server from AUR
if pacman -Qi "$PIHOLE_PKG" &>/dev/null; then
msg ""$PIHOLE_PKG" is already installed."
else
msg "Installing "$PIHOLE_PKG" with $AUR_HELPER."
sudo -u "$SUDO_USER" "$AUR_HELPER" -S --noconfirm "$PIHOLE_PKG"
fi
# 3. Install dependencies from official repositories
msg "Installing required dependencies: ${REQUIRED_PKGS[*]}"
pacman -S --noconfirm --needed "${REQUIRED_PKGS[@]}"
# 4. Configure lighttpd
msg "Configuring lighttpd."
if [ -f "$PIHOLE_EXAMPLE_CONF" ]; then
echo "!!! WARNING: This will overwrite your existing '$LIGHTTPD_CONF'."
echo "!!! If you have custom lighttpd settings, back them up first."
cp "$PIHOLE_EXAMPLE_CONF" "$LIGHTTPD_CONF"
echo "--- Copied Pi-hole example config to $LIGHTTPD_CONF"
else
msg "Could not find Pi-hole's example lighttpd config."
echo "Path not found: $PIHOLE_EXAMPLE_CONF"
exit 1
fi
# 5. Configure PHP
msg "Configuring PHP in '$PHP_INI'."
readonly PHP_EXTENSIONS=("pdo_sqlite" "sockets" "sqlite3")
for ext in "${PHP_EXTENSIONS[@]}"; do
if grep -q ";extension=${ext}" "$PHP_INI"; then
echo "--- Enabling PHP extension: ${ext}"
sed -i "s/;extension=${ext}/extension=${ext}/" "$PHP_INI"
elif grep -q "extension=${ext}" "$PHP_INI"; then
echo "--- PHP extension '${ext}' is already enabled."
else
echo "!!! WARNING: Could not find 'extension=${ext}' in $PHP_INI."
echo "!!! Appending it as a fallback. Please review your php.ini."
echo "extension=${ext}" >> "$PHP_INI"
fi
done
# 6. Enable and start services
msg "Enabling and restarting lighttpd service."
systemctl enable --now lighttpd.service
# 7. Set Pi-hole admin password
msg "Set Pi-hole Admin Password"
echo "--- You will now be prompted to set the web interface password."
pihole -a -p
msg "Pi-hole installation and configuration finished!"
echo "--- Your Pi-hole admin interface should be available at:"
echo "--- http://$(hostname -I | awk '{print $1}')/admin"
echo "################################################################"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment