Skip to content

Instantly share code, notes, and snippets.

@mtahle
Created May 24, 2024 00:44
Show Gist options
  • Save mtahle/ff224e3a84b3c96887f1ce7e5cc08adb to your computer and use it in GitHub Desktop.
Save mtahle/ff224e3a84b3c96887f1ce7e5cc08adb to your computer and use it in GitHub Desktop.
Installation of PHP 8.1, PHP-FPM, and required extensions for Magento 2.x
#!/bin/bash
# Error handling
set -e
set -o pipefail
error_exit() {
echo "Error: $1"
exit 1
}
echo "Starting installation of PHP 8.1, PHP-FPM, and required extensions for Magento 2.x."
echo "Updating package lists..."
sudo apt update || error_exit "Failed to update package lists."
echo "Checking for software-properties-common..."
if ! dpkg -l | grep -q software-properties-common; then
echo "Installing software-properties-common..."
sudo apt install -y software-properties-common || error_exit "Failed to install software-properties-common."
else
echo "software-properties-common already installed."
fi
echo "Checking for PHP PPA repository..."
if ! grep -q "ondrej/php" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
echo "Adding PHP PPA repository..."
sudo add-apt-repository -y ppa:ondrej/php || error_exit "Failed to add PHP PPA repository."
sudo apt update || error_exit "Failed to update package lists after adding PHP PPA."
else
echo "PHP PPA repository already added."
fi
echo "Checking for PHP 8.1 and PHP-FPM..."
if ! dpkg -l | grep -q php8.1; then
echo "Installing PHP 8.1 and PHP-FPM..."
sudo apt install -y php8.1 php8.1-fpm || error_exit "Failed to install PHP 8.1 and PHP-FPM."
else
echo "PHP 8.1 and PHP-FPM already installed."
fi
echo "Checking availability of PHP extensions in the repository..."
extensions=(
php8.1-bcmath
php8.1-ctype
php8.1-curl
php8.1-dom
php8.1-gd
php8.1-iconv
php8.1-intl
php8.1-mbstring
php8.1-openssl
php8.1-pdo
php8.1-mysql
php8.1-simplexml
php8.1-soap
php8.1-xsl
php8.1-zip
php8.1-xml
php8.1-phar
php8.1-json
php8.1-imagick
php8.1-mcrypt
)
# Check if extensions are available in the repository
available_extensions=()
unavailable_extensions=()
for extension in "${extensions[@]}"; do
if apt-cache search "^${extension}$" | grep -q "^${extension}"; then
available_extensions+=("$extension")
else
unavailable_extensions+=("$extension")
fi
done
if [ ${#available_extensions[@]} -eq 0 ]; then
error_exit "No required PHP extensions are available in the repository."
else
echo "The following PHP extensions will be installed: ${available_extensions[*]}"
sudo apt install -y "${available_extensions[@]}" || error_exit "Failed to install PHP extensions."
fi
if [ ${#unavailable_extensions[@]} -ne 0 ]; then
echo "The following PHP extensions are not available in the repository: ${unavailable_extensions[*]}"
fi
echo "Restarting PHP-FPM service..."
sudo systemctl restart php8.1-fpm || error_exit "Failed to restart PHP-FPM service."
echo "Enabling PHP-FPM service to start on boot..."
sudo systemctl enable php8.1-fpm || error_exit "Failed to enable PHP-FPM service."
echo "Verifying PHP installation..."
php -v || error_exit "PHP is not installed properly."
echo "Verifying PHP-FPM service status..."
sudo systemctl status php8.1-fpm || error_exit "PHP-FPM service is not running properly."
echo "PHP 8.1, PHP-FPM, and all available required extensions for Magento 2.x have been successfully installed."
read -p "Do you want to reboot the system to apply all changes? (y/n): " confirm
if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then
echo "Rebooting the system..."
sudo reboot
else
echo "Installation completed. Please reboot the system manually to apply all changes."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment