Skip to content

Instantly share code, notes, and snippets.

@efann
Last active November 7, 2022 20:54
Show Gist options
  • Save efann/12284d693bb88566afcc815e2be47c85 to your computer and use it in GitHub Desktop.
Save efann/12284d693bb88566afcc815e2be47c85 to your computer and use it in GitHub Desktop.
#!/bin/bash
# License: Eclipse Public License - v 2.0 (https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html)
# Updated on November 7, 2022
if [[ $EUID -ne 0 ]]; then
echo -e "\nThis script must be run as root. Exiting. . . .\n\n"
exit 1
fi
if [ -z "$1" ]; then
echo -e "\nOne must pass the PHP version to enable. Exiting. . . .\n\n"
exit 1
fi
if ! [[ "$1" =~ ^[0-9]+\.[0-9]+$ ]]; then
echo -e "\nOne must pass a valid number like 7.3 or 7.4. Exiting. . . .\n\n"
exit 1
fi
lcVersion="$1"
# Grab the first character and convert to an integer.
# By the way, bash works well with integers, not floats.
# And we just need to know if PHP 8 or above.
lnPHPVersion="$((${lcVersion:0:1} + 0))"
# Ensure that the PHP version is installed.
echo -e "==============================================="
echo -e "Installing (if not already installed) basic needed libraries. . . .\n"
apt install "php${lcVersion}"
apt install "php${lcVersion}-bcmath"
apt install "php${lcVersion}-cli"
apt install "php${lcVersion}-common"
apt install "php${lcVersion}-curl"
apt install "php${lcVersion}-gd"
if [ "${lnPHPVersion}" -lt 8 ]; then
apt install "php${lcVersion}-json"
else
# From https://github.com/manala/ansible-roles/issues/548
echo -e "\njson extension is now embedded in php starting with version 8.\n"
fi
apt install "php${lcVersion}-mbstring"
apt install "php${lcVersion}-mysql"
apt install "php${lcVersion}-opcache"
apt install "php${lcVersion}-readline"
apt install "php${lcVersion}-xml"
apt install "php${lcVersion}-zip"
systemctl restart apache2
apt list "php${lcVersion}*" --installed
echo -e "==============================================="
echo -e "Validating the existence of PHP v ${lcVersion}\n"
if [ ! -d "/etc/php/${lcVersion}" ]; then
echo -e "\nPHP v ${lcVersion} is not installed. Exiting. . . .\n"
exit 1
fi
lcPHPIniFile="/etc/php/${lcVersion}/apache2/php.ini"
if [ ! -f ${lcPHPIniFile} ]; then
echo -e "\nPHP v ${lcVersion} ini file (${lcPHPIniFile}) is missing. Exiting. . . .\n"
exit 1
fi
echo -e "==============================================="
echo -e "Disabling all PHP versions. . . .\n"
for lcDir in /etc/php/*; do
lcPHPVer=$(basename "$lcDir")
echo -e "Running a2dismod php$lcPHPVer\n"
a2dismod "php$lcPHPVer"
done
echo -e "==============================================="
echo -e "Disabling mpm_prefork, mpm_worker, mpm_event. . . .\n"
echo -e "Enabling PHP will re-enable the appropriate modules above. . . .\n"
a2dismod mpm_prefork
a2dismod mpm_worker
a2dismod mpm_event
echo -e "\nRestarting apache\n"
systemctl restart apache2
echo -e "==============================================="
echo -e "Enabling php${lcVersion}. . . .\n"
a2enmod "php${lcVersion}"
systemctl restart apache2
echo -e "==============================================="
echo -e "Running update-alternatives. . . .\n"
update-alternatives --set php "/usr/bin/php${lcVersion}"
update-alternatives --set phar "/usr/bin/phar${lcVersion}"
update-alternatives --set phar.phar "/usr/bin/phar.phar${lcVersion}"
update-alternatives --set phpize "/usr/bin/phpize${lcVersion}"
update-alternatives --set php-config "/usr/bin/php-config${lcVersion}"
echo -e "==============================================="
echo -e "Restarting apache\n"
systemctl restart apache2
echo -e "==============================================="
echo -e "Displaying PHP version info with ${lcPHPIniFile}. . . .\n\n"
php -v
echo -e "\n"
php -c "${lcPHPIniFile}" -i | grep 'memory_limit'
php -c "${lcPHPIniFile}" -i | grep 'max_input_vars'
echo -e "\n"
echo -e "==============================================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment