Refactor: setup_php

This commit is contained in:
CanbiZ
2025-11-21 22:10:57 +01:00
committed by GitHub
parent 06d8e5354a
commit aa26bf81e1

View File

@@ -3577,62 +3577,39 @@ function setup_nodejs() {
fi fi
fi fi
} }
# ------------------------------------------------------------------------------
# Installs PHP with selected modules and configures Apache/FPM support.
#
# Description:
# - Adds Sury PHP repo if needed
# - Installs default and user-defined modules
# - Patches php.ini for CLI, Apache, and FPM as needed
#
# Variables:
# PHP_VERSION - PHP version to install (default: 8.4)
# PHP_MODULE - Additional comma-separated modules
# PHP_APACHE - Set YES to enable PHP with Apache
# PHP_FPM - Set YES to enable PHP-FPM
# PHP_MEMORY_LIMIT - (default: 512M)
# PHP_UPLOAD_MAX_FILESIZE - (default: 128M)
# PHP_POST_MAX_SIZE - (default: 128M)
# PHP_MAX_EXECUTION_TIME - (default: 300)
# ------------------------------------------------------------------------------
function setup_php() { function setup_php() {
local PHP_VERSION="${PHP_VERSION:-8.4}" local PHP_VERSION="${PHP_VERSION:-8.4}"
local PHP_MODULE="${PHP_MODULE:-}" local PHP_MODULE="${PHP_MODULE:-}"
local PHP_APACHE="${PHP_APACHE:-NO}" local PHP_APACHE="${PHP_APACHE:-NO}"
local PHP_FPM="${PHP_FPM:-NO}" local PHP_FPM="${PHP_FPM:-NO}"
local DISTRO_ID DISTRO_CODENAME
DISTRO_ID=$(awk -F= '/^ID=/{print $2}' /etc/os-release | tr -d '"')
DISTRO_CODENAME=$(awk -F= '/^VERSION_CODENAME=/{print $2}' /etc/os-release)
local DEFAULT_MODULES="bcmath,cli,curl,gd,intl,mbstring,opcache,readline,xml,zip" local DEFAULT_MODULES="bcmath,cli,curl,gd,intl,mbstring,opcache,readline,xml,zip"
local COMBINED_MODULES local COMBINED_MODULES
local PHP_MEMORY_LIMIT="${PHP_MEMORY_LIMIT:-512M}" local PHP_MEMORY_LIMIT="${PHP_MEMORY_LIMIT:-512M}"
local PHP_UPLOAD_MAX_FILESIZE="${PHP_UPLOAD_MAX_FILESIZE:-128M}" local PHP_UPLOAD_MAX_FILESIZE="${PHP_UPLOAD_MAX_FILESIZE:-128M}"
local PHP_POST_MAX_SIZE="${PHP_POST_MAX_SIZE:-128M}" local PHP_POST_MAX_SIZE="${PHP_POST_MAX_SIZE:-128M}"
local PHP_MAX_EXECUTION_TIME="${PHP_MAX_EXECUTION_TIME:-300}" local PHP_MAX_EXECUTION_TIME="${PHP_MAX_EXECUTION_TIME:-300}"
# Merge default + user-defined modules
if [[ -n "$PHP_MODULE" ]]; then if [[ -n "$PHP_MODULE" ]]; then
COMBINED_MODULES="${DEFAULT_MODULES},${PHP_MODULE}" COMBINED_MODULES="${DEFAULT_MODULES},${PHP_MODULE}"
else else
COMBINED_MODULES="${DEFAULT_MODULES}" COMBINED_MODULES="${DEFAULT_MODULES}"
fi fi
# Deduplicate
COMBINED_MODULES=$(echo "$COMBINED_MODULES" | tr ',' '\n' | awk '!seen[$0]++' | paste -sd, -) COMBINED_MODULES=$(echo "$COMBINED_MODULES" | tr ',' '\n' | awk '!seen[$0]++' | paste -sd, -)
# Get current PHP-CLI version # Ensure PHP version exists
local CURRENT_PHP="" if ! apt-cache show "php${PHP_VERSION}" >/dev/null 2>&1; then
CURRENT_PHP=$(is_tool_installed "php" 2>/dev/null) || true msg_error "PHP ${PHP_VERSION} is not available in repositories"
return 1
fi
# Scenario 1: Already at target version - just update packages # Remove all existing PHP versions
if [[ -n "$CURRENT_PHP" && "$CURRENT_PHP" == "$PHP_VERSION" ]]; then msg_info "Removing existing PHP versions"
msg_info "Update PHP $PHP_VERSION" apt purge -y 'php[0-9]*' >/dev/null 2>&1 || true
msg_ok "Removed existing PHP"
# Ensure Sury repo is available # Ensure Sury repo
if [[ ! -f /etc/apt/sources.list.d/php.sources ]]; then if [[ ! -f /etc/apt/sources.list.d/php.sources ]]; then
manage_tool_repository "php" "$PHP_VERSION" "" "https://packages.sury.org/debsuryorg-archive-keyring.deb" || { manage_tool_repository "php" "$PHP_VERSION" "" "https://packages.sury.org/debsuryorg-archive-keyring.deb" || {
msg_error "Failed to setup PHP repository" msg_error "Failed to setup PHP repository"
@@ -3642,92 +3619,67 @@ function setup_php() {
ensure_apt_working || return 1 ensure_apt_working || return 1
# Perform upgrade with retry logic (non-fatal if fails) # Install base PHP
upgrade_packages_with_retry "php${PHP_VERSION}" || true msg_info "Installing PHP ${PHP_VERSION}"
install_packages_with_retry "php${PHP_VERSION}" || {
cache_installed_version "php" "$PHP_VERSION" msg_error "Failed to install PHP"
msg_ok "Update PHP $PHP_VERSION"
else
# Scenario 2: Different version installed - clean upgrade
if [[ -n "$CURRENT_PHP" && "$CURRENT_PHP" != "$PHP_VERSION" ]]; then
msg_info "Upgrade PHP from $CURRENT_PHP to $PHP_VERSION"
# Stop and disable ALL PHP-FPM versions
stop_all_services "php.*-fpm"
remove_old_tool_version "php"
else
msg_info "Setup PHP $PHP_VERSION"
fi
# Prepare repository (cleanup + validation)
prepare_repository_setup "php" "deb.sury.org-php" || {
msg_error "Failed to prepare PHP repository"
return 1 return 1
} }
# Setup Sury repository # Install modules
manage_tool_repository "php" "$PHP_VERSION" "" "https://packages.sury.org/debsuryorg-archive-keyring.deb" || { IFS=',' read -ra MODULES <<<"${COMBINED_MODULES}"
msg_error "Failed to setup PHP repository"
return 1
}
ensure_apt_working || return 1
fi
# Build module list
local MODULE_LIST="php${PHP_VERSION}"
IFS=',' read -ra MODULES <<<"$COMBINED_MODULES"
for mod in "${MODULES[@]}"; do for mod in "${MODULES[@]}"; do
if apt-cache show "php${PHP_VERSION}-${mod}" >/dev/null 2>&1; then if apt-cache show "php${PHP_VERSION}-${mod}" >/dev/null 2>&1; then
MODULE_LIST+=" php${PHP_VERSION}-${mod}" install_packages_with_retry "php${PHP_VERSION}-${mod}"
else
msg_error "Module not found: php${PHP_VERSION}-${mod}"
fi fi
done done
if [[ "$PHP_FPM" == "YES" ]]; then
MODULE_LIST+=" php${PHP_VERSION}-fpm"
fi
# install apache2 with PHP support if requested # Apache support
if [[ "$PHP_APACHE" == "YES" ]]; then if [[ "$PHP_APACHE" == "YES" ]]; then
if ! dpkg -l 2>/dev/null | grep -q "libapache2-mod-php${PHP_VERSION}"; then
install_packages_with_retry "apache2" "libapache2-mod-php${PHP_VERSION}" || { install_packages_with_retry "apache2" "libapache2-mod-php${PHP_VERSION}" || {
msg_error "Failed to install Apache with PHP module" msg_error "Failed to install Apache PHP module"
return 1 return 1
} }
fi fi
fi
# Install PHP packages with retry logic # PHP-FPM support
install_packages_with_retry $MODULE_LIST || { if [[ "$PHP_FPM" == "YES" ]]; then
msg_error "Failed to install PHP packages" install_packages_with_retry "php${PHP_VERSION}-fpm" || {
msg_error "Failed to install PHP-FPM"
return 1 return 1
} }
cache_installed_version "php" "$PHP_VERSION" fi
# Patch all relevant php.ini files # Activate CLI PHP version
if [[ -x "/usr/bin/php${PHP_VERSION}" ]]; then
update-alternatives --set php "/usr/bin/php${PHP_VERSION}" >/dev/null 2>&1 || true
fi
# Patch php.ini
local PHP_INI_PATHS=("/etc/php/${PHP_VERSION}/cli/php.ini") local PHP_INI_PATHS=("/etc/php/${PHP_VERSION}/cli/php.ini")
[[ "$PHP_FPM" == "YES" ]] && PHP_INI_PATHS+=("/etc/php/${PHP_VERSION}/fpm/php.ini") [[ "$PHP_FPM" == "YES" ]] && PHP_INI_PATHS+=("/etc/php/${PHP_VERSION}/fpm/php.ini")
[[ "$PHP_APACHE" == "YES" ]] && PHP_INI_PATHS+=("/etc/php/${PHP_VERSION}/apache2/php.ini") [[ "$PHP_APACHE" == "YES" ]] && PHP_INI_PATHS+=("/etc/php/${PHP_VERSION}/apache2/php.ini")
for ini in "${PHP_INI_PATHS[@]}"; do for ini in "${PHP_INI_PATHS[@]}"; do
if [[ -f "$ini" ]]; then [[ -f "$ini" ]] || continue
$STD sed -i "s|^memory_limit = .*|memory_limit = ${PHP_MEMORY_LIMIT}|" "$ini" $STD sed -i "s|^memory_limit = .*|memory_limit = ${PHP_MEMORY_LIMIT}|" "$ini"
$STD sed -i "s|^upload_max_filesize = .*|upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}|" "$ini" $STD sed -i "s|^upload_max_filesize = .*|upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}|" "$ini"
$STD sed -i "s|^post_max_size = .*|post_max_size = ${PHP_POST_MAX_SIZE}|" "$ini" $STD sed -i "s|^post_max_size = .*|post_max_size = ${PHP_POST_MAX_SIZE}|" "$ini"
$STD sed -i "s|^max_execution_time = .*|max_execution_time = ${PHP_MAX_EXECUTION_TIME}|" "$ini" $STD sed -i "s|^max_execution_time = .*|max_execution_time = ${PHP_MAX_EXECUTION_TIME}|" "$ini"
fi
done done
# Patch Apache configuration if needed # Apache restart
if [[ "$PHP_APACHE" == "YES" ]]; then if [[ "$PHP_APACHE" == "YES" ]]; then
for mod in $(ls /etc/apache2/mods-enabled/ 2>/dev/null | grep -E '^php[0-9]\.[0-9]\.conf$' | sed 's/\.conf//'); do for mod in $(ls /etc/apache2/mods-enabled/ 2>/dev/null | grep -E '^php[0-9]\.[0-9]\.conf$' | sed 's/\.conf//'); do
if [[ "$mod" != "php${PHP_VERSION}" ]]; then [[ "$mod" == "php${PHP_VERSION}" ]] || $STD a2dismod "$mod" || true
$STD a2dismod "$mod" || true
fi
done done
$STD a2enmod mpm_prefork
$STD a2enmod "php${PHP_VERSION}" $STD a2enmod "php${PHP_VERSION}"
safe_service_restart apache2 || true safe_service_restart apache2 || true
fi fi
# Enable and restart PHP-FPM if requested # FPM restart
if [[ "$PHP_FPM" == "YES" ]]; then if [[ "$PHP_FPM" == "YES" ]]; then
if systemctl list-unit-files | grep -q "php${PHP_VERSION}-fpm.service"; then if systemctl list-unit-files | grep -q "php${PHP_VERSION}-fpm.service"; then
$STD systemctl enable php${PHP_VERSION}-fpm $STD systemctl enable php${PHP_VERSION}-fpm
@@ -3735,7 +3687,7 @@ function setup_php() {
fi fi
fi fi
msg_ok "Setup PHP $PHP_VERSION" msg_ok "Setup PHP ${PHP_VERSION}"
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------