Skip to content

Commit 1f6a141

Browse files
committed
Refactor PHP setup for stricter version enforcement
Reworks the PHP installation logic to always remove conflicting PHP versions before pinning and repository setup, ensuring only the desired version is present. Simplifies module installation by letting apt-get handle missing modules and improves error handling for individual package installations. Removes pre-checks and warnings for unavailable modules, streamlining the process and reducing complexity.
1 parent 27bd553 commit 1f6a141

File tree

1 file changed

+45
-98
lines changed

1 file changed

+45
-98
lines changed

misc/tools.func

Lines changed: 45 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,51 +3628,17 @@ function setup_php() {
36283628
local CURRENT_PHP=""
36293629
CURRENT_PHP=$(is_tool_installed "php" 2>/dev/null) || true
36303630

3631-
# Scenario 1: Already at target version - just update packages
3632-
if [[ -n "$CURRENT_PHP" && "$CURRENT_PHP" == "$PHP_VERSION" ]]; then
3633-
msg_info "Update PHP $PHP_VERSION"
3634-
3635-
# Ensure pinning exists even for updates (prevent unwanted version changes)
3636-
mkdir -p /etc/apt/preferences.d
3637-
cat <<EOF >/etc/apt/preferences.d/php-pin
3638-
Package: php${PHP_VERSION}*
3639-
Pin: version ${PHP_VERSION}.*
3640-
Pin-Priority: 1001
3641-
3642-
Package: php[0-9].*
3643-
Pin: release o=packages.sury.org-php
3644-
Pin-Priority: -1
3645-
EOF
3646-
3647-
# Ensure Sury repo is available
3648-
if [[ ! -f /etc/apt/sources.list.d/php.sources ]]; then
3649-
manage_tool_repository "php" "$PHP_VERSION" "" "https://packages.sury.org/debsuryorg-archive-keyring.deb" || {
3650-
msg_error "Failed to setup PHP repository"
3651-
return 1
3652-
}
3653-
fi
3654-
3655-
ensure_apt_working || return 1
3656-
$STD apt-get update
3657-
3658-
# Perform upgrade with retry logic (non-fatal if fails)
3659-
upgrade_packages_with_retry "php${PHP_VERSION}" || true
3660-
3661-
cache_installed_version "php" "$PHP_VERSION"
3662-
msg_ok "Update PHP $PHP_VERSION"
3663-
else
3664-
# Scenario 2: Different version installed - clean upgrade
3665-
if [[ -n "$CURRENT_PHP" && "$CURRENT_PHP" != "$PHP_VERSION" ]]; then
3666-
msg_info "Upgrade PHP from $CURRENT_PHP to $PHP_VERSION"
3667-
# Stop and disable ALL PHP-FPM versions
3668-
stop_all_services "php.*-fpm"
3669-
else
3670-
msg_info "Setup PHP $PHP_VERSION"
3671-
fi
3631+
# CRITICAL: If wrong version is installed, remove it FIRST before any pinning
3632+
if [[ -n "$CURRENT_PHP" && "$CURRENT_PHP" != "$PHP_VERSION" ]]; then
3633+
msg_info "Removing conflicting PHP ${CURRENT_PHP} (need ${PHP_VERSION})"
3634+
stop_all_services "php.*-fpm"
3635+
$STD apt-get purge -y "php*" 2>/dev/null || true
3636+
$STD apt-get autoremove -y 2>/dev/null || true
3637+
fi
36723638

3673-
# Create APT pinning BEFORE any repo changes to ensure correct version is selected
3674-
mkdir -p /etc/apt/preferences.d
3675-
cat <<EOF >/etc/apt/preferences.d/php-pin
3639+
# NOW create pinning for the desired version
3640+
mkdir -p /etc/apt/preferences.d
3641+
cat <<EOF >/etc/apt/preferences.d/php-pin
36763642
Package: php${PHP_VERSION}*
36773643
Pin: version ${PHP_VERSION}.*
36783644
Pin-Priority: 1001
@@ -3682,28 +3648,19 @@ Pin: release o=packages.sury.org-php
36823648
Pin-Priority: -1
36833649
EOF
36843650

3685-
# Prepare repository (cleanup + validation)
3686-
prepare_repository_setup "php" "deb.sury.org-php" || {
3687-
msg_error "Failed to prepare PHP repository"
3688-
return 1
3689-
}
3690-
3691-
# Remove ALL conflicting PHP versions (critical for version enforcement)
3692-
if [[ -n "$CURRENT_PHP" && "$CURRENT_PHP" != "$PHP_VERSION" ]]; then
3693-
msg_info "Removing PHP ${CURRENT_PHP}"
3694-
$STD apt-get purge -y "php${CURRENT_PHP}*" "libapache2-mod-php${CURRENT_PHP}*" 2>/dev/null || true
3695-
$STD apt-get autoremove -y 2>/dev/null || true
3696-
fi
3651+
# Setup repository
3652+
prepare_repository_setup "php" "deb.sury.org-php" || {
3653+
msg_error "Failed to prepare PHP repository"
3654+
return 1
3655+
}
36973656

3698-
# Setup Sury repository
3699-
manage_tool_repository "php" "$PHP_VERSION" "" "https://packages.sury.org/debsuryorg-archive-keyring.deb" || {
3700-
msg_error "Failed to setup PHP repository"
3701-
return 1
3702-
}
3657+
manage_tool_repository "php" "$PHP_VERSION" "" "https://packages.sury.org/debsuryorg-archive-keyring.deb" || {
3658+
msg_error "Failed to setup PHP repository"
3659+
return 1
3660+
}
37033661

3704-
ensure_apt_working || return 1
3705-
$STD apt-get update
3706-
fi
3662+
ensure_apt_working || return 1
3663+
$STD apt-get update
37073664

37083665
# Get available PHP version from repository
37093666
local AVAILABLE_PHP_VERSION=""
@@ -3714,39 +3671,16 @@ EOF
37143671
return 1
37153672
fi
37163673

3717-
# Build module list with version constraints
3674+
# Build module list - don't pre-check, let apt-get handle it
37183675
local MODULE_LIST="php${PHP_VERSION}=${AVAILABLE_PHP_VERSION}-*"
3719-
local FAILED_MODULES=()
3720-
local INSTALLED_MODULES=()
37213676

37223677
IFS=',' read -ra MODULES <<<"$COMBINED_MODULES"
37233678
for mod in "${MODULES[@]}"; do
3724-
local pkg_name="php${PHP_VERSION}-${mod}"
3725-
if apt-cache search "^${pkg_name}\$" 2>/dev/null | grep -q "^${pkg_name}"; then
3726-
MODULE_LIST+=" ${pkg_name}=${AVAILABLE_PHP_VERSION}-*"
3727-
INSTALLED_MODULES+=("${pkg_name}")
3728-
else
3729-
FAILED_MODULES+=("${pkg_name}")
3730-
fi
3679+
MODULE_LIST+=" php${PHP_VERSION}-${mod}=${AVAILABLE_PHP_VERSION}-*"
37313680
done
37323681

37333682
if [[ "$PHP_FPM" == "YES" ]]; then
3734-
local fpm_pkg="php${PHP_VERSION}-fpm"
3735-
if apt-cache search "^${fpm_pkg}\$" 2>/dev/null | grep -q "^${fpm_pkg}"; then
3736-
MODULE_LIST+=" ${fpm_pkg}=${AVAILABLE_PHP_VERSION}-*"
3737-
INSTALLED_MODULES+=("${fpm_pkg}")
3738-
else
3739-
FAILED_MODULES+=("${fpm_pkg}")
3740-
fi
3741-
fi
3742-
3743-
# Only warn if there are genuinely missing modules
3744-
if [[ ${#FAILED_MODULES[@]} -gt 0 ]]; then
3745-
msg_warn "Modules not available for PHP ${PHP_VERSION}: ${FAILED_MODULES[*]}"
3746-
fi
3747-
3748-
if [[ ${#INSTALLED_MODULES[@]} -gt 0 ]]; then
3749-
msg_info "Will install modules: ${INSTALLED_MODULES[*]}"
3683+
MODULE_LIST+=" php${PHP_VERSION}-fpm=${AVAILABLE_PHP_VERSION}-*"
37503684
fi
37513685

37523686
# install apache2 with PHP support if requested
@@ -3766,23 +3700,36 @@ EOF
37663700
# Install PHP packages with explicit version constraints
37673701
msg_info "Installing PHP ${PHP_VERSION} packages (version ${AVAILABLE_PHP_VERSION})"
37683702
if ! install_packages_with_retry $MODULE_LIST; then
3769-
msg_warn "Failed to install PHP packages with version constraints, attempting without version pins"
3703+
msg_warn "Failed to install PHP packages with version constraints, attempting individual installation"
3704+
3705+
# Install main package first (critical)
37703706
install_packages_with_retry "php${PHP_VERSION}" || {
37713707
msg_error "Failed to install php${PHP_VERSION}"
37723708
return 1
37733709
}
37743710

3775-
# Try to install modules individually without version constraint
3776-
for pkg in "${MODULES[@]}"; do
3777-
install_packages_with_retry "php${PHP_VERSION}-${pkg}" || {
3778-
msg_warn "Could not install php${PHP_VERSION}-${pkg}"
3711+
# Try to install Apache module individually if requested
3712+
if [[ "$PHP_APACHE" == "YES" ]]; then
3713+
install_packages_with_retry "libapache2-mod-php${PHP_VERSION}" || {
3714+
msg_warn "Could not install libapache2-mod-php${PHP_VERSION}"
37793715
}
3716+
fi
3717+
3718+
# Try to install modules individually - skip those that don't exist
3719+
for pkg in "${MODULES[@]}"; do
3720+
if apt-cache search "^php${PHP_VERSION}-${pkg}\$" 2>/dev/null | grep -q "^php${PHP_VERSION}-${pkg}"; then
3721+
install_packages_with_retry "php${PHP_VERSION}-${pkg}" || {
3722+
msg_warn "Could not install php${PHP_VERSION}-${pkg}"
3723+
}
3724+
fi
37803725
done
37813726

37823727
if [[ "$PHP_FPM" == "YES" ]]; then
3783-
install_packages_with_retry "php${PHP_VERSION}-fpm" || {
3784-
msg_warn "Could not install php${PHP_VERSION}-fpm"
3785-
}
3728+
if apt-cache search "^php${PHP_VERSION}-fpm\$" 2>/dev/null | grep -q "^php${PHP_VERSION}-fpm"; then
3729+
install_packages_with_retry "php${PHP_VERSION}-fpm" || {
3730+
msg_warn "Could not install php${PHP_VERSION}-fpm"
3731+
}
3732+
fi
37863733
fi
37873734
fi
37883735
cache_installed_version "php" "$PHP_VERSION"

0 commit comments

Comments
 (0)