#!/bin/bash

APT_PKGS="php-common libphp-embed php-json php-pear php-amqp php-apcu php-ast php-bcmath php-bz2 php-cgi php-cli php-curl php-dba php-dev php-ds php-enchant php-fpm php-gd php-gearman php-gmp php-gnupg php-igbinary php-imagick php-imap php-interbase php-intl php-ldap php-mailparse php-mbstring php-memcache php-memcached php-mongodb php-msgpack php-mysql php-oauth php-odbc php-opcache php-pcov php-pgsql php-phpdbg php-ps php-pspell php-psr php-raphf php-readline php-redis php-rrd php-snmp php-soap php-solr php-sqlite3 php-ssh2 php-sybase php-tidy php-uopz php-uuid php-xdebug php-xml php-xmlrpc php-xsl php-yaml php-zip php-zmq php-mailparse"

apt install -y ${APT_PKGS} && \
echo '' && \
echo 'Finished to install php-fpm. Now, removing unusing packages' && \
echo '=========================================================================================' && \
echo '' && \
apt install -y apache2-utils && \
echo '' && \
echo "Pinned to install the package 'apache2-utils' for using HTTP Basic Authentication." && \
echo '=========================================================================================' && \
echo '' && \
#apt remove --purge -y apache2 apache2-bin apache2-data libapache2-mod-php libapache2-mod-php8.1 && \
apt autoremove -y && \
apt clean all && apt autoclean all && \
export PHP_INSTALLED_VERSION=$(ls -lrth /etc/php/ |tail -n 1 | cut -d ' ' -f10)
if [ "$PHP_INSTALLED_VERSION" = "" ]; then
	export PHP_INSTALLED_VERSION=$(ls -lrth /etc/php/ |tail -n 1 | cut -d ' ' -f9) 
fi
export PHP_FPM_CONF_UBUNTU="/etc/php/${PHP_INSTALLED_VERSION}/fpm/pool.d/www.conf"
echo '' && \
echo '' && \
echo '' && \
sed -i 's|;listen.mode = 0660|listen.mode = 0660|g' "${PHP_FPM_CONF_UBUNTU}" && \
sed -i 's|pm.max_children = 5|pm.max_children = 120|g' "${PHP_FPM_CONF_UBUNTU}" && \
sed -i 's|pm.start_servers = 2|pm.start_servers = 12|g' "${PHP_FPM_CONF_UBUNTU}" && \
sed -i 's|pm.min_spare_servers = 1|pm.min_spare_servers = 6|g' "${PHP_FPM_CONF_UBUNTU}" && \
sed -i 's|pm.max_spare_servers = 3|pm.max_spare_servers = 18|g' "${PHP_FPM_CONF_UBUNTU}" && \
systemctl restart "php${PHP_INSTALLED_VERSION}-fpm.service" && \
echo '' && \
echo '' && \
echo '================ Finished to tune www.conf ==================='

