Skip to content

PHP runtime security hardening: disable_functions, open_basedir, and more

How to harden a PHP runtime using disable_functions, open_basedir, and other ini directives that constrain what PHP can do on a production server.

A compromised PHP application is significantly more dangerous when the runtime has unlimited access to the host filesystem, can execute arbitrary shell commands, and announces its exact version to every visitor. PHP’s ini configuration offers a set of security directives that constrain what PHP can do even after an application-layer compromise — they are not substitutes for secure application code, but they meaningfully limit blast radius. This runbook covers the complete set of security-relevant ini directives, their trade-offs, and how to enforce them at the FPM pool level where .user.ini cannot override them.

How to verify

Audit the current security posture of the running PHP instance:

# Check key security directives for FPM
php-fpm8.3 -i | grep -E \
  'disable_functions|open_basedir|allow_url_fopen|allow_url_include|expose_php|safe_mode|max_execution_time'

# Check for dangerous functions still enabled
php -r 'foreach (["exec","shell_exec","system","passthru","proc_open","popen","eval"] as $f) {
    echo $f . ": " . (function_exists($f) ? "ENABLED" : "disabled") . "\n";
}'

# Check if PHP version is exposed in HTTP headers
curl -sI https://example.com/ | grep -i 'x-powered-by'

# Check open_basedir restriction
php -r 'var_dump(ini_get("open_basedir"));'

What’s happening

PHP’s security directives operate at two levels: those that restrict what PHP code can invoke (functions and file operations), and those that control what information PHP reveals about itself.

disable_functions is a comma-separated list of function names that PHP will refuse to execute. This is enforced at the engine level — the functions are removed from the function table at startup, so function_exists() returns false and calling them throws a fatal error. It cannot be bypassed by application code (unlike ini_set()-based settings).

open_basedir restricts the filesystem paths that PHP functions can access. Any fopen(), include, require, or file operation that resolves to a path outside the open_basedir tree fails with a warning. It applies to most but not all file operations — extensions that bypass the PHP stream layer (some low-level PDO operations, certain PECL extensions) may not respect it.

allow_url_fopen controls whether PHP can open remote URLs with fopen(), file_get_contents(), and similar functions. Setting it to Off prevents Remote File Inclusion (RFI) attacks from fetching remote payloads. Note: allow_url_fopen = Off does not disable curl — curl has its own security model.

expose_php controls whether PHP adds X-Powered-By: PHP/8.3.x to HTTP responses. Disabling it removes version information from response headers but does not prevent fingerprinting through other means (error pages, default error formats).

Critically: these directives are bypass-resistant only when set via php_admin_value / php_admin_flag in the FPM pool configuration. Settings in php.ini can be overridden by application code using ini_set(). Settings in .htaccess or .user.ini use php_value semantics which also allow overrides. Only php_admin_* is immutable from application code.

The procedure

  1. Disable dangerous shell-execution and process-control functions.

    ; /etc/php/8.3/fpm/php.ini
    disable_functions = exec,shell_exec,system,passthru,proc_open,proc_close,proc_get_status,proc_nice,proc_terminate,popen,pclose,pcntl_exec,pcntl_fork,pcntl_signal,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wstopsig,pcntl_wtermsig,pcntl_signal_dispatch,posix_getpwuid,posix_getgrgid,posix_kill,posix_setpgid,posix_setsid,posix_setuid,posix_uname

    Audit your application before applying this list — some legitimate use cases (image processing scripts that shell out to convert, CLI tools called from web processes) use these functions. The approach should be to audit, remove the shell-based implementation if possible, and only then disable the functions.

  2. Configure open_basedir to restrict filesystem access.

    ; /etc/php/8.3/fpm/php.ini
    open_basedir = /var/www/app:/tmp:/usr/share/php

    The path separator is : on Linux. Include /tmp because PHP session storage and many upload libraries write to /tmp by default. Include any PHP include paths your application uses (e.g., PEAR paths, shared library directories).

    Enforce at the pool level for immutability:

    ; /etc/php/8.3/fpm/pool.d/app.conf
    php_admin_value[open_basedir] = /var/www/app:/tmp:/usr/share/php
  3. Disable remote file access.

    ; In php.ini
    allow_url_fopen = Off
    allow_url_include = Off

    Enforce at the pool level:

    ; In pool.d/app.conf
    php_admin_flag[allow_url_fopen] = Off
    php_admin_flag[allow_url_include] = Off

    If your application legitimately uses file_get_contents('https://...'), enable allow_url_fopen selectively per pool rather than globally. Never enable allow_url_include — it allows direct remote code inclusion and has no legitimate production use.

  4. Hide PHP version information.

    ; In php.ini
    expose_php = Off

    Additionally, configure your web server to strip or replace the X-Powered-By header:

    # Nginx: remove the header entirely
    fastcgi_hide_header X-Powered-By;
    # Apache with mod_headers
    Header unset X-Powered-By
  5. Restrict PHP session security.

    ; In php.ini or pool conf
    session.cookie_httponly = On
    session.cookie_secure = On
    session.cookie_samesite = Strict
    session.use_strict_mode = On
    session.sid_length = 48
    session.hash_bits_per_character = 6

    session.use_strict_mode = On prevents PHP from accepting a session ID from user input that does not exist in the session store — it forces session ID regeneration, blocking session fixation attacks.

  6. Set execution and resource limits.

    ; In pool.d/app.conf
    php_admin_value[max_execution_time] = 30
    php_admin_value[max_input_time] = 30
    php_admin_value[memory_limit] = 256M
    php_admin_value[post_max_size] = 32M
    php_admin_value[upload_max_filesize] = 20M
    php_admin_value[max_file_uploads] = 5

    Tight max_execution_time limits reduce the window of opportunity for slow-loris-style attacks and limit damage from runaway scripts.

  7. Validate the configuration with php-fpm -t.

    sudo php-fpm8.3 -t
    # Output should be: test is successful
    sudo systemctl reload php8.3-fpm

Common pitfalls

  • disable_functions does not prevent PHP from using extensions that wrap the same syscalls. The pcntl extension can fork processes; disabling pcntl_fork() from disable_functions does not prevent an extension from calling fork() directly if it bypasses the PHP function table. Disable the extension itself with phpdismod pcntl.
  • open_basedir breaks session storage if /tmp is excluded. PHP’s default session.save_path is /tmp. If /tmp is not in open_basedir, sessions fail silently. Either include /tmp or set session.save_path to a directory within open_basedir.
  • open_basedir has known performance overhead due to path normalization on every file operation. On applications with heavy filesystem access (reading many small files, Composer’s autoloader before classmap optimization), this can add measurable latency. Benchmark before enabling.
  • disable_functions requires a restart, not just a reload. Changes to disable_functions in php.ini take effect only when FPM is fully restarted — because the function table is built at startup. A systemctl reload does not respawn existing workers.
  • Symlinks can bypass open_basedir. A symlink in /var/www/app that points to /etc/passwd is accessible if /var/www/app is in open_basedir, even though /etc is not. Ensure the web application user cannot write to document root directories where symlinks could be created.

PHP security directives are not a substitute for secure application code, but they are a meaningful layer of defence that reduces the blast radius of an application-layer compromise. Stack Harbor’s managed operations service applies these controls as part of every PHP environment build, and maintains them through PHP version upgrades and configuration drift audits.