Article posted Sat Feb 25 2023

Scripts: Updating PHP 7.4 to 8.1

This script first displays a warning message to the user about possible issues with updating PHP from version 7.4 to 8.1. It then asks the user if they want to continue with the update. If the user chooses to continue, the script adds the ondrej/php repository, updates the package lists, and installs PHP 8.1. Finally, the script prompts the user to check their PHP configuration files for any changes that may need to be made for compatibility with PHP 8.1.

This is NOT hands free updating! These scripts are only examples of simplifying the initial steps of updating from 7.4 to 8.1, and are to be used as such. PLEASE only use this script as-is if you know what you are doing.

#!/bin/bash

# Check if the script is being run as root
if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

# Display a warning message about possible issues
echo "WARNING: Updating PHP from version 7.4 to 8.1 may cause compatibility issues with your existing PHP code."
echo "Please make sure to test your code thoroughly after the update."

# Ask the user if they want to continue with the update
read -p "Do you want to continue with the update? (y/n) " choice
case "$choice" in
  y|Y )
    echo "Starting PHP update..."
    ;;
  n|N )
    echo "Update cancelled."
    exit;;
  * )
    echo "Invalid choice. Update cancelled."
    exit;;
esac

# Add the ondrej/php repository
echo "Adding ondrej/php repository..."
add-apt-repository ppa:ondrej/php -y

# Update package lists
echo "Updating package lists..."
apt-get update

# Install PHP 8.1
echo "Installing PHP 8.1..."
apt-get install php8.1 php8.1-common php8.1-cli php8.1-fpm -y

# Prompt the user to check if any changes need to be made to their PHP configuration files
read -p "Do you want to check your PHP configuration files for any changes? (y/n) " choice
case "$choice" in
  y|Y )
    echo "Checking PHP configuration files..."
    diff /etc/php/7.4/fpm/php.ini /etc/php/8.1/fpm/php.ini
    diff /etc/php/7.4/cli/php.ini /etc/php/8.1/cli/php.ini
    ;;
  n|N )
    echo "Skipping PHP configuration file check."
    ;;
  * )
    echo "Invalid choice. Skipping PHP configuration file check."
    ;;
esac

echo "PHP update complete."

Switching between versions

To switch to PHP8.1 including the CLI, the following commands can be run from shell;

sudo a2dismod php7.4 # Disable PHP 7.4
sudo a2enmod php8.1 # Enable PHP 8.1
sudo update-alternatives --set php /usr/bin/php8.1 # Update CLI with PHP 8.1 version

And to switch back to PHP 7.4;

sudo a2dismod php8.1 # Disable PHP 8.1
sudo a2enmod php7.4 # Enable PHP 7.4
sudo update-alternatives --set php /usr/bin/php7.4 # Update CLI with PHP 7.4 version

Installing PHP mods

To add any required mods, such as those needed for Magento 2.4.5+/PHP 8.1+, the following example script could be used to add all PHP required mods required by the ecommerce framework Magento 2.4.5 on PHP 8.1. Simply alter "extensions=()" to include what you require. It is also a good example of a loop code written in bash.

#!/bin/bash

# Check if the script is being run as root
if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

# List of required PHP extensions and modules for Magento 2.4.5
extensions=(bc-math bcmath ctype curl dom gd intl mbstring openssl pcntl pdo_mysql simplexml soap spl xsl zip)

# Loop through the list of extensions and install each one
for extension in "${extensions[@]}"
do
    if [ "$(dpkg -s php$extension | grep Status)" != "Status: install ok installed" ]; then
        echo "Installing PHP extension: $extension"
        apt-get install php$extension -y
    else
        echo "PHP extension $extension is already installed."
    fi
done

echo "PHP extensions installation complete."