Skip to content

Commit

Permalink
feat: support for disabling additional PHP extensions via `PHP_DISABL…
Browse files Browse the repository at this point in the history
…E_EXTENSIONS` env (#46)

* feat: disable php extensions support via `PHP_DISABLE_EXTENSIONS`

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

* feat: disable extensions support for 8.1, 8.2 & add extensions list
  • Loading branch information
joseluisq authored Oct 20, 2024
1 parent 48f393b commit 83d9123
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 1 deletion.
68 changes: 68 additions & 0 deletions 8.1-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 "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!"
echo
fi

exec "$@"
46 changes: 46 additions & 0 deletions 8.1-fpm/extensions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
amqp
apcu
bcmath
bz2
exif
gd
gettext
gmp
igbinary
imagick
imap
intl
lz4
memcache
mongodb
msgpack
mysqli
oauth
opcache
pcntl
pdo_dblib
pdo_mysql
pdo_pgsql
pdo_sqlsrv
pgsql
phalcon
psr
rdkafka
redis
soap
sockets
sodium
sqlsrv
ssh2
swoole
sysvmsg
sysvsem
sysvshm
tidy
uuid
vips
xdebug
xsl
yaml
zip
zstd
68 changes: 68 additions & 0 deletions 8.2-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 "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!"
echo
fi

exec "$@"
46 changes: 46 additions & 0 deletions 8.2-fpm/extensions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
amqp
apcu
bcmath
bz2
exif
gd
gettext
gmp
igbinary
imagick
imap
intl
lz4
memcache
mongodb
msgpack
mysqli
oauth
opcache
pcntl
pdo_dblib
pdo_mysql
pdo_pgsql
pdo_sqlsrv
pgsql
phalcon
psr
rdkafka
redis
soap
sockets
sodium
sqlsrv
ssh2
swoole
sysvmsg
sysvsem
sysvshm
tidy
uuid
vips
xdebug
xsl
yaml
zip
zstd
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 "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!"
echo
fi

exec "$@"
44 changes: 44 additions & 0 deletions 8.3-fpm/extensions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
amqp
apcu
bcmath
bz2
exif
gd
gettext
gmp
igbinary
imap
intl
lz4
memcache
mongodb
msgpack
mysqli
oauth
opcache
pcntl
pdo_dblib
pdo_mysql
pdo_pgsql
pdo_sqlsrv
pgsql
psr
rdkafka
redis
soap
sockets
sodium
sqlsrv
ssh2
swoole
sysvmsg
sysvsem
sysvshm
tidy
uuid
vips
xdebug
xsl
yaml
zip
zstd
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ However, you can still find legacy versions like `7.4.x` or `8.0.x` on [Releases
| &nbsp; | &nbsp; | &nbsp; | &nbsp; |
| **Others** | | | |
| composer | v2.7 | v2.7 | v2.7 |
| &nbsp; | &nbsp; | &nbsp; | &nbsp; |
| **Extensions file** | [8.1-fpm/extensions.txt](8.1-fpm/extensions.txt) | [8.2-fpm/extensions.txt](8.2-fpm/extensions.txt) | [8.3-fpm/extensions.txt](8.3-fpm/extensions.txt) |

**Footnotes**

Expand Down Expand Up @@ -147,7 +149,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 +183,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 additional extensions

The PHP additional 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 in `extensions.txt` file of every PHP version directory or 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 83d9123

Please sign in to comment.