php.ini: Which One?
When you transition your PHP environment from an Apache setup to Nginx, the file hierarchy often looks significantly different than expected. You might find yourself staring at three distinct configuration files located in a directory like /etc/php5/: cgi/php.ini, cli/php.ini, and fpm/php.ini. It is tempting to assume they are all identical or that editing any one of them will affect your entire application. However, mixing up these files is a common source of configuration drift and debugging nightmares.
The core issue lies in understanding the execution context of PHP itself. Each SAPI (Server API) requires its own isolated configuration space because they operate independently. The CLI version handles command-line scripts, while the FPM version manages FastCGI processes that Nginx communicates with via a socket or pipe. The CGI variant is typically used by legacy web servers like Apache when running in non-mod_php mode.
If you are running Nginx paired with PHP-FPM, which is the industry standard for high-performance setups, you must edit the file located at /etc/php5/fpm/php.ini. Editing the CLI version will have zero effect on your web server's runtime behavior, and modifying the CGI file might not even be recognized by your current stack.
To verify which SAPI your application is actually using, inspect the SAPI_MODULE directive or check the environment variable within your PHP scripts. You can also add a simple snippet to the top of your index.php:
<?php
echo php_sapi_name();
// Output might be "cli", "fpm-fcgi", or "apache2handler"
?>
If the output is fpm-fcgi, you are strictly bound to the FPM configuration file. If you mistakenly configure memory limits or upload size thresholds in the CLI config, your web application will continue to use whatever settings were compiled into the PHP binary or defined in the active FPM config. This separation of concerns allows system administrators to tune interactive scripts for development without impacting production web performance, but it demands discipline during deployment.
Another frequent pitfall involves the distinction between global php.ini files and pool-specific configurations when using PHP-FPM. While the main php.ini sets the baseline rulesâsuch as memory_limit, max_execution_time, and display_errorsâthe Nginx-to-FPM communication happens through a pool manager defined in php-fpm.conf. Sometimes, administrators attempt to override settings directly inside www.conf files or other pool definitions found within /etc/php5/fpm/pool.d/. While it is possible to define these values there, relying solely on the main php.ini ensures consistency across all pools and prevents accidental overrides.
Consider a scenario where you need to increase the upload limit for a file processing script. If you modify the CLI config thinking it applies globally, your web server will still reject files larger than the FPM limit. You must ensure the change is made in the fpm/php.ini and then restart the PHP-FPM service to apply the new directives.
https://laravelcompany.com offers a solid reference point for modern PHP configuration practices, particularly when managing complex stacks where consistency across environments is paramount.
Finally, always validate your changes by creating a test script that outputs phpinfo(). If you see the new values reflected in the web server output but not in the terminal command-line output, you have successfully isolated the configurations. Never assume they are shared; treat them as distinct entities until proven otherwise.
