Background
Managing PHP versions is crucial for maintaining compatibility, security, and performance of web applications. Ubuntu often provides multiple PHP versions, but upgrading packages across versions can be tedious. This script simplifies the process by automating package upgrades, switching the active PHP version for Apache, and cleaning up outdated packages.
Prerequisites
- You must have administrative privileges (sudo access).
- Ensure your system is running Ubuntu 22.04 or 24.04.
- Make sure Apache is installed if you are using PHP with a web server.
Script
#!/bin/bash
# Get installed PHP versions
php_versions=$(update-alternatives --list php | awk -F'/' '{print $NF}' | sort -V)
echo "Installed PHP versions:"
echo "$php_versions"
# Ask user which PHP version to upgrade packages to
echo -n "Enter the PHP version to upgrade packages to: "
read target_version
# Get a list of installed PHP packages
php_packages=$(dpkg -l | awk '/^ii/ && /php/{print $2}')
echo "Checking packages that need to be upgraded to PHP $target_version..."
declare -A upgrade_list
for package in $php_packages; do
new_package=$(echo "$package" | sed "s/[0-9]\+\.[0-9]\+/$target_version/")
if apt-cache show "$new_package" &>/dev/null; then
upgrade_list[$package]=$new_package
echo "$package -> $new_package (Available)"
else
echo "$package -> $new_package (Not Found)"
fi
done
# Ask user if they want to proceed
echo -n "Do you want to upgrade these packages? (y/n): "
read proceed
if [[ "$proceed" != "y" ]]; then
echo "Upgrade canceled."
exit 1
fi
# Upgrade packages
for old_pkg in "${!upgrade_list[@]}"; do
new_pkg=${upgrade_list[$old_pkg]}
echo "Upgrading $old_pkg -> $new_pkg"
sudo apt install --only-upgrade -y "$new_pkg"
done
# Show the enabled PHP version for Apache
apache_php=$(readlink -f /etc/alternatives/php | grep -oP 'php[0-9]\.[0-9]+')
echo "Apache is currently using PHP version: $apache_php"
# Ask user if they want to change the PHP version for Apache
echo -n "Do you want to switch Apache to PHP $target_version? (y/n): "
read change_php
if [[ "$change_php" == "y" ]]; then
sudo a2dismod "$apache_php"
sudo a2enmod "php$target_version"
sudo systemctl restart apache2
echo "Apache has been switched to PHP $target_version and restarted."
fi
# List PHP packages that can be removed
old_php_packages=$(dpkg -l | awk -v ver="$target_version" '/^ii/ && /php/ && !index($2, ver){print $2}')
echo "The following PHP packages from old versions can be removed:"
echo "$old_php_packages"
# Ask user if they want to remove old PHP packages
echo -n "Do you want to remove these old PHP packages? (y/n): "
read remove_old
if [[ "$remove_old" == "y" ]]; then
sudo apt remove --purge -y $old_php_packages
sudo apt autoremove -y
echo "Old PHP packages removed."
fi
echo "PHP upgrade process completed."
Setup
To use the script, follow these steps:
- Copy the script below.
- Open a terminal and create a new file:
nano php_upgrade.sh
- Paste the script into the file.
- Save and exit (Press
CTRL + X
, thenY
, thenEnter
). - Make the script executable:
chmod +x php_upgrade.sh
- Run the script with:
sudo ./php_upgrade.sh
- Download the script:
wget -O php_upgrade.sh https://yourwebsite.com/php_upgrade.sh
- Make the script executable:
chmod +x php_upgrade.sh
- Run the script:
sudo ./php_upgrade.sh
Usage
- The script will detect installed PHP versions and display them.
- It will prompt you to choose the PHP version you want to upgrade packages to.
- A list of packages that need upgrading will be shown, including availability.
- Confirm if you want to proceed with the upgrade.
- If needed, you can switch the active PHP version for Apache.
- The script will then list outdated PHP packages and ask if you want to remove them.
- After confirmation, it will uninstall unnecessary packages and clean up dependencies.
Notes
- The script ensures that PHP packages are upgraded without breaking dependencies.
- If you run a PHP-based application, check compatibility with the new version before upgrading.
- Always back up your system before making major changes.