Skip to the fix
PHPSolvedThe fix first, then the reason

Newest fixes

Nomvula Sithole in PHP | | 6 min

Where is php.ini? Finding the file PHP actually loads

Run php --ini for the command line, and call php_ini_loaded_file() through the browser for the website. They are often two different files, and both count.

An open card catalogue drawer with one card pulled out
On this page
  1. Usual places, and why they're only a hint
  2. How PHP searches
  3. The second half: conf.d
  4. Settings that don't come from any php.ini
  5. Several PHP versions on one machine
  6. After the edit
  7. No php.ini anywhere

For the command line, one command answers it:

bash
php --ini
output
Configuration File (php.ini) Path: /opt/homebrew/etc/php/8.2
Loaded Configuration File:         /opt/homebrew/etc/php/8.2/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/8.2/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/8.2/conf.d/ext-opcache.ini

For a website that answer may be wrong, because the PHP behind the web server is another program that starts with its own settings. Ask it from inside. Put this into a file in the site, open it in the browser, and delete the file afterwards:

phpran on PHP 8.2.8
<?php
echo 'how PHP runs: ', PHP_SAPI, "\n";
echo 'version:      ', PHP_MAJOR_VERSION, '.', PHP_MINOR_VERSION, "\n";
echo 'main file:    ', php_ini_loaded_file() ?: 'none', "\n";
echo 'extra files:  ', php_ini_scanned_files() ?: 'none', "\n";
echo 'user file:    ', ini_get('user_ini.filename') ?: 'off', "\n";

Started from a terminal with no configuration at all, it prints the empty case:

real output
how PHP runs: cli
version:      8.2
main file:    none
extra files:  none
user file:    .user.ini

The first line is the interesting one in the browser. fpm-fcgi means PHP-FPM, apache2handler means the Apache module, cgi-fcgi means CGI, and cli-server means PHP's built-in development server. Everything below depends on it.

Usual places, and why they're only a hint

SystemCommand lineWebsite
Debian, Ubuntu/etc/php/8.3/cli/php.ini/etc/php/8.3/fpm/php.ini or /etc/php/8.3/apache2/php.ini
Homebrew on a Mac/opt/homebrew/etc/php/8.2/php.iniThe same file
Docker, official image/usr/local/etc/php/php.ini, if you created itThe same file
Shared hostingWhatever the host choseOften only a .user.ini of yours

The number in the path is the PHP version, and several versions can be installed side by side. On Debian and Ubuntu that gives you one folder per version and one per way of running PHP, so an edit in 8.3/cli changes nothing for a site on 8.2 with FPM. This is the most common reason for "I changed it and nothing happened".

The official Docker image is worth a separate word. Its description says that it ships php.ini-development and php.ini-production and that you should choose one. Until you do, there's no php.ini at all, "main file" prints none, and PHP runs on its built-in defaults.

How PHP searches

The manual describes the order. If the way PHP was started names a file, that one is used: PHPIniDir in Apache, the -c option on the command line. Otherwise PHP looks in the folder from the PHPRC environment variable, on Windows in a registry entry, then in the current folder except for the command line and a few others, then in the folder of the executable on Windows, and then in the folder that was fixed when PHP was compiled. In each place it tries php-SAPI.ini first, for example php-cli.ini, and then php.ini.

You can watch the first two rules. This creates a file in a temporary folder and points PHP to it in two ways:

bashran in bash
printf 'memory_limit = 64M\n' > php.ini
php -c "$PWD" -r 'echo basename(php_ini_loaded_file()), " ", ini_get("memory_limit"), PHP_EOL;'
PHPRC="$PWD" php -r 'echo basename(php_ini_loaded_file()), " ", ini_get("memory_limit"), PHP_EOL;'
php -n -r 'var_dump(php_ini_loaded_file());'
real output
php.ini 64M
php.ini 64M
bool(false)

The last line shows -n, which ignores php.ini completely. It's a good test when PHP behaves strangely: if a problem is gone with -n, a setting causes it and the code doesn't.

The second half: conf.d

After the main file PHP reads every .ini file in a scan folder. Distributions put one small file per extension there, and you can add your own. The files are read in alphabetical order and the last one wins:

bashran in bash
printf 'memory_limit = 64M\n' > php.ini
mkdir -p conf.d
printf 'memory_limit = 96M\n' > conf.d/20-shop.ini
printf 'memory_limit = 80M\n' > conf.d/30-other.ini
PHP_INI_SCAN_DIR="$PWD/conf.d" php -c "$PWD" -r 'echo ini_get("memory_limit"), PHP_EOL;'
real output
80M

The value in php.ini lost against both files, and the one starting with 30 beat the one starting with 20. A search through the whole folder therefore finds the setting faster than reading the main file:

bashran in bash
grep -rn --include='*.ini' '^memory_limit' php.ini conf.d | sort
real output
conf.d/20-shop.ini:1:memory_limit = 96M
conf.d/30-other.ini:1:memory_limit = 80M
php.ini:1:memory_limit = 64M

On a Debian or Ubuntu server the same search is grep -rn '^memory_limit' /etc/php/. Put your own changes into a file such as conf.d/99-local.ini. A package update won't ask you about it, and the difference from the standard is visible at a glance.

PHP_INI_SCAN_DIR replaces the compiled-in folder. With a colon in front, as in PHP_INI_SCAN_DIR=:/srv/php, the standard folder is read first and yours after it.

Settings that don't come from any php.ini

phpinfo() and ini_get() show the value in force, whatever its source. When that value isn't the one in the file you found, something sits on top of it.

