Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Web and Security

php.ini configuration

Reference for php.ini: where PHP finds its configuration, how ini_get() and ini_set() work, which directives can change at runtime, and recommended production values.

php.ini is PHP's configuration file. It sets limits (memory, upload size, execution time), error handling, extensions and many security options. PHP reads it once when it starts: the command line at every launch, PHP-FPM and Apache when the service starts or reloads.

Which file is used differs per installation and per mode (CLI, FPM, Apache often have separate files). You can see it from PHP with php_ini_loaded_file(), change many values at runtime with ini_set(), and read the effective value with ini_get().

INI
memory_limit = 256M
display_errors = Off
upload_max_filesize = 8M

Try it

PHP Reading the effective configuration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Changing values at runtime

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Where PHP looks

  • Main file: compiled-in default path, or the directory given by the PHPRC environment variable or the -c command-line option.
  • Scan directory: extra *.ini files (for example conf.d/20-pdo.ini), loaded alphabetically after the main file. php --ini lists them all.
  • Per-directory: .user.ini files (CGI/FPM only) hold directives marked PHP_INI_PERDIR or PHP_INI_USER. .htaccess with php_value works only with mod_php.
  • Runtime: ini_set() for directives marked PHP_INI_ALL (or PHP_INI_USER).

Check changes with php -i | grep name or the phpinfo() page (never leave that publicly accessible).

Directives you will meet often

Directive Meaning Typical production value
memory_limit Maximum memory per request. 128M to 512M
max_execution_time Seconds a script may run (0 in the CLI). 30
display_errors Print errors into the output. Off
log_errors, error_log Write errors to a log. On, a file path
error_reporting Which levels are reported. E_ALL
upload_max_filesize, post_max_size Upload and POST limits. Keep post_max_size above the upload size. 8M, 10M
max_file_uploads Files per request. 20
date.timezone Default time zone. UTC
session.cookie_httponly, session.cookie_secure, session.cookie_samesite Session cookie safety flags. 1, 1, Lax
expose_php Adds X-Powered-By: PHP. Off
disable_functions Functions blocked for the whole server. site-specific
opcache.enable Bytecode caching; see OPcache. 1

Usage notes

  • CLI and web differ. A value you see in php -i may not be what PHP-FPM uses. Check from a web request or use the right config path.
  • Values are strings. Booleans can be 1, On, true or yes. Sizes accept K, M and G. Use ini_get() and compare carefully.
  • Changing a file needs a reload of FPM or Apache; the CLI picks it up at the next run.
  • Not everything is changeable at runtime. ini_set() returns false for directives such as upload_max_filesize and post_max_size, because the request was already parsed.
  • Development vs production: show errors while developing (display_errors=On, E_ALL); in production log them and never display them. See Error reporting.
  • Ship php.ini-production or a similar template as your starting point; the PHP distribution includes both php.ini-development and php.ini-production.
  • Containers: put overrides in a small .ini file in the scan directory instead of editing the main file.