setup-php/src/scripts/linux.sh

109 lines
4.1 KiB
Bash
Raw Normal View History

2019-10-17 03:11:13 +07:00
tick="✓"
cross="✗"
step_log() {
message=$1
printf "\n\033[90;1m==> \033[0m\033[37;1m%s\033[0m\n" "$message"
}
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-10-11 06:11:25 +07:00
existing_version=$(php-config --version | cut -c 1-3)
2019-12-09 09:39:03 +07:00
semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-')
2019-10-17 03:11:13 +07:00
step_log "Setup PHP and Composer"
sudo mkdir -p /var/run
sudo mkdir -p /run/php
2019-11-02 20:20:10 +07:00
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
2019-10-11 06:11:25 +07:00
if [ "$existing_version" != "$1" ]; then
if [ ! -e "/usr/bin/php$1" ]; then
2019-12-09 09:39:03 +07:00
if [ "$1" = "7.4" ]; then
2019-11-29 13:11:25 +07:00
sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y php"$1" php"$1"-phpdbg php"$1"-xml curl php"$1"-curl >/dev/null 2>&1
2019-12-09 09:39:03 +07:00
elif [ "$1" = "8.0" ]; then
tar_file=php_"$1"%2Bubuntu"$(lsb_release -r -s)".tar.xz
install_dir=~/php/"$1"
sudo DEBIAN_FRONTEND=noninteractive apt-get -y 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 mkdir -m 777 -p ~/php
sudo tar xf "$tar_file" -C ~/php >/dev/null 2>&1 && rm -rf "$tar_file"
sudo ln -sf -S "$1" "$install_dir"/bin/* /usr/bin/ && sudo ln -sf "$install_dir"/etc/php.ini /etc/php.ini
else
sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y php"$1" curl php"$1"-curl >/dev/null 2>&1
2019-11-29 13:11:25 +07:00
fi
status="installed"
else
status="switched"
fi
2019-10-17 03:11:13 +07:00
2019-12-09 09:39:03 +07:00
for tool in pear pecl php phar phar.phar php-cgi php-config phpize phpdbg; do
if [ -e "/usr/bin/$tool$1" ]; then
2019-10-27 07:12:49 +07:00
sudo update-alternatives --set $tool /usr/bin/"$tool$1" >/dev/null 2>&1
fi
done
2019-12-09 14:47:02 +07:00
semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-')
if [ "$1" = "8.0" ]; then
semver=$(php -v | head -n 1 | cut -f 2 -d ' ')
fi
if [ "$status" != "switched" ]; then
2019-12-09 09:39:03 +07:00
status="Installed PHP $semver"
else
2019-12-09 09:39:03 +07:00
status="Switched to PHP $semver"
fi
else
2019-12-09 09:39:03 +07:00
status="PHP $semver Found"
2019-10-17 03:11:13 +07:00
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"
2019-11-07 12:41:35 +07:00
if [ "$2" = "true" ]; then
2019-11-28 17:28:33 +07:00
sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y php"$1"-dev php"$1"-xml >/dev/null 2>&1
2019-11-07 12:41:35 +07:00
sudo update-alternatives --set php-config /usr/bin/php-config"$1" >/dev/null 2>&1
sudo update-alternatives --set phpize /usr/bin/phpize"$1" >/dev/null 2>&1
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
sudo pear config-set php_ini "$ini_file" >/dev/null 2>&1
2019-12-10 07:04:26 +07:00
sudo pear config-set auto_discover 1
sudo pear channel-update pear.php.net
2019-11-07 12:41:35 +07:00
add_log "$tick" "PECL" "Installed"
fi
if [ ! -e "/usr/bin/composer" ]; then
2019-10-12 00:39:05 +07:00
curl -s -L https://getcomposer.org/installer > composer-setup.php
if [ "$(curl -s https://composer.github.io/installer.sig)" != "$(php -r "echo hash_file('sha384', 'composer-setup.php');")" ]; then
2019-09-25 18:34:10 +07:00
>&2 echo 'ERROR: Invalid installer signature'
else
2019-10-12 00:39:05 +07:00
export COMPOSER_ALLOW_SUPERUSER=1
2019-10-10 19:17:17 +07:00
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
fi
2019-09-25 18:34:10 +07:00
rm composer-setup.php
fi
2019-09-28 04:36:41 +07:00
composer global require hirak/prestissimo >/dev/null 2>&1
2019-10-17 03:11:13 +07:00
add_log "$tick" "Composer" "Installed"
2019-10-11 06:11:25 +07:00
add_extension()
{
extension=$1
install_command=$2
prefix=$3
2019-12-16 20:54:49 +07:00
if ! php -m | grep -i -q ^"$extension"$ && [ -e "$ext_dir/$extension.so" ]; then
2019-10-17 03:11:13 +07:00
echo "$prefix=$extension" >> "$ini_file" && add_log "$tick" "$extension" "Enabled"
2019-12-16 20:54:49 +07:00
elif php -m | grep -i -q ^"$extension"$; then
2019-10-17 03:11:13 +07:00
add_log "$tick" "$extension" "Enabled"
2019-12-16 20:54:49 +07:00
elif ! php -m | grep -i -q ^"$extension"$; then
2019-10-17 03:11:13 +07:00
(
eval "$install_command" && \
add_log "$tick" "$extension" "Installed and enabled"
2019-12-09 09:39:03 +07:00
) || add_log "$cross" "$extension" "Could not install $extension on PHP $semver"
2019-10-11 06:11:25 +07:00
fi
2019-10-17 03:11:13 +07:00
}