2019-12-20 18:29:05 +05:30
|
|
|
# Function to log start of a operation
|
2019-10-17 01:41:13 +05:30
|
|
|
step_log() {
|
|
|
|
message=$1
|
|
|
|
printf "\n\033[90;1m==> \033[0m\033[37;1m%s\033[0m\n" "$message"
|
|
|
|
}
|
|
|
|
|
2019-12-20 18:29:05 +05:30
|
|
|
# Function to log result of a operation
|
2019-10-17 01:41:13 +05:30
|
|
|
add_log() {
|
|
|
|
mark=$1
|
|
|
|
subject=$2
|
|
|
|
message=$3
|
|
|
|
if [ "$mark" = "$tick" ]; then
|
|
|
|
printf "\033[32;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s\033[0m\n" "$mark" "$subject" "$message"
|
|
|
|
else
|
|
|
|
printf "\033[31;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s\033[0m\n" "$mark" "$subject" "$message"
|
|
|
|
fi
|
|
|
|
}
|
2019-12-19 11:29:27 +05:30
|
|
|
|
2019-12-20 18:29:05 +05:30
|
|
|
# Function to update php ppa
|
|
|
|
update_ppa() {
|
|
|
|
if [ "$ppa_updated" = "false" ]; then
|
|
|
|
find /etc/apt/sources.list.d -type f -name 'ondrej-ubuntu-php*.list' -exec sudo DEBIAN_FRONTEND=noninteractive apt-fast update -o Dir::Etc::sourcelist="{}" ';' >/dev/null 2>&1
|
|
|
|
ppa_updated="true"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:31:18 +05:30
|
|
|
# Function to setup extensions
|
2019-12-20 18:29:05 +05:30
|
|
|
add_extension() {
|
2019-12-19 11:29:27 +05:30
|
|
|
extension=$1
|
2019-12-20 18:29:05 +05:30
|
|
|
install_command=$2
|
|
|
|
prefix=$3
|
|
|
|
if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then
|
|
|
|
echo "$prefix=$extension" >>"$ini_file" && add_log "$tick" "$extension" "Enabled"
|
|
|
|
elif php -m | grep -i -q -w "$extension"; then
|
|
|
|
add_log "$tick" "$extension" "Enabled"
|
|
|
|
elif ! php -m | grep -i -q -w "$extension"; then
|
|
|
|
(eval "$install_command" && add_log "$tick" "$extension" "Installed and enabled") ||
|
|
|
|
(update_ppa && eval "$install_command" && add_log "$tick" "$extension" "Installed and enabled") ||
|
|
|
|
add_log "$cross" "$extension" "Could not install $extension on PHP $semver"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:31:18 +05:30
|
|
|
# Function to remove extensions
|
|
|
|
remove_extension() {
|
|
|
|
extension=$1
|
2019-12-27 06:56:49 +05:30
|
|
|
if [ -e /etc/php/"$version"/mods-available/"$extension".ini ]; then
|
|
|
|
sudo phpdismod -v "$version" "$extension"
|
2019-12-26 18:31:18 +05:30
|
|
|
fi
|
2019-12-27 06:56:49 +05:30
|
|
|
sudo sed -i "/$extension/d" "$ini_file"
|
2020-01-10 06:18:53 +05:30
|
|
|
sudo rm -rf /etc/php/"$version"/cli/conf.d/*"$extension"* >/dev/null 2>&1
|
|
|
|
sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1
|
2019-12-27 06:56:49 +05:30
|
|
|
}
|
|
|
|
|
2020-01-14 06:57:04 +05:30
|
|
|
# Function to update extension
|
|
|
|
update_extension() {
|
|
|
|
extension=$1
|
|
|
|
latest_version=$2
|
|
|
|
current_version=$(php -r "echo phpversion('$extension');")
|
|
|
|
final_version=$(printf "%s\n%s" "$current_version" "$latest_version" | sort | tail -n 1)
|
|
|
|
if [ "$final_version" != "$current_version" ]; then
|
|
|
|
version_exists=$(apt-cache policy -- *"$extension" | grep "$final_version")
|
|
|
|
if [ -z "$version_exists" ]; then
|
|
|
|
update_ppa
|
|
|
|
fi
|
|
|
|
$apt_install php"$version"-"$extension"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-12-27 06:56:49 +05:30
|
|
|
# Function to setup a remote tool
|
|
|
|
add_tool() {
|
|
|
|
url=$1
|
|
|
|
tool=$2
|
|
|
|
if [ ! -e /usr/local/bin/"$tool" ]; then
|
|
|
|
rm -rf /usr/local/bin/"${tool:?}"
|
|
|
|
fi
|
2020-01-07 08:06:14 +05:30
|
|
|
status_code=$(sudo curl -s -w "%{http_code}" -o /usr/local/bin/"$tool" -L "$url")
|
|
|
|
if [ "$status_code" = "200" ]; then
|
|
|
|
sudo chmod a+x /usr/local/bin/"$tool"
|
|
|
|
add_log "$tick" "$tool" "Added"
|
|
|
|
else
|
|
|
|
add_log "$cross" "$tool" "Could not setup $tool"
|
|
|
|
fi
|
2020-01-14 12:06:52 +05:30
|
|
|
if [ "$tool" = "composer" ]; then
|
|
|
|
composer -q global config process-timeout 0
|
|
|
|
fi
|
2020-01-07 08:06:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
add_composer_tool() {
|
|
|
|
tool=$1
|
|
|
|
release=$2
|
|
|
|
prefix=$3
|
|
|
|
(
|
|
|
|
composer global require "$prefix$release" >/dev/null 2>&1 && \
|
|
|
|
sudo ln -sf "$(composer -q global config home)"/vendor/bin/"$tool" /usr/local/bin/"$tool" && \
|
2019-12-27 06:56:49 +05:30
|
|
|
add_log "$tick" "$tool" "Added"
|
2020-01-07 08:06:14 +05:30
|
|
|
) || add_log "$cross" "$tool" "Could not setup $tool"
|
2019-12-26 18:31:18 +05:30
|
|
|
}
|
|
|
|
|
2020-01-17 12:58:28 +05:30
|
|
|
# Function to setup phpize and php-config
|
|
|
|
add_devtools() {
|
|
|
|
if ! [ -e "/usr/bin/phpize$version" ] || ! [ -e "/usr/bin/php-config$version" ]; then
|
|
|
|
$apt_install php"$version"-dev php"$version"-xml >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
sudo update-alternatives --set php-config /usr/bin/php-config"$version" >/dev/null 2>&1
|
|
|
|
sudo update-alternatives --set phpize /usr/bin/phpize"$version" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2019-12-20 18:29:05 +05:30
|
|
|
# Function to setup the nightly build from master branch
|
|
|
|
setup_master() {
|
|
|
|
tar_file=php_"$version"%2Bubuntu"$(lsb_release -r -s)".tar.xz
|
|
|
|
install_dir=~/php/"$version"
|
|
|
|
sudo mkdir -m 777 -p ~/php
|
|
|
|
$apt_install libicu-dev >/dev/null 2>&1
|
|
|
|
curl -o "$tar_file" -L https://bintray.com/shivammathur/php/download_file?file_path="$tar_file" >/dev/null 2>&1
|
|
|
|
sudo tar xf "$tar_file" -C ~/php >/dev/null 2>&1
|
|
|
|
rm -rf "$tar_file"
|
|
|
|
sudo ln -sf -S "$version" "$install_dir"/bin/* /usr/bin/
|
|
|
|
sudo ln -sf "$install_dir"/etc/php.ini /etc/php.ini
|
2019-12-19 11:29:27 +05:30
|
|
|
}
|
|
|
|
|
2019-12-20 18:29:05 +05:30
|
|
|
# Function to setup PECL
|
2019-12-27 06:56:49 +05:30
|
|
|
add_pecl() {
|
|
|
|
update_ppa
|
2020-01-17 12:58:28 +05:30
|
|
|
add_devtools
|
2019-12-20 18:29:05 +05:30
|
|
|
wget https://github.com/pear/pearweb_phars/raw/master/install-pear-nozlib.phar >/dev/null 2>&1
|
|
|
|
sudo php install-pear-nozlib.phar >/dev/null 2>&1
|
2019-12-27 06:56:49 +05:30
|
|
|
sudo rm -rf install-pear-nozlib.phar >/dev/null 2>&1
|
2019-12-20 18:29:05 +05:30
|
|
|
sudo pear config-set php_ini "$ini_file" >/dev/null 2>&1
|
|
|
|
sudo pear config-set auto_discover 1 >/dev/null 2>&1
|
|
|
|
sudo pear channel-update pear.php.net >/dev/null 2>&1
|
2019-12-27 06:56:49 +05:30
|
|
|
add_log "$tick" "PECL" "Added"
|
2019-12-20 18:29:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
# Function to switch versions of PHP binaries
|
|
|
|
switch_version() {
|
|
|
|
for tool in pear pecl php phar phar.phar php-cgi php-config phpize phpdbg; do
|
|
|
|
if [ -e "/usr/bin/$tool$version" ]; then
|
|
|
|
sudo update-alternatives --set $tool /usr/bin/"$tool$version" >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Variables
|
|
|
|
tick="✓"
|
|
|
|
cross="✗"
|
|
|
|
ppa_updated="false"
|
|
|
|
version=$1
|
|
|
|
apt_install="sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y"
|
2019-10-11 04:41:25 +05:30
|
|
|
existing_version=$(php-config --version | cut -c 1-3)
|
2019-12-09 08:09:03 +05:30
|
|
|
semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-')
|
2019-12-20 18:29:05 +05:30
|
|
|
|
|
|
|
# Setup PHP
|
2019-12-27 06:56:49 +05:30
|
|
|
step_log "Setup PHP"
|
2019-11-27 21:56:29 +05:30
|
|
|
sudo mkdir -p /var/run
|
|
|
|
sudo mkdir -p /run/php
|
2019-12-20 18:29:05 +05:30
|
|
|
|
|
|
|
if [ "$existing_version" != "$version" ]; then
|
|
|
|
if [ ! -e "/usr/bin/php$version" ]; then
|
|
|
|
update_ppa
|
|
|
|
ppa_updated=1
|
|
|
|
if [ "$version" = "7.4" ]; then
|
2020-01-08 22:07:21 +05:30
|
|
|
$apt_install php"$version" php"$version"-curl php"$version"-mbstring php"$version"-xml php"$version"-phpdbg >/dev/null 2>&1
|
2019-12-20 18:29:05 +05:30
|
|
|
elif [ "$version" = "8.0" ]; then
|
|
|
|
setup_master
|
|
|
|
else
|
2020-01-08 22:07:21 +05:30
|
|
|
$apt_install php"$version" php"$version"-curl php"$version"-mbstring php"$version"-xml >/dev/null 2>&1
|
2019-12-20 18:29:05 +05:30
|
|
|
fi
|
|
|
|
status="installed"
|
|
|
|
else
|
|
|
|
status="switched"
|
|
|
|
fi
|
|
|
|
|
|
|
|
switch_version
|
|
|
|
|
|
|
|
semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-')
|
|
|
|
if [ "$version" = "8.0" ]; then
|
|
|
|
semver=$(php -v | head -n 1 | cut -f 2 -d ' ')
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$status" != "switched" ]; then
|
|
|
|
status="Installed PHP $semver"
|
|
|
|
else
|
|
|
|
status="Switched to PHP $semver"
|
|
|
|
fi
|
2019-11-30 05:58:14 +05:30
|
|
|
else
|
2019-12-09 08:09:03 +05:30
|
|
|
status="PHP $semver Found"
|
2019-10-17 01:41:13 +05:30
|
|
|
fi
|
|
|
|
|
|
|
|
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
|
|
|
|
ext_dir=$(php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||")
|
|
|
|
sudo chmod 777 "$ini_file"
|
|
|
|
add_log "$tick" "PHP" "$status"
|