echo "Update prerequisite packages"
sudo apt-get install ca-certificates apt-transport-https -qy

echo "Update GPG key"
sudo apt-key del B188E2B695BD4743
wget -nv -O - "https://packages.sury.org/php/apt.gpg" --no-check-certificate | sudo apt-key add -
sudo su -c 'echo "deb https://packages.sury.org/php/ $distro buster main contrib non-free rpi" > /etc/apt/sources.list.d/php.list'

echo "Apt Update"
sudo apt-get -y update

echo "Install PHP8.1"
sudo DEBIAN_FRONTEND=noninteractive apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" -qy php8.1-cli php8.1-common php8.1-curl php8.1-mbstring php8.1-intl php8.1-xml php8.1-mcrypt php8.1-zip php8.1-sqlite3 libapache2-mod-php8.1

echo "Check PHP Version"
php -v

echo "Rebooting."
sudo reboot now