How to Install the PHP XSL Extension on aaPanel Using a Custom Bash Script

Prerequisites:

  • A running instance of aaPanel.
  • Basic knowledge of SSH and bash scripting.
  • PHP installed on aaPanel.

Step-by-Step Guide:

1. Bash Script:

Run the following bash command

bash <(curl -s https://gist.githubusercontent.com/takielias/ef13a92d7a9371216eefb517c9994c61/raw/f3900c87516a6b2d549a95d9520453371a427239/xsl.sh) install 82

Or you may create a .sh file containing the following script

#!/bin/bash

# Setting the PATH to ensure all required commands are available
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$HOME/bin

# Function to install XSL extension
Install_Xsl()
{
    # Ensure the public.sh file is available
    public_file=/www/server/panel/install/public.sh
    if [ ! -f $public_file ];then
        wget -O $public_file https://download.bt.cn/install/public.sh -T 5;
    fi
    . $public_file

    # Set download URL from the sourced public file
    download_Url=$NODE_URL
    
    # Check if xsl.so is already in the configuration
    isInstall=`cat /www/server/php/$version/etc/php.ini | grep 'xsl.so'`
    if [ "${isInstall}" != "" ];then
        echo "php-$vphp already has xsl installed, please choose another version!"
        return
    fi
    
    # Download and extract the extension if not already present
    if [ ! -d "/www/server/php/$version/src/ext/xsl" ];then
        mkdir -p /www/server/php/$version/src
        wget -O ext-$version.zip $download_Url/install/ext/ext-$version.zip
        unzip -o ext-$version.zip -d /www/server/php/$version/src/ > /dev/null
        mv /www/server/php/$version/src/ext-$version /www/server/php/$version/src/ext
        rm -f ext-$version.zip
    fi

    # Locate and define the extension file based on PHP version
    case "${version}" in
        '53') extFile='/www/server/php/53/lib/php/extensions/no-debug-non-zts-20090626/xsl.so' ;;
        '54') extFile='/www/server/php/54/lib/php/extensions/no-debug-non-zts-20100525/xsl.so' ;;
        '55') extFile='/www/server/php/55/lib/php/extensions/no-debug-non-zts-20121212/xsl.so' ;;
        '56') extFile='/www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226/xsl.so' ;;
        '70') extFile='/www/server/php/70/lib/php/extensions/no-debug-non-zts-20151012/xsl.so' ;;
        '71') extFile='/www/server/php/71/lib/php/extensions/no-debug-non-zts-20160303/xsl.so' ;;
        '72') extFile='/www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/xsl.so' ;;
        '73') extFile='/www/server/php/73/lib/php/extensions/no-debug-non-zts-20180731/xsl.so' ;;
        '74') extFile='/www/server/php/74/lib/php/extensions/no-debug-non-zts-20190902/xsl.so' ;;
        '80') extFile='/www/server/php/80/lib/php/extensions/no-debug-non-zts-20200930/xsl.so' ;;
        '81') extFile='/www/server/php/81/lib/php/extensions/no-debug-non-zts-20210902/xsl.so' ;;
        '82') extFile='/www/server/php/82/lib/php/extensions/no-debug-non-zts-20220829/xsl.so' ;;
    esac
    
    # Compile and install the extension if not present
    if [ ! -f "$extFile" ];then
        cd /www/server/php/$version/src/ext/xsl
        /www/server/php/$version/bin/phpize
        ./configure --with-php-config=/www/server/php/$version/bin/php-config
        make && make install
    fi
    
    # Check if the installation was successful
    if [ ! -f "${extFile}" ];then
        echo "ERROR during xsl installation!"
        return;
    fi
    
    # Update PHP configuration files
    echo "extension=$extFile" >> /www/server/php/$version/etc/php.ini
    if [ -f /www/server/php/$version/etc/php-cli.ini ];then
        echo "extension = $extFile" >> /www/server/php/$version/etc/php-cli.ini
    fi
    
    # Reload the PHP-FPM service
    service php-fpm-$version reload
    echo '==========================================================='
    echo 'XSL installation successful!'
}

Uninstall_Xsl()
{
	if [ ! -f "/www/server/php/$version/bin/php-config" ];then
		echo "php-$vphp 未安装,请选择其它版本!"
		return
	fi
	isInstall=`cat /www/server/php/$version/etc/php.ini|grep 'xsl.so'`
	if [ "${isInstall}" == "" ];then
		echo "php-$vphp 未安装xsl,请选择其它版本!"
		return
	fi

	sed -i '/xsl.so/d'  /www/server/php/$version/etc/php.ini
	if [ -f /www/server/php/$version/etc/php-cli.ini ];then
        sed -i '/xsl.so/d' /www/server/php/$version/etc/php-cli.ini
    fi
	service php-fpm-$version reload
	echo '==============================================='
	echo 'successful!'
}

# Main logic to handle script parameters for install or uninstall actions
actionType=$1
version=$2
vphp=${version:0:1}.${version:1:1}
if [ "$actionType" == 'install' ];then
    Install_Xsl
elif [ "$actionType" == 'uninstall' ];then
    Uninstall_Xsl
else
    echo "Invalid action type specified. Use 'install' or 'uninstall'."
    exit 1
fi

2. Script Breakdown:

PATH Setup:
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$HOME/bin

This line ensures that all the necessary system commands (wget, unzip, phpize, etc.) are available during script execution by setting up the environment PATH.

