Skip to content

Commit 8ad68e7

Browse files
committed
core: improve setup_php robustness and version validation
Enhances the setup_php function to enforce explicit PHP version pinning during installation, adds checks for available versions and missing modules, and improves error handling. The script now verifies the installed PHP version matches the requested version and provides fallback installation logic if version-constrained installs fail.
1 parent 0881867 commit 8ad68e7

File tree

1 file changed

+86
-13
lines changed

1 file changed

+86
-13
lines changed

misc/tools.func

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,35 +3671,91 @@ function setup_php() {
36713671
}
36723672

36733673
ensure_apt_working || return 1
3674+
3675+
# Force version preference during installation
3676+
mkdir -p /etc/apt/preferences.d
3677+
cat <<EOF >/etc/apt/preferences.d/php-pin
3678+
Package: php${PHP_VERSION}*
3679+
Pin: version ${PHP_VERSION}.*
3680+
Pin-Priority: 1001
3681+
3682+
Package: php8.*
3683+
Pin: release o=packages.sury.org-php
3684+
Pin-Priority: -1
3685+
EOF
3686+
$STD apt-get update
36743687
fi
36753688

3676-
# Build module list
3677-
local MODULE_LIST="php${PHP_VERSION}"
3689+
# Get available PHP version from repository
3690+
local AVAILABLE_PHP_VERSION=""
3691+
AVAILABLE_PHP_VERSION=$(apt-cache show "php${PHP_VERSION}" 2>/dev/null | grep -m1 "^Version:" | awk '{print $2}' | cut -d- -f1) || true
3692+
3693+
if [[ -z "$AVAILABLE_PHP_VERSION" ]]; then
3694+
msg_error "PHP ${PHP_VERSION} not found in configured repositories"
3695+
return 1
3696+
fi
3697+
3698+
# Build module list with version constraints
3699+
local MODULE_LIST="php${PHP_VERSION}=${AVAILABLE_PHP_VERSION}-*"
3700+
local FAILED_MODULES=()
3701+
36783702
IFS=',' read -ra MODULES <<<"$COMBINED_MODULES"
36793703
for mod in "${MODULES[@]}"; do
3680-
if apt-cache show "php${PHP_VERSION}-${mod}" >/dev/null 2>&1; then
3681-
MODULE_LIST+=" php${PHP_VERSION}-${mod}"
3704+
if apt-cache show "php${PHP_VERSION}-${mod}" 2>/dev/null | grep -q "^Package:"; then
3705+
MODULE_LIST+=" php${PHP_VERSION}-${mod}=${AVAILABLE_PHP_VERSION}-*"
3706+
else
3707+
FAILED_MODULES+=("php${PHP_VERSION}-${mod}")
36823708
fi
36833709
done
3710+
36843711
if [[ "$PHP_FPM" == "YES" ]]; then
3685-
MODULE_LIST+=" php${PHP_VERSION}-fpm"
3712+
if apt-cache show "php${PHP_VERSION}-fpm" 2>/dev/null | grep -q "^Package:"; then
3713+
MODULE_LIST+=" php${PHP_VERSION}-fpm=${AVAILABLE_PHP_VERSION}-*"
3714+
else
3715+
FAILED_MODULES+=("php${PHP_VERSION}-fpm")
3716+
fi
3717+
fi
3718+
3719+
if [[ ${#FAILED_MODULES[@]} -gt 0 ]]; then
3720+
msg_warn "Some modules unavailable for PHP ${PHP_VERSION}: ${FAILED_MODULES[*]}"
36863721
fi
36873722

36883723
# install apache2 with PHP support if requested
36893724
if [[ "$PHP_APACHE" == "YES" ]]; then
36903725
if ! dpkg -l 2>/dev/null | grep -q "libapache2-mod-php${PHP_VERSION}"; then
3691-
install_packages_with_retry "apache2" "libapache2-mod-php${PHP_VERSION}" || {
3692-
msg_error "Failed to install Apache with PHP module"
3726+
msg_info "Installing Apache with PHP ${PHP_VERSION} module"
3727+
install_packages_with_retry "apache2" || {
3728+
msg_error "Failed to install Apache"
36933729
return 1
36943730
}
3731+
install_packages_with_retry "libapache2-mod-php${PHP_VERSION}=${AVAILABLE_PHP_VERSION}-*" || {
3732+
msg_warn "Failed to install libapache2-mod-php${PHP_VERSION}, continuing without Apache module"
3733+
}
36953734
fi
36963735
fi
36973736

3698-
# Install PHP packages with retry logic
3699-
install_packages_with_retry $MODULE_LIST || {
3700-
msg_error "Failed to install PHP packages"
3701-
return 1
3702-
}
3737+
# Install PHP packages with explicit version constraints
3738+
msg_info "Installing PHP ${PHP_VERSION} packages (version ${AVAILABLE_PHP_VERSION})"
3739+
if ! install_packages_with_retry $MODULE_LIST; then
3740+
msg_warn "Failed to install PHP packages with version constraints, attempting without version pins"
3741+
install_packages_with_retry "php${PHP_VERSION}" || {
3742+
msg_error "Failed to install php${PHP_VERSION}"
3743+
return 1
3744+
}
3745+
3746+
# Try to install modules individually without version constraint
3747+
for pkg in "${MODULES[@]}"; do
3748+
install_packages_with_retry "php${PHP_VERSION}-${pkg}" || {
3749+
msg_warn "Could not install php${PHP_VERSION}-${pkg}"
3750+
}
3751+
done
3752+
3753+
if [[ "$PHP_FPM" == "YES" ]]; then
3754+
install_packages_with_retry "php${PHP_VERSION}-fpm" || {
3755+
msg_warn "Could not install php${PHP_VERSION}-fpm"
3756+
}
3757+
fi
3758+
fi
37033759
cache_installed_version "php" "$PHP_VERSION"
37043760

37053761
# Patch all relevant php.ini files
@@ -3735,7 +3791,24 @@ function setup_php() {
37353791
fi
37363792
fi
37373793

3738-
msg_ok "Setup PHP $PHP_VERSION"
3794+
# Verify PHP installation - critical check
3795+
if ! command -v php >/dev/null 2>&1; then
3796+
msg_error "PHP installation verification failed - php command not found"
3797+
return 1
3798+
fi
3799+
3800+
local INSTALLED_VERSION=$(php -v 2>/dev/null | awk '/^PHP/{print $2}' | cut -d. -f1,2)
3801+
3802+
# Critical: if major.minor doesn't match, fail and cleanup
3803+
if [[ "$INSTALLED_VERSION" != "$PHP_VERSION" ]]; then
3804+
msg_error "PHP version mismatch: requested ${PHP_VERSION} but got ${INSTALLED_VERSION}"
3805+
msg_error "This indicates a critical package installation issue"
3806+
# Don't cache wrong version
3807+
return 1
3808+
fi
3809+
3810+
cache_installed_version "php" "$INSTALLED_VERSION"
3811+
msg_ok "Setup PHP ${INSTALLED_VERSION}"
37393812
}
37403813

37413814
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)