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
-
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_unameAudit 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. -
Configure
open_basedirto restrict filesystem access.; /etc/php/8.3/fpm/php.ini open_basedir = /var/www/app:/tmp:/usr/share/phpThe path separator is
:on Linux. Include/tmpbecause PHP session storage and many upload libraries write to/tmpby 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 -
Disable remote file access.
; In php.ini allow_url_fopen = Off allow_url_include = OffEnforce at the pool level:
; In pool.d/app.conf php_admin_flag[allow_url_fopen] = Off php_admin_flag[allow_url_include] = OffIf your application legitimately uses
file_get_contents('https://...'), enableallow_url_fopenselectively per pool rather than globally. Never enableallow_url_include— it allows direct remote code inclusion and has no legitimate production use. -
Hide PHP version information.
; In php.ini expose_php = OffAdditionally, configure your web server to strip or replace the
X-Powered-Byheader:# Nginx: remove the header entirely fastcgi_hide_header X-Powered-By;# Apache with mod_headers Header unset X-Powered-By -
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 = 6session.use_strict_mode = Onprevents 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. -
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] = 5Tight
max_execution_timelimits reduce the window of opportunity for slow-loris-style attacks and limit damage from runaway scripts. -
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_functionsdoes not prevent PHP from using extensions that wrap the same syscalls. Thepcntlextension can fork processes; disablingpcntl_fork()fromdisable_functionsdoes not prevent an extension from callingfork()directly if it bypasses the PHP function table. Disable the extension itself withphpdismod pcntl.open_basedirbreaks session storage if/tmpis excluded. PHP’s defaultsession.save_pathis/tmp. If/tmpis not inopen_basedir, sessions fail silently. Either include/tmpor setsession.save_pathto a directory withinopen_basedir.open_basedirhas 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_functionsrequires a restart, not just a reload. Changes todisable_functionsinphp.initake effect only when FPM is fully restarted — because the function table is built at startup. Asystemctl reloaddoes not respawn existing workers.- Symlinks can bypass
open_basedir. A symlink in/var/www/appthat points to/etc/passwdis accessible if/var/www/appis inopen_basedir, even though/etcis 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.