Every PHP extension you load increases the attack surface and memory footprint of your FPM workers. Most PHP installations that have been in service for years carry a long tail of extensions that were installed during setup and never revisited: php-gd for an image feature that was removed, php-soap for a long-dead third-party integration, phar open and executable when your application does not ship any .phar files. This article is the operator’s reference for auditing what is loaded, why it matters, and how to lock the extension set to only what your application actually uses.
How to verify
Get the authoritative list of loaded extensions and their origin:
# All loaded extensions for the FPM SAPI
php-fpm8.3 -i | grep -E '^(extension_dir|Loaded Configuration)'
php-fpm8.3 -m | sort
# CLI extensions (may differ from FPM)
php -m | sort
# Which ini files are contributing extensions
php --ini
ls /etc/php/8.3/fpm/conf.d/ | sort
# Which packages installed those ini files
dpkg -l 'php8.3-*' | grep ^ii | awk '{print $2}' | sort
What’s happening
PHP ships a set of bundled extensions that are always compiled in and cannot be disabled via ini (pcre, reflection, spl, standard). Most other extensions are either compiled as shared objects (.so files) and loaded via extension= or zend_extension= directives, or compiled statically into specific PHP builds.
For Debian/Ubuntu systems using ondrej/php, each extension is a separate package that installs a .ini file into /etc/php/8.3/mods-available/. The phpenmod and phpdismod tools create or remove symlinks in /etc/php/8.3/fpm/conf.d/ and /etc/php/8.3/cli/conf.d/ respectively. This separation lets you enable an extension for FPM without enabling it for CLI.
The production extension philosophy should be: if you cannot name the application feature that requires the extension, disable it. The cost of re-enabling a disabled extension is trivial (a phpenmod call and an FPM reload). The cost of an exploited extension is not.
Extensions to have on every production PHP server:
| Extension | Why |
|---|---|
opcache | Bytecode cache — mandatory for performance |
mbstring | Multibyte string handling — required by most frameworks |
curl | HTTP client used by almost every external integration |
openssl | TLS — required by curl, PDO, and many others |
pdo | Database abstraction layer |
pdo_mysql / pdo_pgsql | Specific database drivers — load only what you use |
json | Built-in since PHP 8.0, always available |
intl | ICU-based internationalization — required by Symfony, Laravel |
xml | XML parsing — required by many packages even if you do not parse XML directly |
tokenizer | Required by Composer itself for class scanning |
Extensions to disable unless explicitly required:
| Extension | Risk | Notes |
|---|---|---|
xdebug | High performance cost; debug socket opens outbound connections | Use trigger mode only; disable between sessions |
phar | phar.readonly=Off allows writing/executing phar files | Keep phar.readonly=On; disable if no phar files used |
posix | Exposes process, user, and group management to PHP | Rarely needed by web applications |
syslog | Allows PHP to write to system logs | Useful for logging but check if your app uses it |
pcntl | Process control — fork, exec, signal handling | Only needed for daemon/worker processes, not web FPM |
ftp | Plain-text FTP client | Replace with SFTP via ssh2 if needed |
imap | IMAP/POP3 with known CVEs in the C library | Use a modern IMAP library via SMTP instead |
ldap | LDAP authentication | Enable only if your application authenticates against LDAP |
The procedure
-
Generate an extension inventory against your application’s actual require statements.
# Extract PHP extension requirements from Composer composer show --all | grep -E '"ext-' # Or directly from composer.json and all installed packages find vendor -name composer.json -exec grep -h '"ext-' {} \; | \ grep -oP '"ext-\K[^"]+' | sort -uThis gives you the ground-truth set of extensions your application’s dependency tree requires.
-
Compare against what is currently loaded.
# Save the loaded set php-fpm8.3 -m | sort > /tmp/loaded-extensions.txt # Save what Composer says is required composer show --all 2>/dev/null | grep -oP '"ext-\K[^"]+' | \ sort -u > /tmp/required-extensions.txt # Diff — left-only items are loaded but not declared as required diff /tmp/required-extensions.txt /tmp/loaded-extensions.txt -
Disable extensions that are not in the required set.
# Disable for FPM only (keep enabled for CLI if needed) sudo phpdismod -v 8.3 -s fpm imap sudo phpdismod -v 8.3 -s fpm ftp sudo phpdismod -v 8.3 -s fpm posix sudo phpdismod -v 8.3 -s fpm pcntl # Reload FPM — no restart needed sudo systemctl reload php8.3-fpm # Verify php-fpm8.3 -m | grep -E 'imap|ftp|posix|pcntl' # Should return nothing -
Harden phar if it must remain enabled.
; /etc/php/8.3/fpm/conf.d/20-phar.ini [phar] phar.readonly = On phar.require_hash = OnWith
phar.readonly = On, phar archives cannot be created or modified at runtime — they can only be read.phar.require_hash = Onrequires all phar files to have a valid signature. -
Lock extension configuration in FPM pool with
php_admin_value.For extensions that accept ini settings, lock the sensitive ones at the pool level so
.user.inicannot override them:; /etc/php/8.3/fpm/pool.d/app.conf php_admin_flag[allow_url_fopen] = Off php_admin_flag[allow_url_include] = Off php_admin_value[phar.readonly] = 1 -
Run the application’s test suite with the reduced extension set before applying to production.
# On a staging server with the same extension set: php vendor/bin/phpunit --testdox 2>&1 | tail -20 # Run integration/smoke tests curl -sf "https://staging.example.com/health" | jq .
Common pitfalls
- Disabling an extension that a transitive dependency requires silently breaks the app. Extensions like
mbstringandxmlare pulled in by dozens of packages indirectly. Always cross-check Composer’s full dependency tree, not just yourcomposer.json. allow_url_fopen = Onis required by some legitimate use cases (remote streams) but enables remote file inclusion. Set it toOffby default at the pool level and enable it only for specific pools that genuinely need it.- Extensions disabled for FPM may still be enabled for CLI. A CLI script running as a cron job may have a different extension set than FPM. Audit both with
php-fpm8.3 -mandphp -mseparately. - PECL extensions installed without a matching distro package bypass
phpdismod— they are enabled via a manually written ini file. Track these separately. - Removing a PHP package with
apt remove php8.3-imapalso removes the ini file — which is cleaner thanphpdismodbut leaves no breadcrumb if you need to re-enable later. Preferphpdismodfor reversibility.
The right extension set for production is the smallest one that satisfies your application’s declared dependencies. Stack Harbor’s managed operations service includes PHP runtime hardening reviews that cover extension audits, phar configuration, and ini locking as part of every managed environment onboarding.