diff --git a/misc/tools.func b/misc/tools.func index fdde509..0fbde82 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -93,7 +93,7 @@ install_node_and_modules() { } function install_postgresql() { - local PG_VERSION="${PG_VERSION:-15}" + local PG_VERSION="${PG_VERSION:-16}" local CURRENT_PG_VERSION="" local DISTRO local NEED_PG_INSTALL=false @@ -169,6 +169,93 @@ function install_mariadb() { fi } +install_php() { + local PHP_VERSION="${PHP_VERSION:-8.4}" + local PHP_MODULE="${PHP_MODULE:-}" + local PHP_APACHE="${PHP_APACHE:-NO}" + local PHP_FPM="${PHP_FPM:-NO}" + local DEFAULT_MODULES="bcmath,cli,curl,gd,intl,mbstring,opcache,readline,xml,zip" + local COMBINED_MODULES + + local PHP_MEMORY_LIMIT="${PHP_MEMORY_LIMIT:-512M}" + local PHP_UPLOAD_MAX_FILESIZE="${PHP_UPLOAD_MAX_FILESIZE:-128M}" + local PHP_POST_MAX_SIZE="${PHP_POST_MAX_SIZE:-128M}" + local PHP_MAX_EXECUTION_TIME="${PHP_MAX_EXECUTION_TIME:-300}" + + # Merge default + user-defined modules + if [[ -n "$PHP_MODULE" ]]; then + COMBINED_MODULES="${DEFAULT_MODULES},${PHP_MODULE}" + else + COMBINED_MODULES="${DEFAULT_MODULES}" + fi + + # Deduplicate modules + COMBINED_MODULES=$(echo "$COMBINED_MODULES" | tr ',' '\n' | awk '!seen[$0]++' | paste -sd, -) + + local CURRENT_PHP + CURRENT_PHP=$(php -v 2>/dev/null | awk '/^PHP/{print $2}' | cut -d. -f1,2) + + if [[ "$CURRENT_PHP" != "$PHP_VERSION" ]]; then + $STD echo "PHP $CURRENT_PHP detected, migrating to PHP $PHP_VERSION" + if [[ ! -f /etc/apt/sources.list.d/php.list ]]; then + $STD curl -fsSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb + $STD dpkg -i /tmp/debsuryorg-archive-keyring.deb + echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" \ + >/etc/apt/sources.list.d/php.list + $STD apt-get update + fi + + $STD apt-get purge -y "php${CURRENT_PHP//./}"* || true + fi + + local MODULE_LIST="php${PHP_VERSION}" + IFS=',' read -ra MODULES <<<"$COMBINED_MODULES" + for mod in "${MODULES[@]}"; do + MODULE_LIST+=" php${PHP_VERSION}-${mod}" + done + + if [[ "$PHP_APACHE" == "YES" ]]; then + # Optionally disable old Apache PHP module + if [[ -f /etc/apache2/mods-enabled/php${CURRENT_PHP}.load ]]; then + $STD a2dismod php${CURRENT_PHP} || true + fi + fi + + if [[ "$PHP_FPM" == "YES" ]]; then + $STD systemctl stop php${CURRENT_PHP}-fpm || true + $STD systemctl disable php${CURRENT_PHP}-fpm || true + fi + + $STD apt-get install -y $MODULE_LIST + msg_ok "Installed PHP $PHP_VERSION with selected modules" + + if [[ "$PHP_APACHE" == "YES" ]]; then + $STD systemctl restart apache2 || true + fi + + if [[ "$PHP_FPM" == "YES" ]]; then + $STD systemctl enable php${PHP_VERSION}-fpm + $STD systemctl restart php${PHP_VERSION}-fpm + fi + + # Patch all relevant php.ini files + local PHP_INI_PATHS=() + PHP_INI_PATHS+=("/etc/php/${PHP_VERSION}/cli/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") + + for ini in "${PHP_INI_PATHS[@]}"; do + if [[ -f "$ini" ]]; then + msg_info "Patching $ini" + sed -i "s|^memory_limit = .*|memory_limit = ${PHP_MEMORY_LIMIT}|" "$ini" + sed -i "s|^upload_max_filesize = .*|upload_max_filesize = ${PHP_UPLOAD_MAX_FILESIZE}|" "$ini" + sed -i "s|^post_max_size = .*|post_max_size = ${PHP_POST_MAX_SIZE}|" "$ini" + sed -i "s|^max_execution_time = .*|max_execution_time = ${PHP_MAX_EXECUTION_TIME}|" "$ini" + msg_ok "Patched $ini" + fi + done +} + function install_mysql() { local MYSQL_VERSION="${MYSQL_VERSION:-8.0}" local CURRENT_VERSION=""