Skip to content

PHP extensions for production: what to enable, what to disable

Which PHP extensions belong on a production server, which ones carry security or performance risk, and how to audit your loaded extension set systematically.

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:

ExtensionWhy
opcacheBytecode cache — mandatory for performance
mbstringMultibyte string handling — required by most frameworks
curlHTTP client used by almost every external integration
opensslTLS — required by curl, PDO, and many others
pdoDatabase abstraction layer
pdo_mysql / pdo_pgsqlSpecific database drivers — load only what you use
jsonBuilt-in since PHP 8.0, always available
intlICU-based internationalization — required by Symfony, Laravel
xmlXML parsing — required by many packages even if you do not parse XML directly
tokenizerRequired by Composer itself for class scanning

Extensions to disable unless explicitly required:

ExtensionRiskNotes
xdebugHigh performance cost; debug socket opens outbound connectionsUse trigger mode only; disable between sessions
pharphar.readonly=Off allows writing/executing phar filesKeep phar.readonly=On; disable if no phar files used
posixExposes process, user, and group management to PHPRarely needed by web applications
syslogAllows PHP to write to system logsUseful for logging but check if your app uses it
pcntlProcess control — fork, exec, signal handlingOnly needed for daemon/worker processes, not web FPM
ftpPlain-text FTP clientReplace with SFTP via ssh2 if needed
imapIMAP/POP3 with known CVEs in the C libraryUse a modern IMAP library via SMTP instead
ldapLDAP authenticationEnable only if your application authenticates against LDAP

The procedure

  1. 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 -u

    This gives you the ground-truth set of extensions your application’s dependency tree requires.

  2. 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
  3. 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
  4. Harden phar if it must remain enabled.

    ; /etc/php/8.3/fpm/conf.d/20-phar.ini
    [phar]
    phar.readonly = On
    phar.require_hash = On

    With phar.readonly = On, phar archives cannot be created or modified at runtime — they can only be read. phar.require_hash = On requires all phar files to have a valid signature.

  5. 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.ini cannot 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
  6. 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 mbstring and xml are pulled in by dozens of packages indirectly. Always cross-check Composer’s full dependency tree, not just your composer.json.
  • allow_url_fopen = On is required by some legitimate use cases (remote streams) but enables remote file inclusion. Set it to Off by 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 -m and php -m separately.
  • 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-imap also removes the ini file — which is cleaner than phpdismod but leaves no breadcrumb if you need to re-enable later. Prefer phpdismod for 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.