Unverified Commit 63e9392a authored by Samruddhi Khandale's avatar Samruddhi Khandale Committed by GitHub

PHP: Install additional versions (#61)

* PHP: install additional versions

* fix bug

* fix test scenario

* adding quotes

* change identifier

* nit

* tweak: to unblock git merge

* nit
parent 27e63203
...@@ -31,9 +31,5 @@ ...@@ -31,9 +31,5 @@
"containerEnv": { "containerEnv": {
"PHP_PATH": "/usr/local/php/current", "PHP_PATH": "/usr/local/php/current",
"PATH": "${PHP_PATH}/bin:${PATH}" "PATH": "${PHP_PATH}/bin:${PATH}"
},
"install": {
"app": "",
"file": "install.sh"
} }
} }
\ No newline at end of file
...@@ -16,6 +16,10 @@ export PHP_DIR=${PHP_DIR:-"/usr/local/php"} ...@@ -16,6 +16,10 @@ export PHP_DIR=${PHP_DIR:-"/usr/local/php"}
USERNAME=${USERNAME:-"automatic"} USERNAME=${USERNAME:-"automatic"}
UPDATE_RC=${UPDATE_RC:-"true"} UPDATE_RC=${UPDATE_RC:-"true"}
# Comma-separated list of php versions to be installed
# alongside VERSION, but not set as default.
ADDITIONAL_VERSIONS=${ADDITIONAL_VERSIONS:-""}
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
if [ "$(id -u)" -ne 0 ]; then if [ "$(id -u)" -ne 0 ]; then
...@@ -111,91 +115,108 @@ PHPIZE_DEPS="autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c" ...@@ -111,91 +115,108 @@ PHPIZE_DEPS="autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c"
# Install dependencies # Install dependencies
check_packages $RUNTIME_DEPS $PHP_DEPS $PHPIZE_DEPS check_packages $RUNTIME_DEPS $PHP_DEPS $PHPIZE_DEPS
# Fetch latest version of PHP if needed install_php() {
if [ "${VERSION}" = "latest" ] || [ "${VERSION}" = "lts" ]; then VERSION="$1"
find_version_from_git_tags
fi
PHP_INSTALL_DIR="${PHP_DIR}/${VERSION}" # Fetch latest version of PHP if needed
if [ -d "${PHP_INSTALL_DIR}" ]; then if [ "${VERSION}" = "latest" ] || [ "${VERSION}" = "lts" ]; then
echo "(!) PHP version ${VERSION} already exists." find_version_from_git_tags
exit 1 fi
fi
if ! cat /etc/group | grep -e "^php:" > /dev/null 2>&1; then PHP_INSTALL_DIR="${PHP_DIR}/${VERSION}"
groupadd -r php if [ -d "${PHP_INSTALL_DIR}" ]; then
fi echo "(!) PHP version ${VERSION} already exists."
usermod -a -G php "${USERNAME}" exit 1
fi
PHP_URL="https://www.php.net/distributions/php-${VERSION}.tar.gz" if ! cat /etc/group | grep -e "^php:" > /dev/null 2>&1; then
groupadd -r php
fi
usermod -a -G php "${USERNAME}"
PHP_INI_DIR="${PHP_INSTALL_DIR}/ini" PHP_URL="https://www.php.net/distributions/php-${VERSION}.tar.gz"
CONF_DIR="${PHP_INI_DIR}/conf.d"
mkdir -p "${CONF_DIR}";
PHP_EXT_DIR="${PHP_INSTALL_DIR}/extensions" PHP_INI_DIR="${PHP_INSTALL_DIR}/ini"
mkdir -p "${PHP_EXT_DIR}" CONF_DIR="${PHP_INI_DIR}/conf.d"
mkdir -p "${CONF_DIR}";
PHP_SRC_DIR="/usr/src/php" PHP_EXT_DIR="${PHP_INSTALL_DIR}/extensions"
mkdir -p $PHP_SRC_DIR mkdir -p "${PHP_EXT_DIR}"
cd $PHP_SRC_DIR
wget -O php.tar.xz "$PHP_URL"
tar -xf $PHP_SRC_DIR/php.tar.xz -C "$PHP_SRC_DIR" --strip-components=1 PHP_SRC_DIR="/usr/src/php"
cd $PHP_SRC_DIR; mkdir -p $PHP_SRC_DIR
cd $PHP_SRC_DIR
wget -O php.tar.xz "$PHP_URL"
# PHP 7.4+, the pecl/pear installers are officially deprecated and are removed in PHP 8+ tar -xf $PHP_SRC_DIR/php.tar.xz -C "$PHP_SRC_DIR" --strip-components=1
# Thus, requiring an explicit "--with-pear" cd $PHP_SRC_DIR;
IFS="."
read -a versions <<< "${VERSION}"
PHP_MAJOR_VERSION=${versions[0]}
PHP_MINOR_VERSION=${versions[1]}
VERSION_CONFIG="" # PHP 7.4+, the pecl/pear installers are officially deprecated and are removed in PHP 8+
if (( $(($PHP_MAJOR_VERSION)) >= 8 )) || (( $(($PHP_MAJOR_VERSION)) == 7 && $(($PHP_MINOR_VERSION)) >= 4 )); then # Thus, requiring an explicit "--with-pear"
VERSION_CONFIG="--with-pear" IFS="."
fi read -a versions <<< "${VERSION}"
PHP_MAJOR_VERSION=${versions[0]}
PHP_MINOR_VERSION=${versions[1]}
./configure --prefix="${PHP_INSTALL_DIR}" --with-config-file-path="$PHP_INI_DIR" --with-config-file-scan-dir="$CONF_DIR" --enable-option-checking=fatal --with-curl --with-libedit --with-openssl --with-zlib --with-password-argon2 --with-sodium=shared "$VERSION_CONFIG" EXTENSION_DIR="$PHP_EXT_DIR"; VERSION_CONFIG=""
if (( $(($PHP_MAJOR_VERSION)) >= 8 )) || (( $(($PHP_MAJOR_VERSION)) == 7 && $(($PHP_MINOR_VERSION)) >= 4 )); then
VERSION_CONFIG="--with-pear"
fi
make -j "$(nproc)" ./configure --prefix="${PHP_INSTALL_DIR}" --with-config-file-path="$PHP_INI_DIR" --with-config-file-scan-dir="$CONF_DIR" --enable-option-checking=fatal --with-curl --with-libedit --with-openssl --with-zlib --with-password-argon2 --with-sodium=shared "$VERSION_CONFIG" EXTENSION_DIR="$PHP_EXT_DIR";
find -type f -name '*.a' -delete
make install
find "${PHP_INSTALL_DIR}" -type f -executable -exec strip --strip-all '{}' + || true
make clean
cp -v $PHP_SRC_DIR/php.ini-* "$PHP_INI_DIR/"; make -j "$(nproc)"
cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" find -type f -name '*.a' -delete
make install
find "${PHP_INSTALL_DIR}" -type f -executable -exec strip --strip-all '{}' + || true
make clean
# Install xdebug cp -v $PHP_SRC_DIR/php.ini-* "$PHP_INI_DIR/";
"${PHP_INSTALL_DIR}/bin/pecl" install xdebug cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
XDEBUG_INI="${CONF_DIR}/xdebug.ini"
echo "zend_extension=${PHP_EXT_DIR}/xdebug.so" > "${XDEBUG_INI}" # Install xdebug
echo "xdebug.mode = debug" >> "${XDEBUG_INI}" "${PHP_INSTALL_DIR}/bin/pecl" install xdebug
echo "xdebug.start_with_request = yes" >> "${XDEBUG_INI}" XDEBUG_INI="${CONF_DIR}/xdebug.ini"
echo "xdebug.client_port = 9003" >> "${XDEBUG_INI}"
# Install PHP Composer if needed echo "zend_extension=${PHP_EXT_DIR}/xdebug.so" > "${XDEBUG_INI}"
if [[ "${INSTALL_COMPOSER}" = "true" ]] || [[ $(composer --version) = "" ]]; then echo "xdebug.mode = debug" >> "${XDEBUG_INI}"
addcomposer echo "xdebug.start_with_request = yes" >> "${XDEBUG_INI}"
fi echo "xdebug.client_port = 9003" >> "${XDEBUG_INI}"
CURRENT_DIR="${PHP_DIR}/current" # Install PHP Composer if needed
if [[ ! -d "${CURRENT_DIR}" ]]; then if [[ "${INSTALL_COMPOSER}" = "true" ]] || [[ $(composer --version) = "" ]]; then
ln -s -r "${PHP_INSTALL_DIR}" ${CURRENT_DIR} addcomposer
fi fi
if [ "${OVERRIDE_DEFAULT_VERSION}" = "true" ]; then CURRENT_DIR="${PHP_DIR}/current"
if [[ $(ls -l ${CURRENT_DIR}) != *"-> ${PHP_INSTALL_DIR}"* ]] ; then if [[ ! -d "${CURRENT_DIR}" ]]; then
rm "${CURRENT_DIR}"
ln -s -r "${PHP_INSTALL_DIR}" ${CURRENT_DIR} ln -s -r "${PHP_INSTALL_DIR}" ${CURRENT_DIR}
fi fi
fi
rm -rf ${PHP_SRC_DIR} if [ "${OVERRIDE_DEFAULT_VERSION}" = "true" ]; then
if [[ $(ls -l ${CURRENT_DIR}) != *"-> ${PHP_INSTALL_DIR}"* ]] ; then
rm "${CURRENT_DIR}"
ln -s -r "${PHP_INSTALL_DIR}" "${CURRENT_DIR}"
fi
fi
rm -rf "${PHP_SRC_DIR}"
updaterc "if [[ \"\${PATH}\" != *\"${CURRENT_DIR}\"* ]]; then export PATH=\"${CURRENT_DIR}/bin:\${PATH}\"; fi"
}
updaterc "if [[ \"\${PATH}\" != *\"${CURRENT_DIR}\"* ]]; then export PATH=${CURRENT_DIR}/bin:\${PATH}; fi" install_php "${VERSION}"
# Additional php versions to be installed but not be set as default.
if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
OLDIFS=$IFS
IFS=","
read -a additional_versions <<< "$ADDITIONAL_VERSIONS"
for version in "${additional_versions[@]}"; do
OVERRIDE_DEFAULT_VERSION="false"
install_php "${version}"
done
IFS=$OLDIFS
fi
chown -R "${USERNAME}:php" "${PHP_DIR}" chown -R "${USERNAME}:php" "${PHP_DIR}"
chmod -R g+r+w "${PHP_DIR}" chmod -R g+r+w "${PHP_DIR}"
......
#!/bin/bash
set -e
# Optional: Import test library
source dev-container-features-test-lib
check "php version 8.1.4 installed as default" php --version | grep 8.1.4
check "php version 8.0.17 installed" ls -l /usr/local/php | grep 8.0.17
check "php version 8.0.3 installed" ls -l /usr/local/php | grep 8.0.3
# Report result
reportResults
{ {
"install_additional_php": {
"image": "ubuntu:focal",
"features": {
"php": {
"version": "8.1.4",
"additional_versions": "8.0.17,8.0.3"
}
}
},
"install_additional_java": { "install_additional_java": {
"image": "ubuntu:focal", "image": "ubuntu:focal",
"features": { "features": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment