This note is based off the excellent Linuxize.com. I recommend going to their site for a more detailed tutorial, and more tutorials in general. I’m writing my own version because I want a copy in case their site shuts down, and because my own notes are a quicker reference than searching for a specific article online.

Add Debian developer Ondřej Surý’s repository that includes multiple PHP versions.

sudo apt install software-properties-common

If you’re using Ubuntu:

sudo add-apt-repository ppa:ondrej/php

If you’re using Debian:

sudo apt install -y apt-transport-https lsb-release ca-certificates curl
curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x
sudo apt update

The script, if you’re wondering and don’t want to just run anything from the internet (a good idea) is as follows:

#!/bin/sh
# To add this repository please do:
 
if [ "$(whoami)" != "root" ]; then
    SUDO=sudo
fi
 
${SUDO} apt-get update
${SUDO} apt-get -y install lsb-release ca-certificates curl
${SUDO} curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
${SUDO} dpkg -i /tmp/debsuryorg-archive-keyring.deb
${SUDO} sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
${SUDO} apt-get update

If You’re Using Apache

Install Apache on Ubuntu or Debian, then install PHP as an Apache module. Replace 8.4 with your desired PHP version.

sudo apt install php8.4 libapache2-mod-php8.4

Once the packages are installed, restart Apache to load the PHP module.

sudo systemctl restart apache2

Configure Apache with PHP-FPM

Replace 8.4 with your desired PHP version.

sudo apt update
sudo apt install php8.4-fpm libapache2-mod-fcgid
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.4-fpm
systemctl restart apache2

If You’re using Nginx

Replace 8.4 with your desired PHP version.

sudo apt update
sudo apt install php8.4-fpm

Now edit your Nginx server block, and add the following lines so Nginx can process PHP files.

server {
 
    # . . . other code
 
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    }
}

Now restart your Nginx service.

sudo systemctl restart nginx

Installing PHP extensions

Replace 8.4 with your desired PHP version.

sudo apt install php8.4-[extname]