Skip to content

Commit

Permalink
feat: disable php extensions support via PHP_DISABLE_EXTENSIONS
Browse files Browse the repository at this point in the history
Example:

$ docker run --rm -p 8088:80 -e PHP_DISABLE_EXTENSIONS="amqp,mongodb,zstd" \
       joseluisq/php-fpm:8.3.12 sh -c "echo '<?php phpinfo();' > index.php; php -S [::]:80 -t ."

Disabling 3 extension(s): amqp mongodb zstd
OK: 'amqp' disabled
OK: 'mongodb' disabled
OK: 'zstd' disabled

Verifying PHP extensions...
PHP 8.3.12 (cli) (built: Sep 26 2024 23:00:14) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.12, Copyright (c), by Zend Technologies
    with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans
[05-Oct-2024 02:37:25] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful
Tests were successful!
[Sat Oct  5 02:37:25 2024] PHP 8.3.12 Development
Server (http://[::]:80) started
  • Loading branch information
joseluisq committed Oct 5, 2024
1 parent 48f393b commit 5e721b7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
68 changes: 68 additions & 0 deletions 8.3-fpm/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,72 @@ if [[ -n "$ENV_SUBSTITUTION_ENABLE" ]] && [[ "$ENV_SUBSTITUTION_ENABLE" = "true"
/envsubst.sh
fi

# Disable PHP extensions on demand
extensions=${PHP_DISABLE_EXTENSIONS//[[:blank:]]/}
extensions=${extensions//,/ }
extensions_count=$(echo $extensions | grep -o " " | wc -l)

if [[ -n "$extensions" ]]; then extensions_count=$((extensions_count + 1)); fi

if [[ $extensions_count -gt 0 ]]; then
echo "Disabling $extensions_count extension(s): $(echo $extensions)"

ext_dir=$(php -r 'echo ini_get("extension_dir");')
for ext in $extensions; do
disabled=0

ext_file="$ext_dir/$ext.so"
if [[ -f "$ext_file" ]]; then
mv -f $ext_file "$ext_file.disabled"
disabled=1
fi

ext_file_ini=${PHP_INI_DIR}/conf.d/docker-php-ext-$ext.ini
if [[ -f "$ext_file_ini" ]]; then
mv -f $ext_file_ini "$ext_file_ini.disabled"
disabled=1
fi

if [[ "$disabled" = 1 ]]; then
echo "OK: '$ext' disabled"
fi
done

echo
echo "Verifying PHP extensions..."

php -v
php-fpm --test

PHP_ERROR="$(php -v 2>&1 1>/dev/null)"

if [ -n "${PHP_ERROR}" ]; then
echo "${PHP_ERROR}"
false
fi

PHP_ERROR="$(php -i 2>&1 1>/dev/null)"

if [ -n "${PHP_ERROR}" ]; then
echo "${PHP_ERROR}"
false
fi

PHP_FPM_ERROR="$(php-fpm -v 2>&1 1>/dev/null)"

if [ -n "${PHP_FPM_ERROR}" ]; then
echo "${PHP_FPM_ERROR}"
false
fi

PHP_FPM_ERROR="$(php-fpm -i 2>&1 1>/dev/null)"

if [ -n "${PHP_FPM_ERROR}" ]; then
echo "${PHP_FPM_ERROR}"
false
fi

echo "Tests were successful!"
fi

exec "$@"
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ docker run --rm -p 8088:80 joseluisq/php-fpm:8.1 sh -c "echo '<?php phpinfo();'
- Additional PHP `.ini` files to load: `/usr/local/etc/php/conf.d`
- Custom PHP `.ini` file generated (only if `ENV_SUBSTITUTION_ENABLE=true`): `/usr/local/etc/php/conf.d/default-php.ini`

## Configurable environment variables
## Configurable Environment Variables

**PHP-FPM** and **PHP** configurations can be overwritten using environment variables.
To do so, just indicate the substitution of values using `ENV_SUBSTITUTION_ENABLE=true` (since it is disabled by default).
Expand Down Expand Up @@ -181,6 +181,12 @@ Settings replaced into `/usr/local/etc/php/conf.d/default-php.ini` file (`php.in
- `PHP_EXPOSE_PHP=On`
- `PHP_SESSION_GC_MAXLIFETIME=1440`

### Disable PHP extensions

The PHP extensions can be disabled at startup by providing the `PHP_DISABLE_EXTENSIONS` environment variable with one or more names. For example `PHP_DISABLE_EXTENSIONS=psr,exif,bz2`.

Find the valid extension names by using `php -m`. For example `docker run --rm joseluisq/php-fpm:8.3 php -m | grep "exif".

## Docker Compose examples

[docker-compose](https://docs.docker.com/compose/) examples for [Nginx](https://hub.docker.com/_/nginx) and [Apache](https://hub.docker.com/_/httpd) servers can be found under the [./examples](./examples) directory.
Expand Down

0 comments on commit 5e721b7

Please sign in to comment.