Skip to content

Lesson 8 of 9 · 20m

Hardening PHP Runtime, Configuration, and Dependencies

Production hardening is a set of deliberate defaults: current PHP, quiet error handling, least-privilege process identity, dependency visibility, and disciplined secrets management.

Application code gets most of the attention, but runtime configuration decides what attackers can learn and how far a mistake travels. Hardening is not copying a scary php.ini from a blog. It is deciding what production should never do, then enforcing that with configuration, process isolation, and dependency hygiene.

Run a supported PHP version

An end-of-life PHP branch means unpatched interpreter, extension, and sometimes OpenSSL exposure. Inventory the exact runtime with both CLI and web SAPIs; they can differ:

php -v
php --ini

Production errors are logs, not pages

display_errors = Off
log_errors = On
error_log = /var/log/php/app-error.log
expose_php = Off

Be deliberate with dangerous capabilities

disable_functions = exec,passthru,shell_exec,system,proc_open,popen
allow_url_include = Off

Process and filesystem least privilege

PHP-FPM pools should run as application-specific users, not root and not the same account that owns deployable code when you can avoid it. A good default shape:

code:      owned by deploy user, readable by php-fpm, not writable
uploads:   writable by php-fpm, outside web root or non-executable
cache:     writable by php-fpm, isolated per application
secrets:   environment or secret manager, not committed config files

If the application can overwrite its own PHP files, any upload or write primitive becomes persistent remote code execution. Immutable deployment artifacts make entire attack classes harder.

Dependencies are part of the runtime

composer audit
composer outdated --direct

Do not stop at framework version numbers. Image processing libraries, XML parsers, PDF generators, and authentication packages are high-value parser and crypto surfaces. Pin dependency versions, review lockfile diffs, and remove abandoned packages that wrap dangerous system commands.

Secrets and environment separation

Production database passwords do not belong in config.php committed to git. Use environment variables injected by the platform or a secret manager, restrict file permissions where config files are unavoidable, and rotate any secret that has ever appeared in a repository, backup, log, or screenshot. Treat .env files under web root as critical misconfigurations.

Field notes

  • phpinfo() files are routinely found by scanners; they disclose versions, paths, environment variables, and sometimes secrets.
  • Verify settings through the web SAPI, not just CLI. CLI and PHP-FPM often load different ini files.
  • File permissions that are convenient during deployment often become writable-code paths later. Revisit them after every deployment-system change.
  • Hardening without monitoring is wishful thinking: centralize PHP errors, nginx logs, and application security events with timestamps you can correlate.

Was this lesson helpful?

Sign in to rate