The FPM pool. php_value[...] and php_admin_value[...] lines in the pool file are applied to every request. The FPM manual says that the admin form can't be overridden with ini_set().

A .user.ini file. With CGI and FPM, PHP looks for it in the folder of the script and upwards to the document root. It can hold only settings with the modes INI_PERDIR and INI_USER, and the manual gives 300 seconds as the default time before a changed file is read again.

.htaccess or httpd.conf. php_value and php_flag lines count when PHP is an Apache module. With FPM behind Apache the same lines in an .htaccess file are unknown to Apache. Put into the main configuration without the module, 2.4.66 refused to start with Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration; in an .htaccess file the visitor gets a 500 page.

The script. ini_set() changes settings of the mode INI_ALL or INI_USER for the current request.

To tell the last kind from the rest, compare the two values that PHP keeps. global_value is what the configuration gave, local_value is what is in force now:

phpran on PHP 8.2.8
<?php
ini_set('memory_limit', '200M');
$all = ini_get_all(null, true);
printf("%s -> %s\n", $all['memory_limit']['global_value'], $all['memory_limit']['local_value']);

foreach (['memory_limit', 'upload_max_filesize', 'short_open_tag', 'opcache.enable', 'allow_url_fopen'] as $name) {
    $access = $all[$name]['access'] ?? null;
    echo str_pad($name, 20), match ($access) {
        INI_USER => 'script only',
        INI_PERDIR, INI_PERDIR | INI_USER => 'php.ini, .user.ini, .htaccess',
        INI_PERDIR | INI_SYSTEM => 'php.ini, .user.ini, .htaccess',
        INI_SYSTEM => 'php.ini or httpd.conf',
        INI_ALL => 'anywhere, also in the script',
        null => 'extension not loaded',
        default => (string) $access,
    }, "\n";
}
real output
256M -> 200M
memory_limit        anywhere, also in the script
upload_max_filesize php.ini, .user.ini, .htaccess
short_open_tag      php.ini, .user.ini, .htaccess
opcache.enable      extension not loaded
allow_url_fopen     php.ini or httpd.conf

The run was started with -d memory_limit=256M, so the first number is 256M and not PHP's default of 128M. The table answers the next question as well: whether the place where you want to put a setting is allowed to change it. phpinfo() prints the same pair as "Local Value" and "Master Value".

Several PHP versions on one machine

A server that has been upgraded once usually has two PHP versions, and the php in your terminal isn't necessarily the one a site uses. The FPM program can be asked without going through the web server:

bash
php-fpm8.3 -i | grep -E 'Server API|Loaded Configuration File|Scan this dir'

It prints the same information as phpinfo() for that version and for FPM. The name of the program differs: php-fpm8.3 on Debian and Ubuntu, plain php-fpm on many other systems.

Which version a site uses is decided in the web server, by the socket or port it talks to. In an nginx server block look for fastcgi_pass, and in Apache for a SetHandler line with proxy:unix:. The pool file with the same socket in its listen line belongs to the version you're after:

bash
grep -rn 'fastcgi_pass' /etc/nginx/
grep -rn '^listen *=' /etc/php/*/fpm/pool.d/

Two sites on one server can therefore have different versions, different pools and different values for the same setting, all of them correct. Write down the pair of version and pool for the site you care about before you change anything.

On Windows the question is easier to answer and easier to get wrong, because several complete PHP folders may be lying around. The XAMPP FAQ has the same advice as the top of this page: open phpinfo() through the server and read "Loaded Configuration File". Remember that Apache has to be restarted afterwards.

After the edit

PHP reads php.ini once, when the process starts. A command line script starts fresh each time. A website doesn't:

  • PHP-FPM: sudo systemctl reload php8.3-fpm, with your version in the name. Restarting nginx does nothing for PHP.
  • Apache module: reload Apache.
  • Built-in server: stop it and start it again.
  • .user.ini: wait for the cache time.

Before the reload, let PHP check the file. A typo in a setting's name is accepted silently, because unknown names may belong to an extension that isn't loaded. A broken quote is reported:

bashran in bash
printf 'memory_limt = 1M\npost_max_size = 20M\n' > typo.ini
php -c typo.ini -r 'echo ini_get("post_max_size"), " ", var_export(ini_get("memory_limt"), true), PHP_EOL;'
real output
20M false

There was no message about memory_limt. A missing quote is different: the same PHP answers with syntax error, unexpected end of file and the line number, and still starts with the settings it could read. So a file with an error in the middle doesn't stop a server, it quietly cuts the configuration short. After a reload, read the value you changed through the same page you started with. Looking at the file only proves what you typed.

No php.ini anywhere

"None" as the main file isn't an error. PHP has a built-in value for everything, and the differences from a production file are large enough to notice: without a file display_errors is on and log_errors is off, so visitors see the errors and you don't. Copy the template that came with your PHP and rename it:

bash
php -r 'echo PHP_CONFIG_FILE_PATH, PHP_EOL;'
sudo cp php.ini-production "$(php -r 'echo PHP_CONFIG_FILE_PATH;')/php.ini"

The constant holds the compiled-in folder. The templates come with the PHP sources and with the official Docker image. On Ubuntu they are in the packages too, at /usr/lib/php/8.3/php.ini-production and beside it.

Which settings to look at first depends on why you came. For an upload that's refused it's the chain of size limits. For a blank page it's error reporting and the log. And if the question came from phpMyAdmin, its own file is config.inc.php and not php.ini; the checks for localhost/phpmyadmin say where that one lives.

Same subject