Created
May 8, 2025 14:56
-
-
Save a-bashtannik/2f2d23bba04e8d55aa95661a8484957a to your computer and use it in GitHub Desktop.
Update your existing PHP package setup collection to version PHP 8.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Colors for output | |
GREEN='\033[0;32m' | |
RED='\033[0;31m' | |
YELLOW='\033[0;33m' | |
NC='\033[0m' # No Color | |
echo -e "${YELLOW}PHP Packages Update Script to Version 8.4${NC}" | |
echo "----------------------------------------------------" | |
# Check for root privileges | |
if [ "$(id -u)" -ne 0 ]; then | |
echo -e "${RED}This script must be run with root privileges${NC}" | |
exit 1 | |
fi | |
# Check PHP 8.4 availability | |
if ! command -v php8.4 &> /dev/null && ! apt-cache show php8.4 &> /dev/null; then | |
echo -e "${RED}PHP 8.4 not found or not available in repositories.${NC}" | |
echo "Attempting to add repository..." | |
# Adding PPA repository for PHP (if necessary) | |
add-apt-repository -y ppa:ondrej/php | |
apt-get update | |
fi | |
# Get list of installed PHP packages (excluding PHP 8.4) | |
echo -e "${YELLOW}Getting list of installed PHP packages...${NC}" | |
INSTALLED_PACKAGES=$(dpkg -l | grep php | grep -v php8.4 | awk '{print $2}' | sort) | |
if [ -z "$INSTALLED_PACKAGES" ]; then | |
echo -e "${YELLOW}No installed PHP packages found.${NC}" | |
exit 0 | |
fi | |
# Save the package list to a file | |
echo "$INSTALLED_PACKAGES" > /tmp/php_packages.txt | |
echo -e "${GREEN}Found $(wc -l < /tmp/php_packages.txt) installed PHP packages.${NC}" | |
# Prepare list of packages for PHP 8.4 | |
echo -e "${YELLOW}Preparing package list for PHP 8.4...${NC}" | |
PHP84_PACKAGES="" | |
while IFS= read -r package; do | |
# Skip empty lines | |
if [ -z "$package" ]; then | |
continue | |
fi | |
# Convert package names for PHP 8.4 | |
NEW_PACKAGE=$(echo "$package" | sed -E 's/php[0-9]\.[0-9]/php8.4/g') | |
# Check package availability | |
if apt-cache show "$NEW_PACKAGE" &> /dev/null; then | |
PHP84_PACKAGES="$PHP84_PACKAGES $NEW_PACKAGE" | |
echo -e "- ${GREEN}$package${NC} -> ${GREEN}$NEW_PACKAGE${NC}" | |
else | |
echo -e "- ${GREEN}$package${NC} -> ${RED}$NEW_PACKAGE (not available)${NC}" | |
fi | |
done < /tmp/php_packages.txt | |
# Remove old packages | |
echo -e "\n${YELLOW}Removing old PHP packages...${NC}" | |
apt-get remove -y $INSTALLED_PACKAGES | |
# Install new packages for PHP 8.4 | |
if [ -n "$PHP84_PACKAGES" ]; then | |
echo -e "\n${YELLOW}Installing packages for PHP 8.4...${NC}" | |
apt-get install -y $PHP84_PACKAGES | |
echo -e "\n${GREEN}Installation completed. Checking installed PHP 8.4 packages:${NC}" | |
dpkg -l | grep php8.4 | |
else | |
echo -e "\n${RED}No available packages for PHP 8.4 installation.${NC}" | |
fi | |
# Clean up the system | |
echo -e "\n${YELLOW}Cleaning up the system...${NC}" | |
apt-get autoremove -y | |
apt-get clean | |
echo -e "\n${GREEN}PHP packages update to version 8.4 completed!${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment