apache/scripts/check_php.sh
2023-12-01 00:31:34 +01:00

274 lines
9.3 KiB
Bash

#!/bin/bash
echo "Checking php..."
# Install last php version supported by wordpress
DISTRO=`python3 -c 'import distro; print(distro.id())'`
PHP_VERSION=$(php -v 2>/dev/null | grep -oE "PHP ([0-9]+\.[0-9]+)" | grep -oE "[0-9]+\.[0-9]+")
CHOOSE_PHP_VERSION="8.2"
if [ -n $1 ]; then
CHOOSE_PHP_VERSION=$1
fi
#echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "PHP Version is "}'
# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1F3045A5DF7587C3
if [ "$DISTRO" = 'debian' ] || [ "$DISTRO" = 'ubuntu' ]; then
echo "Update PHP if you need..."
if [ ! -f '/etc/apt/sources.list.d/php.list' ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Installing Sury repos..."}'
if [ "$DISTRO" = 'debian' ]; then
# sudo wget -O - https://packages.sury.org/php/README.txt | sh -s
sudo apt-get update
sudo apt-get -y install lsb-release ca-certificates curl
sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sudo ln -s /usr/share/keyrings/deb.sury.org-php.gpg /etc/apt/trusted.gpg.d/deb.sury.org-php.gpg
sudo sh -c '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'
sudo apt-get update
else
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
fi
fi
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Installed Sury repos sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot install Sury repos..."}'
exit
fi
sudo apt-get update
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Updated with sury repos sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot update apt repos with sury repos, retriying..."}'
exit
fi
if [ "$PHP_VERSION" = "$CHOOSE_PHP_VERSION" ]; then
echo "Ok, you have php ${CHOOSE_PHP_VERSION}"
echo "Updating php-fpm pm to ondemand"
sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php/${CHOOSE_PHP_VERSION}/fpm/pool.d/www.conf
else
sudo apt-get install -y php${CHOOSE_PHP_VERSION} php${CHOOSE_PHP_VERSION}-fpm php${CHOOSE_PHP_VERSION}-gd php${CHOOSE_PHP_VERSION}-mysql php${CHOOSE_PHP_VERSION}-curl php${CHOOSE_PHP_VERSION}-mbstring php${CHOOSE_PHP_VERSION}-intl php${CHOOSE_PHP_VERSION}-imagick php${CHOOSE_PHP_VERSION}-xml php${CHOOSE_PHP_VERSION}-zip php${CHOOSE_PHP_VERSION}-redis unzip mariadb-client
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Installed php ${CHOOSE_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Error: cannot install php ${CHOOSE_PHP_VERSION}\"}"
exit;
fi
sudo systemctl restart php${CHOOSE_PHP_VERSION}-fpm.service
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Restarted php ${CHOOSE_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Cannot restart php ${CHOOSE_PHP_VERSION}...\"}"
exit;
fi
echo "Updating php-fpm pm to ondemand"
sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php/${CHOOSE_PHP_VERSION}/fpm/pool.d/www.conf
fi
if [ -f "/usr/local/bin/composer" ]; then
echo "Updating composer..."
# need sudo for composer autoupdate
sudo /usr/local/bin/composer self-update
else
echo "Installing composer..."
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi
php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
if [ $RESULT -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Composer installed sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot install composer!"}'
exit 1
fi
fi
elif [ "$DISTRO" = 'rocky' ] || [ "$DISTRO" = 'fedora' ]; then
echo "Update PHP if you need..."
sudo dnf -y update
FINAL_PHP_VERSION="${CHOOSE_PHP_VERSION/\./}"
#echo $FINAL_PHP_VERSION
if [ "$PHP_VERSION" = "$CHOOSE_PHP_VERSION" ]; then
echo "Ok, you have php ${CHOOSE_PHP_VERSION}"
# Check mariadb installed
sudo dnf -y install mariadb
echo "Updating php-fpm pm to ondemand"
sudo sed -i 's/pm = dynamic/pm = ondemand/g' /etc/opt/remi/php${FINAL_PHP_VERSION}/php-fpm.d/www.conf
else
sudo dnf -y install php${FINAL_PHP_VERSION} php${FINAL_PHP_VERSION}-php-gd php${FINAL_PHP_VERSION}-php-mysqlnd php${FINAL_PHP_VERSION}-php-imap php${FINAL_PHP_VERSION}-php-intl php${FINAL_PHP_VERSION}-php-fpm php${FINAL_PHP_VERSION}-php-process composer unzip mariadb php${FINAL_PHP_VERSION}-php-pecl-zip
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Installed php ${FINAL_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Error: cannot install php ${FINAL_PHP_VERSION} for wordpress\"}"
exit;
fi
#Installing php8.2-fpm
#/etc/opt/remi/php82/php-fpm.d/www.conf
#sudo sed -i 's/user = apache/user = hosting/g' /etc/opt/remi/php82/php-fpm.d/www.conf
#sudo sed -i 's/group = apache/group = hosting/g' /etc/opt/remi/php82/php-fpm.d/www.conf
#pm = dynamic
sudo systemctl restart php${FINAL_PHP_VERSION}-php-fpm.service
if [ $? -eq 0 ]; then
echo "{\"error\": 0, \"status\": 0, \"progress\": 100, \"no_progress\":0, \"message\": \"Restart php ${FINAL_PHP_VERSION} sucessfully...\"}"
else
echo "{\"error\": 1, \"status\": 1, \"progress\": 100, \"no_progress\":0, \"message\": \"Error: cannot restart php ${FINAL_PHP_VERSION}\"}"
exit;
fi
fi
else
sudo pacman -S --needed --noconfirm php-legacy php-legacy-gd php-legacy-fpm php-legacy-sodium composer mariadb
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "php-legacy installed..."}'
else
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Cannot install php legacy in arch..."}'
exit;
fi
sed -i 's/pm = dynamic/pm = ondemand/g' /etc/php-legacy/php-fpm.d/www.conf
sudo sed -i 's/^;zend_extension=opcache$/zend_extension=opcache/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=iconv$/extension=iconv/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=intl$/extension=intl/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=mysqli$/extension=mysqli/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=gd$/extension=gd/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=bcmath$/extension=bcmath/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=bz2$/extension=bz2/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=ftp$/extension=ftp/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=gd$/extension=gd/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=gettext$/extension=gettext/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=sockets$/extension=sockets/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=sodium$/extension=sodium/g' /etc/php-legacy/php.ini
sudo sed -i 's/^;extension=pdo_mysql$/extension=pdo_mysql/g' /etc/php-legacy/php.ini
sudo systemctl enable php-legacy-fpm.service && sudo systemctl restart php-legacy-fpm.service
if [ $? -eq 0 ]; then
echo '{"error": 0, "status": 0, "progress": 100, "no_progress":0, "message": "Restarted php legacy sucessfully..."}'
else
echo '{"error": 1, "status": 1, "progress": 100, "no_progress":0, "message": "Error: cannot restart php-legacy"}'
exit;
fi
fi