Public File Check and Sourcing:
public_file=/www/server/panel/install/public.sh
if [ ! -f $public_file ];then
    wget -O $public_file https://download.bt.cn/install/public.sh -T 5;
fi
. $public_file
  • public_file: Defines the path where the public.sh file should exist.
  • Check for public.sh: If the public.sh file doesn’t exist, it downloads it from a specified URL.
  • Source public.sh: The script sources public.sh, which is expected to set up necessary variables (like NODE_URL) required for the installation process.
Checking for Existing XSL Installation:
isInstall=`cat /www/server/php/$version/etc/php.ini | grep 'xsl.so'`
if [ "${isInstall}" != "" ];then
    echo "php-$vphp already has xsl installed, please choose another version!"
    return
fi
  • isInstall: Checks if the xsl.so extension is already configured in the PHP php.ini file.
  • If xsl.so is found, the script outputs a message and exits to prevent reinstallation.
Downloading and Extracting the XSL Extension:
if [ ! -d "/www/server/php/$version/src/ext/xsl" ];then
    mkdir -p /www/server/php/$version/src
    wget -O ext-$version.zip $download_Url/install/ext/ext-$version.zip
    unzip -o ext-$version.zip -d /www/server/php/$version/src/ > /dev/null
    mv /www/server/php/$version/src/ext-$version /www/server/php/$version/src/ext
    rm -f ext-$version.zip
fi
  • Directory Check: If the directory for the XSL extension doesn’t exist, it proceeds to create it.
  • Download: Downloads the extension source code as a zip file.
  • Extract: Extracts the contents into the appropriate directory and removes the zip file.
Define the Extension File Path:
case "${version}" in
    '53') extFile='/www/server/php/53/lib/php/extensions/no-debug-non-zts-20090626/xsl.so' ;;
    ...
    '82') extFile='/www/server/php/82/lib/php/extensions/no-debug-non-zts-20220829/xsl.so' ;;
esac
  • This section sets the appropriate path for the xsl.so extension file based on the PHP version being used.
Compiling and Installing the XSL Extension:
if [ ! -f "$extFile" ];then
    cd /www/server/php/$version/src/ext/xsl
    /www/server/php/$version/bin/phpize
    ./configure --with-php-config=/www/server/php/$version/bin/php-config
    make && make install
fi
  • Compile Check: If the xsl.so file doesn’t exist at the defined path, the script will compile the extension.
  • phpize: Prepares the PHP extension for compilation.
  • configure: Configures the build process with the PHP installation.
  • make && make install: Compiles and installs the extension.
Verifying Installation Success:
if [ ! -f "${extFile}" ];then
    echo "ERROR during xsl installation!"
    return;
fi
  • After compilation, it checks if the xsl.so file exists. If not, it outputs an error message and exits.
Updating PHP Configuration:
echo "extension=$extFile" >> /www/server/php/$version/etc/php.ini
if [ -f /www/server/php/$version/etc/php-cli.ini ];then
    echo "extension = $extFile" >> /www/server/php/$version/etc/php-cli.ini
fi
  • php.ini Update: Adds the XSL extension to the PHP configuration.
  • php-cli.ini Update: If the CLI configuration file exists, it also updates it with the XSL extension.
Reloading PHP-FPM Service:
service php-fpm-$version reload
echo '==========================================================='
echo 'XSL installation successful!'
  • Reload Service: Reloads the PHP-FPM service to apply the new configuration.
  • Success Message: Outputs a success message indicating the installation was successful.
Uninstalling XSL Extension:

The Uninstall_Xsl() function follows a similar structure:

  1. Check PHP Installation: Ensures the specified PHP version is installed.
  2. Check XSL Installation: Verifies if XSL is installed before attempting to uninstall it.
  3. Remove Configuration: Removes the xsl.so entries from php.ini and php-cli.ini.
  4. Reload PHP-FPM: Reloads the PHP-FPM service to apply the changes.
  5. Success Message: Outputs a success message after uninstallation.
Main Logic:
actionType=$1
version=$2
vphp=${version:0:1}.${version:1:1}
if [ "$actionType" == 'install' ];then
    Install_Xsl
elif [ "$actionType" == 'uninstall' ];then
    Uninstall_Xsl
else
    echo "Invalid action type specified. Use 'install' or 'uninstall'."
    exit 1
fi
  • Parameters: The script takes two parameters: actionType (install or uninstall) and version (PHP version).
  • Determine Action: Based on actionType, it calls either Install_Xsl or Uninstall_Xsl.

This detailed breakdown explains each part of the script, making it easier to understand the logic and flow.

3. Installation Process

Make the Script Executable:
You need to make the script executable:

   chmod +x xsl_install.sh

Run the Script to Install XSL:
To install the XSL extension for a specific PHP version (e.g., PHP 8.2), run the following command:

   sudo ./xsl_install.sh install 82

Replace 82 with the appropriate version number if you’re using a different PHP version.

Verify the Installation:
After the script completes, you can verify that the XSL extension was successfully installed by running:

   php -m | grep xsl

If the XSL extension is installed, this command will return xsl.

Conclusion

By following the steps outlined above, you can easily install the XSL extension on your aaPanel-managed server using a custom bash script. This script automates the process of downloading, compiling, and configuring the XSL extension for various PHP versions, saving you time and reducing the potential for errors.

Whether you’re managing multiple PHP versions or just want a streamlined way to handle extension installations, this script provides a reliable solution. If you encounter any issues or have questions, feel free to leave a comment below or reach out to the aaPanel community for support.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.