Where Can I Find the php.ini for PHP-CLI?
When you run php -i or php --ini, you are often confronted with a confusing output that lists multiple configuration files. The most common scenario involves an Apache environment where the web server uses one configuration file, and the command-line interface (CLI) uses another. This discrepancy frequently leads to runtime errors when developers assume that a directive set in the global configuration applies everywhere.
The core issue is that PHP loads different php.ini files depending on how it was invoked. In a typical Linux distribution like Ubuntu, the web server daemon (usually running as www-data) and your interactive shell user process distinct environments. The web server often runs with Apache2Config, while CLI uses the standard cli configuration found in /etc/php/7.x/cli/php.ini.
If you have added a custom include path or defined a specific library directory in the main system-wide php.ini, it is entirely possible that this change has not propagated to the CLI interpreter. The CLI version might be reading a default file located in /etc/php/<version>/cli/php.ini that lacks your custom extensions or include paths, resulting in "undefined function" or "failed to load dynamic library" errors when you execute scripts from the terminal.
To resolve this, you must locate the specific configuration file active for your current CLI session. The most reliable method is to run the following command:
php --ini
This command outputs a report detailing the loaded ini files. You will see lines indicating "Loaded Configuration File" and potentially "Additional .ini files parsed." If you are on Ubuntu, you will likely see paths under /etc/php/7.x/cli/php.ini.
If you need to modify the configuration for CLI specifically without affecting your web server, you should edit the file indicated by the "Loaded Configuration File" output. On Debian-based systems, this is almost always /etc/php/<version>/cli/php.ini. However, if your PHP installation was compiled from source or installed via a different package manager (like Homebrew on macOS), the path might differ, such as /usr/local/etc/php/conf.d/.
A critical pitfall to avoid is editing the global system configuration file directly. Many distributions use an php.ini-development or a template that gets copied during installation. Modifying the system file can lead to permission issues if you are not running as root. Instead, it is often safer to create a custom configuration file in the conf.d directory:
sudo nano /etc/php/7.x/cli/conf.d/custom.ini
Inside this file, you can add your include paths or define environment-specific variables. This approach ensures that your changes are isolated and do not interfere with the default PHP installation or other users on the system.
Another frequent mistake involves using the include_path directive. If you have set an external library path in the web server's configuration but not in the CLI config, PHP will fail to locate those files when run from the command line. Remember that include_path is deprecated as a global variable in favor of autoloading classes or using require/include statements explicitly defined within your code. However, for legacy libraries or C extensions loaded via extension= directives, ensuring the path exists in the specific CLI configuration file is mandatory.
If you are using Laravel or other modern frameworks, you might encounter similar issues where the framework expects certain paths to be available. While some hosting providers bundle a shared php.ini, local development environments often require manual configuration. If you are managing multiple services (web and CLI) on the same machine, always verify which file is active before deploying changes.
For more complex setups involving SAPIs like FPM or CGI, the configuration might be split between the main php.ini and specific .ini files in the conf.d folder. Always check the output of php --ini after making changes to confirm that your new directives are being loaded by the correct interpreter.
Understanding these distinctions prevents unnecessary debugging time and ensures that your application behaves consistently across different execution contexts.